【问题标题】:How to point each plot to correct y axis (many plots, two y axes, in R with ggplot2)如何将每个图指向正确的 y 轴(许多图,两个 y 轴,在 R 中使用 ggplot2)
【发布时间】:2018-01-20 06:56:01
【问题描述】:

因此,我使用一系列输入将两组与第三组进行了比较。对于三组中的每一组,我都有一个输入范围的值和置信区间。对于这两个比较,我也有该输入范围的 p 值。现在我想绘制所有五个数据系列,但使用第二个轴作为 p 值。

我能够做到这一点,除了一件事:我如何确保 R 知道将哪个图分配给第二个轴?

这就是现在的样子。底部的两个数据系列应向右放大到 Y 轴。

ggplot(df) + 
  geom_pointrange(aes(x=x, ymin=minc, ymax=maxc, y=meanc, color="c")) + 
  geom_pointrange(aes(x=x, ymin=minb, ymax=maxb, y=meanb, color="b")) +
  geom_pointrange(aes(x=x, ymin=mina, ymax=maxa, y=meana, color="a")) +
  geom_point(aes(x=x, y=c, color="c")) +
  geom_point(aes(x=x, y=b, color="b")) +
  scale_y_continuous(sec.axis = sec_axis(~.*0.2))

df 是一个数据框,其列名是上面列出的所有变量,所有行值都是对应的数据点。

【问题讨论】:

  • 可以放大p值/0.2
  • @SRivero Ha,我喜欢这个主意。现在将使用它。虽然我认为有一种方法可以为每个绘图提供一个定义单位或 y 轴的属性,这样您就不需要更改数据值来绘制它。
  • @SRivero 你的意思是哈德利的第二个答案?嗯,我明白了,谢谢。您会添加您的评论作为答案吗?我看到了 C.K. 的第四个答案。还建议您解决该问题。
  • 另见here

标签: r ggplot2 axis axis-labels axes


【解决方案1】:

如果您将 DF 从宽变为长,并在均值和 CI 之间使用不同的 aes(即形状、颜色、填充),您可以得到您想要的,忠于 Hadley 的大炮和图形福音语法。

您没有提供可重现的示例,因此我使用了自己的示例。 (文末置顶)

df2 <- df %>% 
       mutate(CatCI = if_else(is.na(CI), "", Cat)) # Create a categorical name to map the CI to the legend.

ggplot(df2, aes(x = x)) +
      geom_pointrange(aes(ymin = min, ymax = max, y = mean, color = Cat), shape = 16) +
      geom_point(data =  dplyr::filter(df2,!is.na(CI)), ## Filter the NA within the CI
            aes(y = (CI/0.2),  ## Transform the CI's y position to fit the right axis.
            fill = CatCI), ## Call a second aes the aes
            shape = 25, size = 5, alpha = 0.25 ) + ## I changed shape, size, and fillto help with visualization
      scale_y_continuous(sec.axis = sec_axis(~.*0.2, name = "P Value")) +
      labs(color = "Linerange\nSinister Axis", fill = "P value\nDexter Axis", y = "Mean")

结果:

数据框:

df <- structure(list(Cat = c("a", "b", "c", "a", "b", "c", "a", "b", 
"c", "a", "b", "c", "a", "b", "c"), x = c(2, 2, 2, 2.20689655172414, 
2.20689655172414, 2.20689655172414, 2.41379310344828, 2.41379310344828, 
2.41379310344828, 2.62068965517241, 2.62068965517241, 2.62068965517241, 
2.82758620689655, 2.82758620689655, 2.82758620689655), mean = c(0.753611797661977, 
0.772340941644911, 0.793970086962944, 0.822424652072316, 0.837015408776649, 
0.861417383841253, 0.87023105762465, 0.892894201949377, 0.930096326498796, 
0.960862178366363, 0.966600321596147, 0.991206984637544, 1.00714201832596, 
1.02025006679944, 1.03650896186786), max = c(0.869753641121797, 
0.928067675294351, 0.802815304215019, 0.884750162053761, 1.03609814491961, 
0.955909854315582, 1.07113399603486, 1.02170928767791, 1.05504846273091, 
1.09491706586801, 1.20235615364205, 1.12035782960649, 1.17387406039167, 
1.13909154635088, 1.0581878034897), min = c(0.632638511783381, 
0.713943701135991, 0.745868763626567, 0.797491261486603, 0.743382797144923, 
0.827693203320894, 0.793417962991821, 0.796917421637021, 0.92942504556723, 
0.89124101157585, 0.813058838839382, 0.91701749675892, 0.943744642652422, 
0.912869230576973, 0.951734254896252), CI = c(NA, 0.164201137643034, 
0.154868406784159, NA, 0.177948094206453, 0.178360305763648, 
NA, 0.181862670931493, 0.198447350829814, NA, 0.201541499248143, 
0.203737532636542, NA, 0.205196077692786, 0.200992205838595), 
    CatCI = c("", "b", "c", "", "b", "c", "", "b", "c", "", "b", 
    "c", "", "b", "c")), .Names = c("Cat", "x", "mean", "max", 
"min", "CI", "CatCI"), row.names = c(NA, 15L), class = "data.frame")

【讨论】:

    猜你喜欢
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    相关资源
    最近更新 更多