简体   繁体   English

如何使用 ggplot 和 geom_bar() 在 r 的条形 plot 上添加 y 轴值?

[英]How to add y axis values on the bar plot in r using ggplot and geom_bar()?

Hi I have been trying to summarize my data set by sum of a particular column and using ggplot and geom_bar() directly to do so instead of using tidyvers and dplyr libraries.嗨,我一直在尝试通过特定列的总和来总结我的数据集,并直接使用 ggplot 和 geom_bar() 来这样做,而不是使用 tidyvers 和 dplyr 库。

I read about weight aesthetic and that it can help plotting bar graph as sum of a particular feature instead of count and it worked.我读到了重量美学,它可以帮助将条形图绘制为特定特征的总和而不是计数,并且它起作用了。

Problem is I am unable to plot the y value on top of the bars as I do not know how should I write geom_text(????) code:问题是我无法 plot 条顶部的 y 值,因为我不知道应该如何编写 geom_text(????) 代码:

p1<-ggplot(df, aes(x= ageBracket, fill= movement, **weight= amount**))+ geom_bar(width= 0.5, position= 'dodge')+ theme_hc()+scale_x_discrete('Age Group', limits=c("16 to 25 years", "26 to 35 years", "36 to 45 years", "> 46 years"))+ theme(legend.position= "bottom",legend.background = element_rect(fill="lightblue", size=0.2, linetype="solid"))+ scale_fill_brewer(palette = "Dark2")+ scale_y_continuous("Amount per Age Group",labels = comma)+ **geom_text(?????)**
print(p1)

Blockquote块引用

Here fill has helped me to segregate the bars that I have dodged, weight has helped me to sum data based on the filters of: movement & ageBracket, I just want the y axis values on top of the respective bar and not want to create a separate data frame.这里的填充帮助我分离了我躲过的条,重量帮助我根据以下过滤器对数据求和:运动和年龄括号,我只希望 y 轴值位于相应条的顶部而不想创建一个单独的数据框。 What should I do??我应该怎么办??

The current version of ggplot2 doesn't allow labels in geom_text() when the weight aesthetic is used.当前版本的 ggplot2 在使用重量美学时不允许在 geom_text() 中使用标签。 You'll have to summarise the data frame and then do it the usual way using the label aesthetic.您必须总结数据框,然后使用 label 美学以通常的方式进行。

Actually, there's not that much extra work required as shown using the mtcars dataset.实际上,如使用 mtcars 数据集所示,不需要那么多额外的工作。 Only two extra lines required, and you don't need to create a new data frame if you pipe the results forward.只需要两行额外的行,如果您将结果向前推进,则不需要创建新的数据框。

library(ggplot2)
library(dplyr)
data(mtcars)

(1) Using weight aesthetic to sum the mpg variable by am and cyl . (1) 使用权重美学通过amcylmpg变量求和。 Cannot add labels to bars (6 lines).无法向条形添加标签(6 行)。

mtcars %>%
  mutate(am=factor(am, labels=c("auto","manual")), cyl=factor(cyl)) %>%
  ggplot(aes(x=cyl, fill=am, weight=mpg)) + 
  geom_bar(width=0.5, position='dodge') +
  labs(y="Total miles/gallon", x="Cylinders", fill="Transmission") +
  theme_minimal()

(2) Summarising data first, then using label aesthetic with geom_text, can add labels to bars (2 extra lines). (2)先汇总数据,再用label审美和geom_text,可以给条添加标签(多出2行)。

mtcars %>%
  mutate(am=factor(am, labels=c("auto","manual")), cyl=factor(cyl)) %>%
  group_by(cyl, am) %>%  # extra
  summarise(n = sum(mpg)) %>% # extra
  ggplot(aes(x=cyl, fill=am, y=n, label=n)) + 
  geom_col(width=0.5, position='dodge') +
  geom_text(position=position_dodge(0.5), vjust=-0.25) + 
  labs(y="Total miles/gallon", x="Cylinders", fill="Transmission") +
  theme_minimal()

在此处输入图像描述

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

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