如何指出每个图来修正y轴(许多图,两个y轴,在R中用ggplot2) [英] How to point each plot to correct y axis (many plots, two y axes, in R with ggplot2)
问题描述
除了一件事之外,我能够做到这一点:我如何确保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 = (aes(x = x,ymin = mina,ymax = maxa,y = meana,color =a))+
geom_point(aes(aes (x = x,y = c,color =b))+
scale_y_continuous(sec.axis = sec_axis(〜。* 0.2))
df是一个数据帧,其列名是您看到的所有变量如上所列,所有的行值都是相应的数据点。
你可以得到你想要的东西,坚持哈德利的大炮和图形福音语法,如果你转换哟你的DF从广泛到长久,并采用不同的aes(即形状,颜色,填充)在手段和CI之间。
你没有提供一个可重复的例子,所以我雇用了我自己的。 (在帖子结尾的Dput)
df2 < - df%>%
mutate(CatCI = if_else(is.na(CI),,Cat))#创建一个分类名称以将CI映射到图例。
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)),##过滤CI中的NA
aes(y =(CI / 0.2),##变换CI的y位置以适合右轴。
fill = CatCI),##调用aes aes
shape = 25,size = 5,alpha = 0.25)+ ##我改变了形状,大小,并填充以帮助实现可视化
scale_y_continuous(sec.axis = sec_axis(〜。* 0.2,name =P Value))+
labs(color =Linerange \Sinister Axis,fill = P值\ nDexter Axis,y =平均值)
结果:
数据框:
df < - structure(list(Cat = c(a,b,c ,a,b,c,a,b,
c,a,b,c,a,b (2),2,2.20689655172414,
2.20689655172414,2.20689655172414,2.41379310344828,2.41379310344828,$ b $ ,平均值= 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 ),最大值= 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.74586 8763626567,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 ,15L),class =data.frame)
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. Now I would like to plot all five data series, but use a second axis for the p values.
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?
This is what it looks like now. The bottom two data series should be scaled up to the Y axis to the right.
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.
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 (i.e. shape, color, fill) between means and CI.
You did not provide a reproducible example, so I employ my own. (Dput at the end of the post)
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")
这篇关于如何指出每个图来修正y轴(许多图,两个y轴,在R中用ggplot2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!