错误(?)coord_flip()与coord_cartesian()和geom_errorbar()无法正常工作

时间:2017-02-14 11:59:54

标签: r ggplot2

我用这个代码来生成情节:

df <- data.frame(x = c("Male", "Female"),y = c(5, 6), error = c(1, 10))
ggplot(df,aes(x=x, y=y, ymax=y+error, ymin=y-error))+
geom_errorbar(width=.15)+
coord_cartesian(ylim=c(0,10))+ 
geom_point(shape=22, size=3, fill="red")+
coord_flip()+
theme_bw(20)

enter image description here 据报道,coord_cartesian()似乎与coord_flip()正常工作。事实上评论:

#coord_flip()+

生成以下内容:

enter image description here

我如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

就像@Axeman在他/她的评论中所说的那样,如果你在你的ggplot对象中包含coord_flip(ylim=c(0,10),你将会在翻转坐标的情况下得到你问题中的第二个情节。事实上,您甚至不需要coord_cartesian - 以下代码......

ggplot(df,aes(x=x, y=y, ymax=y+error, ymin=y-error))+
geom_errorbar(width=.15)+
geom_point(shape=22, size=3, fill="red")+
coord_flip(ylim = c(0,10))+
theme_bw(20)

...制作这个情节:

enter image description here