0

I have the following data df:

   type     operation  year    month    weekday
1  A1       AAAA       2011    11       Tue
2  A1       BBBB       2011    11       Mon
3  D3       AAAA       2012     1       Tue
4  A1       AAAA       2012     1       Thu
5  D3       DDDD       2012    11       Thu
6  D3       BBBB       2013     1       Wed

Then I need to group this data by type and operation and calculate the number of occurances of each combination. I am currently grouping df as follows:

grouped_data = aggregate(df,
                 list(type_names = df$type,
                      operation = df$operation),
                 FUN = function(x){NROW(x)})

Then I plot it using grouped barchart:

ggplot(grouped_data, aes(type_names,type)) +   
  geom_bar(aes(fill = operation), position = "dodge", stat="identity")+
  coord_flip()

My question is how to add additional labels to bars? In particular I store some additional information about each type in a separate data frame called add_data. Let's say this is the popularity index (pop_ind) of each type. How can I attach pop_ind values to each corresponding bar?

FiofanS
  • 309
  • 2
  • 6
  • 13
  • you would add `geom_text(data = add_data, aes(label = pop_ind))` assuming you as the same `type_names` value in `add_data` for aligning your x-values – Nate Oct 20 '16 at 15:26
  • @NathanDay: I tried this approach. The `add_data` contains two columns `type_names` and `pop_ind`. The only thing is that `add_data` contains more types than `grouped_data`. I get the error `Error in eval(expr, envir, enclos) : object 'type_names' not found`. I don't understand why it cannot find the object `type_names`... – FiofanS Oct 20 '16 at 15:53
  • Why don't you merge the 2 dataframes before plotting, at least for the labelling column ? – bVa Oct 20 '16 at 15:57
  • 1
    Could you provide us a dataset to suggest you solutions ? – bVa Oct 20 '16 at 16:01
  • that error is likely from you specifying `x = type_names` outside of `aes`, but it's impossible to telll/help until we see `dput(add_data)`. PS `dput()` is a better way to add example data to you questions as opposed to text tables – Nate Oct 20 '16 at 16:07
  • @FiofanS, If you have tried that approach you _need_ to include that in the question and explain what is going wrong. – Axeman Oct 20 '16 at 16:39

0 Answers0