Building a logarithmic scale in R rCharts using NVD3

I use rCharts to create an interactive scatter plot in R. The following code works very well:

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

I need to use the log scale for the X and Y axis. How can I specify this in rCharts without hacking the html / javascript generated?

Update 1: A more realistic and static version of the plot that I am trying to get 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'))
+3
source share
1 answer

I think this question is a little old, but I had a similar problem and solve this problem using the information in this post: rCharts nvd3 locks the library lock

Here is my solution for a chart with a breakdown on a base scale with a base of 10, it should not 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
+2
source

All Articles