R ggplot2: using stat_summary (mean) and logarithmic scale

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

I have a bunch of measurements over time and I want to plot them in R. Here is a sample of my data. I've got 6 measurements for each of 4 time points:

values 

The scale of these data is arbitrary, and in fact I'm going to normalize it so that the average of t=0 is 1.

norm 

So far so good. Using ggplot, I plot both the individual points, as well as a line that goes through the average at each time point:

require (ggplot2) p 

However, now I want to apply a logarithmic scale, and this is where my trouble starts. When I do:

q 

The line does NOT go through 0 at t=0, as you would expect because log (1) == 0. Instead the line crosses the y-axis slightly below 0. Apparently, ggplot applies the mean after log transformation, which gives a different result. I want it to take the mean before log transformation.

How can I tell ggplot to apply the mean first? Is there a better way to create this chart?

回答1:

scale_y_log2() will do the transformation first and then calculate the geoms.

coord_trans() will do the opposite: calculate the geoms first, and the transform the axis.

So you need coord_trans(ytrans = "log2") instead of scale_y_log2()



回答2:

f1 


回答3:

The best solution I found for this issue was to use a combo of coord_trans() and scale_y_continuous(breaks = breaks)

As previously suggested, using coord_trans will scale your axis without transforming the data, however it will leave you with an ugly axis.

Setting the limits in coord_trans works for some things, but if you want to fix your axis to have specific labels, you will then include scale_y_continuous with the breaks you'd like set.

coord_trans(y = 'log10') + scale_y_continuous(breaks = breaks) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!