使用geom_text函数时的几个标签

问题描述

我正在使用以下代码,并且我希望每个条形都有一个标签来指示该条的值,而不是如下所示的几个标签。有人可以帮忙吗?

    ggplot(data = iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
      geom_bar(stat = "summary",show.legend = T,fun = mean) +
      ylab("Sepal Length") + xlab("Types of Species") +
      ggtitle('Sepal Length vs Species') +
      scale_fill_manual("legend",values = c(
                          "setosa" = "black","versicolor" = "orange","virginica" = "blue"
                        )) + geom_text(aes(label = Sepal.Length))

enter image description here

解决方法

您可以尝试以下方法:

ggplot(data = iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
  geom_bar(stat = "summary",show.legend = T,fun = mean) +
  ylab("Sepal Length") +
  xlab("Types of Species") +
  ggtitle('Sepal Length vs Species') +
  scale_fill_manual("legend",values = c(
                      "setosa" = "black","versicolor" = "orange","virginica" = "blue"
                    )) +
  geom_text(aes(label = round(..y..,2)),stat = "summary",vjust = -.5)

enter image description here