如何移动两个geom的x轴相对位置 [英] How to shift x axis positions of two geoms relative to each other
本文介绍了如何移动两个geom的x轴相对位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 ggplot
图。我需要将误差线相对于抖动点。我的代码是:
data(cabbages,package =MASS)
require ggplot2)
pos_1< - position_jitterdodge(
jitter.width = 0.25,
jitter.height = 0,
dodge.width = 0.9
)
gg < -
ggplot(数据= cabbages,
aes(
x = Cult,
y = HeadWt,
color = Cult,
fill = Cult
))+
geom_jitter(alpha = 0.4,position = pos_1)+
stat_summary(fun.y =意思是,geom =point,size = 3)+
stat_summary(fun.data =mean_cl_normal,
geom =errorbar,
width = 0.05,
lwd = 1,
fun.args = list(conf.int = 0.95)
)+
theme_bw()
print (gg)
目前的结果是:
我需要这样的东西:
解决方案
您可以在 aes $中为
( x
添加一个偏移量c code> stat_summary aes(x = as.numeric(Cult)+ 0.2)
):
ggplot(data = cabbages,
aes(x = Cult,
y = HeadWt,
color = Cult,
fill = Cult))+
geom_jitter(alpha = 0.4,position = pos_1)+
stat_summary(aes(x = as.numeric(Cult)+ 0.2),fun .y =mean,geom =point,size = 3)+
stat_summary(aes(x = as.numeric(Cult)+ 0.2),fun.data =mean_cl_normal,
geom =errorbar,
宽度= 0.05,
lwd = 1,
fun.args = list(conf.int = 0.95))+
theme_bw()
I have a ggplot
plot. I need to shift error bars relative to jittered points. My code is:
data("cabbages", package = "MASS")
require("ggplot2")
pos_1 <- position_jitterdodge(
jitter.width = 0.25,
jitter.height = 0,
dodge.width = 0.9
)
gg <-
ggplot(data = cabbages,
aes(
x = Cult,
y = HeadWt,
colour = Cult,
fill = Cult
)) +
geom_jitter(alpha = 0.4, position = pos_1) +
stat_summary(fun.y = "mean", geom = "point", size = 3) +
stat_summary(fun.data = "mean_cl_normal",
geom = "errorbar",
width = 0.05,
lwd = 1,
fun.args = list(conf.int = 0.95)
) +
theme_bw()
print(gg)
Current result is:
And I need something like this:
解决方案
You may add an offset to x
in aes
in eachstat_summary
(aes(x = as.numeric(Cult) + 0.2)
):
ggplot(data = cabbages,
aes(x = Cult,
y = HeadWt,
colour = Cult,
fill = Cult)) +
geom_jitter(alpha = 0.4, position = pos_1) +
stat_summary(aes(x = as.numeric(Cult) + 0.2), fun.y = "mean", geom = "point", size = 3) +
stat_summary(aes(x = as.numeric(Cult) + 0.2), fun.data = "mean_cl_normal",
geom = "errorbar",
width = 0.05,
lwd = 1,
fun.args = list(conf.int = 0.95)) +
theme_bw()
这篇关于如何移动两个geom的x轴相对位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文