JohnD
JohnD

Reputation: 121

ggplot2 bar plot by two groups and mean of y variable

I'm trying to create a bar plot for which I have two groups and the y variable is the mean of one of those groups.

Sample Bar Graph

enter image description here

So looking at the above bar graph in the photo, I have bars grouped by country and prosocial, and on the y-axis I have taken the fraction of prosocial individuals. I am only able, however, to create a bar plot that only takes the mean of prosocial and groups it by country. Basically, it's just one bar per county. Which is not exactly what I'm looking for. So far this is the code I've been using to group the data for the bar plot, which has been somewhat unsuccessful.

plotData <- myData2[!is.na(myData2$prosocial),]
plotData <- plotData %>% 
  mutate(mean_prosocial = mean(prosocial)) %>% 
  group_by(country) %>%
  summarise(mean_prosocial = mean(prosocial),se = sd(prosocial) / sqrt(n()))

This only groups by country and if I want to group by prosocial as well, I obviously just get NAs for the mean variable. Below is a link to the working data: workable data.

Thanks.

Upvotes: 2

Views: 1819

Answers (1)

Aleksandr
Aleksandr

Reputation: 1914

Say you want to find the fraction of prosocial/non-prosocial across countries:

require(dplyr)
require(ggplot2)

First find how many observations in each country. Later it will be used in fraction calculation.

count_country <- myData2 %>% 
        filter(!is.na(prosocial)) %>% 
        group_by(country) %>% 
        summarise(n = length(country)) %>% 
        ungroup

Next find the number of prosocial/non-prosocial count across countries.

count_prosocial <- myData2 %>% 
        filter(!is.na(prosocial)) %>% 
        group_by(country, prosocial) %>% 
        summarise(n = length(prosocial)) %>% 
        mutate(prosocial = as.factor(prosocial))

Merge two dataframes by country name and find the fractions:

df <- count_prosocial %>% 
        left_join(count_country, by = "country") %>% 
        mutate(frac = round(n.x / n.y, 2))

Display fractions across different countries using facet_wrap:

ggplot(data=df, aes(x=prosocial, y=frac, fill=prosocial)) +
  geom_bar(stat = "identity")+
  geom_text(aes(x=prosocial, y=frac, label = frac),
            position = position_dodge(width = 1),
            vjust = 2, size = 3, color = "white", fontface = "bold")+
  facet_wrap(~country)+
  labs(y =  "Fraction of prosocial/non-prosocial") +
  scale_fill_discrete(labels=c("Prosocial", "Individualist"))+
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())

enter image description here

Upvotes: 3

Related Questions