简体   繁体   English

如何将每个图指向正确的y轴(许多图,两个y轴,带ggplot2的R)

[英]How to point each plot to correct y axis (many plots, two y axes, in R with ggplot2)

So I have compared two groups with a third using a range of inputs. 因此,我比较了两组和一组使用一系列输入的一组。 For each of the three groups I have a value and a confidence interval for a range of inputs. 对于这三组中的每组,我都有一个值和一个置信区间,用于一系列输入。 For the two comparisons I also have a p-value for that range of inputs. 对于这两个比较,我对于输入范围也有一个p值。 Now I would like to plot all five data series, but use a second axis for the p values. 现在,我想绘制所有五个数据系列,但使用第二个轴作为p值。

I am able to do that except for one thing: how do I make sure that R knows which of the plots to assign to the second axis? 除了一件事情,我能够做到这一点:我如何确保R知道将哪个图分配给第二个轴?

This is what it looks like now. 这就是现在的样子。 The bottom two data series should be scaled up to the Y axis to the right. 底部两个数据系列应按比例放大到右侧的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 is a dataframe whose column names are all the variables you see listed above, all row values are the corresponding datapoints. df是一个数据框,其列名是上面列出的所有变量,所有行值都是相应的数据点。

在此处输入图片说明

You can get what you want, staying true to Hadley's cannon and Grammar of Graphics gospel, if you transform your DF from wide to long, and employ a different aes (ie shape, color, fill) between means and CI. 如果将DF从宽变长到长,并在均值和CI之间采用不同的aes(即形状,颜色,填充),则可以得到想要的东西,忠实于Hadley的加农炮和Graphics语法。

You did not provide a reproducible example, so I employ my own. 您没有提供可复制的示例,因此我雇用了自己的示例。 (Dput at the end of the post) (帖子末尾的Dput)

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

Result: 结果:

![在此处输入图片描述

Dataframe: 数据框:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM