Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr/ggplot using pipeline

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

like image 420
Dasr Avatar asked Jun 11 '26 02:06

Dasr


1 Answers

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

your summarised data %>% ggplot() +geom_col(...)
like image 158
tjebo Avatar answered Jun 13 '26 19:06

tjebo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!