我正在尝试创建一个旋转的条形图,其名称在Y轴上,值(-0.5,0,0.5)在X轴上。我有酒吧工作,但当我试图添加scale_x_discrete Y轴标签消失。我做错了什么?
dtm <- data.frame(
month = factor(c("Police","Country","Rules")),
value = c(-.51,-.1,.9)
)
dtm$colour <- ifelse(dtm$value < 0, "firebrick1","steelblue")
ggplot(dtm, aes(month, value, label = month)) + geom_text(aes(label=value), position = position_dodge(width = .9)) +
geom_bar(stat = "identity",aes(fill = colour))+
coord_flip() + labs(x = "", y = "") +
scale_y_continuous(breaks = NULL,labels=dtm$month) +theme_bw()这一行引起问题
scale_x_discrete("Day of the Week",breaks = NULL,labels=c(-1,-.5,0,.5,1)) + 发布于 2017-09-27 18:39:08
当您使用coord_flip时,x轴变成y轴,反之亦然。我建议把它放在最后,这样就不会让人困惑了。另外,设置labs空白会在您用scale_x_discrete重写它时引起一些问题。
g1 <- ggplot(dtm, aes(month, value, label = month)) + geom_text(aes(label=value, hjust = ifelse(value >=0,-.1,1.2)), position = position_dodge(width = .9)) +
geom_bar(stat = "identity",aes(fill = colour))+
scale_y_continuous(breaks =seq(-1,1,.5), labels=seq(-1,1,.5),expand=c(.1,.1)) +
scale_x_discrete("Day of the Week")+
coord_flip() +
theme_bw()我在geom_text中使用了一个geom_text语句来根据值是正还是负来设置hjust。我还将expand=c(.1,.1)添加到scale_y_continuous中,以添加一些填充,这样标签就不会被掩盖。

https://stackoverflow.com/questions/46454791
复制相似问题