当x值为字符

时间:2016-09-29 16:33:28

标签: r ggplot2

我想诠释' ggplot2条形图右上角的文本,其中包含x轴的字符和y轴的数字。我看到的所有文档都是,要注释文本,x和y坐标都必须给出数值。

以下是一个示例图表: -

这是数据框

df1 <- data.frame( p=c("a","b","c","a","b","c"),
                   v=c(10,9,8,6,5,2),
                   u=c("aa","bb","cc","aa","bb","cc")
                  )

汇总数据框

df2 <- df1 %>% select(p, v) %>% group_by(p) %>% summarise_each(funs(sum))

条形图

p <-    ggplot(data = df2, aes(p, v, label = v)) +
        geom_bar(stat = "identity", position = "dodge") +
        geom_text(position = position_dodge(.9),  vjust = -1, fontface = "bold", size = 5)

p

1 个答案:

答案 0 :(得分:1)

您应该可以将位置放在aes()内。这对我有用(除非我误解你的意图):

ggplot(data = df2, aes(p, v, label = v)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(position = position_dodge(.9),  vjust = -1, fontface = "bold", size = 5) +
  geom_text(aes(x = "c", y = 15, label = "Here I am"))

enter image description here