R ggplot2:使用 stat_summary (平均值)和对数刻度

发布于 2024-09-12 03:03:36 字数 1206 浏览 3 评论 0原文

我有一堆随时间变化的测量结果,我想在 R 中绘制它们。这是我的数据示例。我对 4 个时间点中的每一个进行了 6 个测量:

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

这些数据的规模是任意的,事实上我将对其进行标准化,以便 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。相反,该线穿过略低于 0 的 y 轴。显然,ggplot 应用对数变换后的均值,这会给出不同的结果。我希望它采用对数转换之前的均值。

我如何告诉 ggplot 首先应用平均值?有没有更好的方法来创建这个图表?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蛮可爱 2024-09-19 03:03:38

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

coord_trans() 将执行相反的操作:首先计算几何图形,然后变换轴。

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

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

偏爱你一生 2024-09-19 03:03:38

如果您不想使用 coord_trans() 但仍想转换数据,解决此问题的解决方法是创建一个将对其进行反向转换的函数:

f1 <- function(x) {
  log10(mean(10 ^ x)) 
}

stat_summary (fun.y = f1, geom="line", mapping = aes (group = 1))

A work around to solve it, if you don´t want to use coord_trans() and still want to transform the data, is to create a function which will back transform it:

f1 <- function(x) {
  log10(mean(10 ^ x)) 
}

stat_summary (fun.y = f1, geom="line", mapping = aes (group = 1))
无边思念无边月 2024-09-19 03:03:38

我找到的解决此问题的最佳解决方案是使用 coord_trans()scale_y_continuous(breaks = Breaks) 的组合

如前所述,使用 coord_trans > 将在不转换数据的情况下缩放你的轴,但是它会给你留下一个丑陋的轴。

coord_trans 中设置限制适用于某些情况,但如果您想修复轴以具有特定标签,则需要将 scale_y_continuous 包含在您想要设置的中断中。

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文