如何将第二组标签添加到第二组数据点

时间:2017-03-20 13:36:06

标签: r label geom-bar

我已经在条形图的条形图中添加了标签(2016年回复率),但又希望为每个条形图中的单个/黑色数据点(2015响应率)添加标签。

enter image description here

这是我的代码:

ggplot(merged2[merged2$TrustCode %in% acuteCodes, ], aes(x = TrustName, y = ResponseRate16)) + 
  geom_bar(fill="white",stat="identity", colour="blue") +
  geom_point(aes(x = TrustName, y = ResponseRate15), shape=18, size=3, colour="black") +
  geom_text(aes(label=ResponseRate16, x=TrustName, y=1.10*ResponseRate16), colour="black")

如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

与映射条形图值完全相同:添加geom_text()

ggplot(merged2[merged2$TrustCode %in% acuteCodes, ], aes(x = TrustName, y = ResponseRate16)) + 
  geom_bar(fill = "white", stat = "identity", colour = "blue") +
  geom_point(aes(y = ResponseRate15), shape = 18, size = 3, colour = "black") +
  geom_text(aes(label = ResponseRate16, y = 1.10*ResponseRate16), colour="black") +
  geom_text(aes(label = ResponseRate15, y = 1.10*ResponseRate15), colour="red")

我稍微清理了重复x映射的代码。如果aesthetic已经映射到初始ggplot函数中,则无需复制nudge_y

您可能还想使用y arg而不是设置新的ggplot(merged2[merged2$TrustCode %in% acuteCodes, ], aes(x = TrustName, y = ResponseRate16)) + geom_bar(fill = "white", stat = "identity", colour = "blue") + geom_text(aes(label = ResponseRate16), nudge_y = .5, colour="black") + geom_point(aes(y = ResponseRate15), shape = 18, size = 3, colour = "black") + geom_text(aes(label = ResponseRate15, y = ResponseRate15), nudge_y = .5, colour="red") 美学。

{{1}}