1

我可以寻求您的帮助,用 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))

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")

我只是想知道如何在每个条上获得第二个数字标签?

谢谢,

4

1 回答 1

3

R 图形,包括ggplot2,是笔在纸上的,即layers(每个geom_...)将按顺序绘制。

所以,如果你想在 ageom_text之上有一个geom_bargeom_text将需要在geom_bar.

更新ggplot20.9.3(当前版本)

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")

生产

在此处输入图像描述

于 2013-03-01T03:31:58.477 回答