将值和字符放在条形图中

时间:2014-02-25 12:55:07

标签: r ggplot2

我有一个数据集:

    >x1
      col1 col2 col3
   1    0    8  100
   2  0.1    5  200
   3  0.2    3  300
   4  0.3    1  400

我生成条形图:

ggplot(x1[,1:2],aes(x=col1, y=col2)) + 
 geom_bar(stat="identity") +
 ggtitle("plot1")

生成的图形如下所示

enter image description here

目前,每个酒吧都很坚固。但是,我想将其设为透明,并将第三列中的值叠加到每个相应的条上。在下文中,我将展示我希望看到的草案。但是如何用ggplot做到这一点?

enter image description here

1 个答案:

答案 0 :(得分:4)

这是一个解决方案:

ggplot(x1, aes(x = col1, y = col2)) +
  geom_bar(stat = "identity", fill = "transparent", colour = "black")+
  ggtitle("plot1") +
  geom_text(aes(label = paste("$", col3)), vjust = 2)

使用参数fill删除条形的fill = "transparent"颜色。使用colour = "black"创建黑色边框。

函数geom_text用于向绘图添加文本。在这里,vjust可用于修改标签的垂直位置。

enter image description here