E B E B - 1 year ago 94
R Question

in R how do you graph frequency and distribution for occurrence of value in a column

I have a dataframe that has indicators as values of a column

X Y Ind
1 10000 N
2 10000 N
3 10000 G
4 10000 L

I want to create a bargraph using ggplot that will give me the Total Count and Total Y based on Indicator Value side by side .
I am trying to figure out how to implement an aggregation on the dataframe without summarizing it and creating a Count value per categorical value of Ind

Answer Source

One option would be to get count (n()) and sum of 'Y' after grouping by 'Ind', gather (from tidyr to reshape it to 'long' format) and get the barplot with geom_bar (from ggplot2).

df1 %>%
  group_by(Ind) %>% 
  summarise(Count=n(), TotalY = sum(Y)) %>% 
  gather(Var, Val, -Ind) %>% 
  ggplot(., aes(x=Ind, y = Val, fill=Var)) +
            geom_bar(stat="identity", position="dodge")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download