简体   繁体   English

在R中注释ggplot条形图

[英]Annotate ggplot bar plot in R

How can I annotate the following plot?如何注释以下情节? I want to include counts on top of each bar.我想在每个栏的顶部包含计数。

g <- ggplot(mpg, aes(class))
# Number of cars in each class:
g + geom_bar()

I only know how to do it if I do a group by and create a new column 'count' for example.例如,如果我进行分组并创建一个新列“计数”,我只知道该怎么做。

You can do:你可以做:

ggplot(mpg, aes(class)) + 
  geom_bar() +
  geom_text(stat = "count", aes(label = after_stat(count)), nudge_y = 1)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM