ggplot指定类别x r的垂直段的位置 [英] ggplot specify position of vertical segments for categorical x r
问题描述
我正在绘制行数据,并添加了一段可信区间和一个黑点,用于统计计算出的拟合值.
I am plotting row data and I added a segment of the credible intervals and a black point for the fitted values statistically calculated.
我的问题是我希望这些行(和黑点)相对于行数据略微(水平)移动.
My problem is that I would like to have these lines (and the black point) slightly moved (horizontally) respect to the row data.
我尝试了抖动及其所有可能的组合,结果很糟糕,因为我从不同的列获取y的开始和结束的值...因此,随着抖动的出现,线条不再水平了.
I tried jitter and all the possible combinations of it, the results are terrible because I get the values of y start and end from different columns...hence with jitter the lines are not anymore horizontal.
我试图向x(pb_values)添加一个固定值,但是由于x是一个因数,所以它给了我一个错误.
I tried to add a fixed value to the x (pb_values) however it gives me an error because my x is a factor.
dat_stack1(我的数据集):
dat_stack1 (my dataset):
dat_stack1<-data.frame(pb_cluster= rep(3:5, 6))
dat_stack1$pb_cluster<-factor(dat_stack1$pb_cluster)
dat_stack1$total<-c(28, 12, 3, 326, 14, 125, 74, 40, 115, 382, 70, 36, 36, 23, 28,185,19,107)
dat_stack1$couple<-factor(c(1, 1, 1, 2, 2, 2, 5, 5, 5, 8, 8, 8, 9, 9, 9, 11, 11, 11))
dat_stack1$lower<-c(55, 0.1851138, 8.9413495, 55.5002200, 0.1851138, 8.9413495, 55.5002200, 0.1851138, 8.9413495, 55.5002200, 0.1851138, 8.9413495, 55.5002200, 0.1851138, 8.9413495, 55.5002200, 0.1851138, 8.9413495)
dat_stack1$upper<-c(225.47047, 68.04097, 114.92182, 225.47047, 68.04097, 114.92182, 225.47047, 68.04097, 114.92182, 225.47047,68.04097,114.92182, 225.47047, 68.04097, 114.92182, 225.47047, 68.04097, 114.92182)
dat_stack1$fit<-c(124.93260, 18.87026, 46.84022,124.93260, 18.87026,46.84022,124.93260, 18.87026, 46.84022, 124.93260, 18.87026, 46.84022,124.93260, 18.87026, 46.84022, 124.93260 ,18.87026 ,46.84022)
g<-g_stack<-ggplot(data=dat_stack1, aes(x=pb_cluster, y = total, shape=couple))+
geom_point()+
scale_x_discrete(limits=c("3","4","5"), labels=c("mate", "familiar","unfamiliar"))+
theme(axis.text.x = element_text(angle=90))+
geom_segment(aes(x=pb_cluster, xend=pb_cluster, y = lower, yend = upper))+
geom_point(aes(x=pb_cluster, y = fit),color="black")
不幸的是,我的声誉太低,无法发布图片!但是代码现在可以重现
Unfortunately my reputation is too low to post images!But the code is now reproducible
有人知道如何移动这些垂直线吗?
Any idea how to move these vertical lines?
我尝试过的事情:
geom_segment(aes(x=pb_cluster, xend=pb_cluster,
y = lower, yend = upper)+position="jitter")
和
geom_segment(aes(x=pb_cluster +0.1, xend=pb_cluster+0.1,
y = lower, yend = upper))
以及所有可能的括号组合.
And all the possible combinations of parentheses.
当然,我在网上寻找相似的图形,但找不到任何图形->通常,垂直线是直接从数据点计算而来,而不是添加到不同列的行数据中!
Of course I looked online for similar graph but couldn't find any--> usually vertical lines are calculated directly from the data points and not added to the row data from different columns!
推荐答案
如果您的x值确实是因子,则可以在geom_segment()
和最后一个geom_point()
中的x值周围使用as.numeric()
不变,然后移动线和点.
If your x values really are factors then you can uses as.numeric()
around x values in geom_segment()
and last geom_point()
to be able to add some small constant and then shift line and point.
ggplot(data=dat_stack1, aes(x=pb_cluster, y = total, shape=couple))+
geom_point()+
scale_x_discrete(limits=c("3","4","5"),labels=c("mate", "familiar","unfamiliar"))+
theme(axis.text.x = element_text(angle=90))+
geom_segment(aes(x=as.numeric(pb_cluster)+.1, xend=as.numeric(pb_cluster)+.1,
y = lower, yend = upper))+
geom_point(aes(x=as.numeric(pb_cluster)+0.1, y = fit),color="black")
这篇关于ggplot指定类别x r的垂直段的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!