Вопрос

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'))
Это было полезно?

Решение

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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top