R 如何在-ve和+条的另一侧标记条形图;用ggplot计算ve值?
我是否可以寻求您的帮助,以使用ggplot2标记条形图,如下图所示: 我使用以下代码获取所附的绘图:R 如何在-ve和+条的另一侧标记条形图;用ggplot计算ve值?,r,plot,ggplot2,R,Plot,Ggplot2,我是否可以寻求您的帮助,以使用ggplot2标记条形图,如下图所示: 我使用以下代码获取所附的绘图: library(ggplot2) test <- data.frame(x = c("Moderately Poor", "Deeply Poor", "Deeply & Intensely Poor", "Intensely Poor", "Overall Poverty"), y = c(0.024, -0.046, -0.025, -0.037, -0.083)) te
library(ggplot2)
test <- data.frame(x = c("Moderately Poor", "Deeply Poor", "Deeply & Intensely Poor", "Intensely Poor", "Overall Poverty"), y = c(0.024, -0.046, -0.025, -0.037, -0.083))
test$colour <- ifelse(test$y < 0, "firebrick1", "steelblue")
test$hjust <- ifelse(test$y > 0, 1.03, -0.03)
ggplot(test, aes(x, y, label = x, hjust = hjust)) +
geom_text(aes(y = 0, colour = colour)) +
geom_bar(stat = "identity", aes(fill = colour))
last_plot() + coord_flip() + labs(x = "", y = "") +
scale_x_discrete(breaks = NA) + theme_bw() +
opts(legend.position = "none")
库(ggplot2)
测试R图形,包括ggplot2
,是笔在纸上的,即层
(每个geom…
)将按顺序绘制
因此,如果您希望在geom_条
的顶部有一个geom_文本
,则geom_文本
需要位于geom_条
之后
更新ggplot2
0.9.3(当前版本)
产生
是否有帮助?或者@Arun是否可以在每个条上添加另一个反映值的标签?对不起,我不太明白。你是说每个条有两个文本标签?@Arun,是的。我指的是条上的第二个数字标签,如上面更新的图所示。
ggplot(test, aes(x, y)) +
geom_text(aes(y = 0, colour = colour, hjust =hjust, label = x), size=4.5) +
geom_bar(stat = "identity", aes(fill = colour)) +
geom_text(colour ='black',
aes(label = paste( formatC( round(y*100, 2 ), format='f', digits=2 ),'%'),
hjust = ifelse(y>0,1,0))) +
coord_flip() + labs(x = "", y = "") +
scale_x_discrete(breaks = NULL) + theme_bw() +
theme(legend.position = "none")