条形图上方的R geom_bar和facet_grid标签

时间:2022-05-22 14:57:35

I am trying to make this graph look better, and I'm stuck with the labels (the numbers in this case). How can I make them to show on the top of their correspondent bar? Notice it is a facet_grid. I have the following code and the output:

我试图使这个图表看起来更好,我坚持使用标签(在这种情况下的数字)。如何让它们显示在对应栏的顶部?注意它是facet_grid。我有以下代码和输出:

ggplot(articles_europaoccidental_sex_count_unique_group, aes(Country, percentage)) + geom_bar(stat = "identity", position = "dodge", aes(fill=Gender)) + 
    facet_grid(~Propuesta) + geom_text(aes(label = round(percentage, 2)), position = position_dodge(width = 0.9), vjust = -1)

条形图上方的R geom_bar和facet_grid标签 Thanks!

谢谢!

1 个解决方案

#1


2  

You are almost there, just you need to move aes(fill=Gender) to inside ggplot

你几乎就在那里,只需要将aes(fill = Gender)移到ggplot里面

library(tidyverse)
#Reproducible data set
test_mtcars <- mtcars %>% group_by(cyl,am, gear) %>% summarise(mean = mean(mpg))

ggplot(test_mtcars, aes(as.factor(cyl), mean, fill=as.factor(am))) + geom_bar(stat = "identity", position = "dodge") + 
facet_grid(~gear) + geom_text(aes(label = round(mean, 2)), position = position_dodge(width = 0.9), vjust = -1)

条形图上方的R geom_bar和facet_grid标签

#1


2  

You are almost there, just you need to move aes(fill=Gender) to inside ggplot

你几乎就在那里,只需要将aes(fill = Gender)移到ggplot里面

library(tidyverse)
#Reproducible data set
test_mtcars <- mtcars %>% group_by(cyl,am, gear) %>% summarise(mean = mean(mpg))

ggplot(test_mtcars, aes(as.factor(cyl), mean, fill=as.factor(am))) + geom_bar(stat = "identity", position = "dodge") + 
facet_grid(~gear) + geom_text(aes(label = round(mean, 2)), position = position_dodge(width = 0.9), vjust = -1)

条形图上方的R geom_bar和facet_grid标签