R ggplot2:使用stat_summary(均值)和对数比例 [英] R ggplot2: using stat_summary (mean) and logarithmic scale

查看:4013
本文介绍了R ggplot2:使用stat_summary(均值)和对数比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着时间的推移,我有很多测量数据,我想在R中绘制它们。下面是我的数据样本。我对4个时间点中的每一个都有6次测量:

 值<-c(1012.0,1644.9,837.0,1200.9 ,1652.0,981.5,
2236.9,1697.5,2087.7,1500.8,
2789.3,1502.9,2051.3,3070.7,3105.4,
2692.5,1488.5,1978.1,1925.4,1524.3,
2772.0 (c(0,12,24,72),c(6,6,6)))


$ b这些数据的规模是任意的,实际上我将对它进行规范化,以使t = 0的平均值为1。

  norm < -  values / mean(values [time == 0])

到目前为止这么好。使用 ggplot ,我绘制了各个点以及在每个时间点通过平均线的线。

  require(ggplot2)
p < - ggplot(data = data.frame(time,norm),mapping = aes(x = time,y = norm))+
stat_summary(fun.y = mean,geom =line,mapping = aes(group = 1))+
geom_point()

但是,现在我想要应用对数刻度,这就是我的麻烦开始的地方。当我这样做时:

  q < -  ggplot(data = data.frame(time,norm),mapping = aes(x = time,y = norm))+ 
stat_summary(fun.y = mean,geom =line,mapping = aes(group = 1))+
geom_point()+
scale_y_log2 ()

该行在t = 0时不会经过0,正如您所期望的那样,因为log 1)== 0.相反,这条线横穿y轴略低于0。显然, ggplot 会应用log变换后的平均值,它给出一个不同的结果。我希望它能够在日志转换之前使用 >首先应用平均值?有没有更好的方法来创建此图表?

解决方案

scale_y_log2()将首先进行转换,然后计算几何。



coord_trans()会做相反的处理:计算

所以你需要 coord_trans(ytrans =log2),而不是 scale_y_log2()


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 <- c (1012.0, 1644.9, 837.0, 1200.9, 1652.0, 981.5, 
    2236.9, 1697.5, 2087.7, 1500.8,
    2789.3, 1502.9, 2051.3, 3070.7, 3105.4, 
    2692.5, 1488.5, 1978.1, 1925.4, 1524.3,
    2772.0, 1355.3, 2632.4, 2600.1)
time <- factor (rep (c(0, 12, 24, 72), c(6, 6, 6, 6)))

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 <- values / mean (values[time == 0])

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 <- ggplot(data = data.frame(time, norm), mapping = aes (x = time, y = norm)) +
    stat_summary (fun.y = mean, geom="line", mapping = aes (group = 1)) +
    geom_point()

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

q <- ggplot(data = data.frame(time, norm), mapping = aes (x = time, y = norm)) +
    stat_summary (fun.y = mean, geom="line", mapping = aes (group = 1)) +
    geom_point() + 
    scale_y_log2()

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?

解决方案

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()

这篇关于R ggplot2:使用stat_summary(均值)和对数比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆