-4

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

Updated: This

E B
  • 1,073
  • 3
  • 23
  • 36
  • As the Total Count and Total Y are so different in scale, plotting side by side would result in Total Count almost nonexistent. DId you really meant side by side? – akrun Oct 02 '16 at 02:23
  • @akrun, i get what you mean but i would like to have a way to show atleast the count in the same graph that i am showing the total Y ..is there a way to do that or should i just show two graph separately – E B Oct 02 '16 at 03:13
  • http://stackoverflow.com/questions/3505701/r-grouping-functions-sapply-vs-lapply-vs-apply-vs-tapply-vs-by-vs-aggrega – jogo Oct 04 '16 at 08:05

1 Answers1

-1

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).

library(dplyr)
library(tidyr)
library(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")
akrun
  • 874,273
  • 37
  • 540
  • 662