简体   繁体   中英

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

情节1

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

your summarised data %>% ggplot() +geom_col(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM