问题
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.
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.
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 (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?
What I tried:
geom_segment(aes(x=pb_cluster, xend=pb_cluster,
y = lower, yend = upper)+position="jitter")
and
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!
回答1:
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")
来源:https://stackoverflow.com/questions/28672705/ggplot-specify-position-of-vertical-segments-for-categorical-x-r