3

I am using rCharts to create an interactive scatter plot in R. The following code works just fine:

library(rCharts)
test.df <- data.frame(x=sample(1:100,size=30,replace=T),
                      y=sample(10:1000,size=30,replace=T), 
                      g=rep(c('a','b'),each=15))
n1 <- nPlot(y ~ x, group="g", data=test.df, type='scatterChart')
n1

What I need is to use a log-scale for both X- and Y-axis. How can I specify this in rCharts without hacking the produced html/javascript?

Update 1: A more realistic and static version of the plot I am trying to get plotted with rCharts:

set.seed(2935)
y_nbinom <- c(rnbinom(n=20, size=10, mu=90), rnbinom(n=20, size=20, mu=1282), rnbinom(n=20, size=30, mu=12575))
x_nbinom <- c(rnbinom(n=20, size=30, mu=100), rnbinom(n=20, size=40, mu=1000), rnbinom(n=20, size=50, mu=10000))
x_fixed <- c(rep(100,20), rep(1000,20), rep(10000,20))
realp <- rep(0:2, each=2) * 20 + sample(1:20, size=6, replace=F)
tdf <- data.frame(y = c(y_nbinom,y_nbinom,y_nbinom[realp]), x=c(x_fixed,x_nbinom,x_nbinom[realp]), type=c(rep(c('fixed','nbinom'),each=60), rep('real',6)))
with(tdf, plot(x, y, col=type, pch=19, log='xy'))
dojuba
  • 2,199
  • 1
  • 18
  • 16
  • Do you just want to plot `log(x)` vs. `log(y)`? Easiest way is to transform the data directly. – Ramnath Feb 18 '14 at 15:06
  • 1
    @Ramnath thanks for reply. I keep this solution as a last resort. I prefer the non-transformed values on a transformed axis - I want to use the interactive ability of this plot to draw attention to differences between groups by turning them on and off; and it would be much easier for the audience to see the non-transformed values directly rather than calculating exp while listening... – dojuba Feb 18 '14 at 16:03
  • Can you provide a static plot so that I understand what you are looking for? – Ramnath Feb 18 '14 at 18:06
  • @Ramnath I added the static version of the plot which looks now very similar to that I eventually would like to produce in rCharts / slidify for a presentation. The reason to keep the data untransformed is just for the audience... I thought using a log-scale in rCharts/NVD3 would be trivial, but perhaps it isn't due to the 'magnify' control. – dojuba Feb 19 '14 at 09:38

1 Answers1

2

I think this question is a bit old but I had a similar problem and solve it using the info in this post: rCharts nvd3 library force ticks

Here my solution for a base10 log-scaled stacked area chart, it shouldn't be too different for a scatter plot.

df<-data.frame(x=rep(10^seq(0,5,length.out=24),each=4),
           y=round(runif(4*24,1,50)),
           var=rep(LETTERS[1:4], 4))
df$x<-log(df$x,10)

p <- nPlot(y ~ x, group = 'var', data = df,
         type = 'stackedAreaChart', id = 'chart')

p$xAxis(tickFormat = "#!function (x) {    
tickformat = [1,10,100,1000,10000,'100k'];
return tickformat[x];}!#")

p
Community
  • 1
  • 1
Jon Nagra
  • 1,538
  • 1
  • 16
  • 36
  • Thank you for taking the time for an old question. It is not as straightforward as I'd prefer it to be (two steps: log the data, relabel the axis ticks), but definitely does the job! – dojuba Mar 10 '15 at 14:50
  • I have found a slightly better (more general) way to do this: p$xAxis(tickFormat = "#!function (x) { return Math.round(100*Math.pow(10,x))/100;}!#"). You can substitute 10 for any base. The 100 is to avoid numbers with more than two decimals. – Jon Nagra Mar 24 '15 at 13:36