3

Can someone tell me how I integrate the following dplyr code with a ggplot bar chart. I have them both working indipendently but whenever I try to put it together something goes wrong.

Here is the dplyr code which produces a summary table (inserted below script):

pop.2.df %>%
gather(key = "trial", value = 'capture','sco.1','sco.2', 'sco.3') %>% 
group_by(trial, capture) %>% summarise(n = n()) 

A tibble: 6 x 3
Groups:   trial [?] 
trial capture     n
  <chr>   <chr> <int>
1 sco.1       n    28
2 sco.1       y    94
3 sco.2       n    38
4 sco.2       y    84
5 sco.3       n    45
6 sco.3       y    77

Here is the ggplot code which produces the barchart, however I can only get it to work if I first make an object from the script above then plot. I would like to have it all working within one piece of code using pipeline operator:

 ggplot(data = pop.2.df.v2) + geom_bar(mapping = aes(x = trial, fill = capture), position = "dodge")

plot1

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Dasr
  • 777
  • 6
  • 16

1 Answers1

1

You are using the wrong geom: geom_bar needs a count statistic. Use geom_col instead

your summarised data %>% ggplot() +geom_col(...)
tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Thanks. I don't really get why it shouldn't work with geom_bar as after the summarise the stat should be count, should it not? Anyway, I got it to work as wanted with following: `pop.2.df %>% gather(key = "trial", value = 'capture', 'sco.1','sco.2', 'sco.3') %>% group_by(trial, capture) %>% summarise(count= n()) %>% ggplot() + geom_col(mapping = aes(x = trial, y = count, fill=capture), position = "dodge")` – Dasr Feb 19 '18 at 12:12
  • nope, the geom_bar needs something to count on (present in your orginal data frame), not the already counted values. geom_col uses the given values – tjebo Feb 19 '18 at 12:18
  • 1
    You could use `geom_bar` with `stat = "identity"`, which basically is `geom_col` – kath Feb 19 '18 at 13:20
  • Just in case anyone is still looking at this question. I have found that you can easily pass a data frame through a pipeline to any argument location (not just first as I initially thought). This is very useful in cases where you want to do a lot of manipulation prior to passing on the data frame. See this for an example: https://stackoverflow.com/questions/43881601/r-using-piping-to-pass-a-single-argument-to-multiple-locations-in-a-function – Dasr May 22 '18 at 14:54
  • @Dasr piping works with many objects, not only data frames. see this one here, really nice intro to piping : https://stackoverflow.com/questions/42385010/using-the-pipe-and-dot-notation – tjebo May 22 '18 at 16:11