简体   繁体   中英

Using dplyr and ggplot for multiple variables

I am using dplyr to subset my data and generate multiple graphs for one variable, but I would like to use the same subset for multiple variables. for the sample data reprex ,

reprex <- structure(list(daycode = c("a", "a", "a", "a", "a", "a", "b", 
"b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "c"), status = c("1", 
"2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", 
"1", "2", "1", "2"), IHT = c(20, 65, 31, 62, 19, 58, 27, 27, 
28, 28, 23, 25, 27, 26, 59, 34, 16, 18), TJ = c(19L, 141L, 80L, 
181L, 24L, 166L, 65L, 103L, 40L, 98L, 45L, 88L, 59L, 86L, 54L, 
98L, 21L, 74L)), class = "data.frame", row.names = c(NA, -18L
))

and using the code

d.plots <- reprex %>%
  group_by(status, daycode) %>%
  summarise(day.TJ = mean(TJ), sdtj = sd(TJ),
            day.IHT = mean(IHT), 
            sdIHT = sd(IHT)) %>%
  mutate(sdTJ.hi = day.TJ + sdtj, sdTJ.lo = day.TJ - sdtj, sdIHT.hi = day.IHT + sdIHT,
         sdIHT.lo = day.IHT - sdIHT) %>%
  do(plots = ggplot(data = .) +
       aes(x = daycode, y = day.TJ, group = 1) +
       geom_col(fill = "red")+
       geom_text(aes(y = day.TJ + 2, label = signif(day.TJ, 2))) +
       geom_line(aes(y = sdTJ.hi), size = 0.8) +
       geom_line(aes(y = sdTJ.lo), size = 0.8)+
       labs(x = "Day", y = "Avg Jumps" ) +
       theme_bw() +
       ggtitle(unique(.$status)))
d.plots$plots

I can get the two graphs for the variable TJ , but would like to get the same graphs for variable IHT as well.

在此处输入图像描述

在此处输入图像描述

I would pivot the dataframe to an even longer format, so you'll have the variable ( IHT or JT ) in a column and it can be used without repeating code.

I will do so and use your code to prepare the plots. I will also propose a better visualization of the data; of course I don't know what you are going to use these for, so be free to ignore it.

library(dplyr)
library(tidyr)
library(ggplot2)

d.plots<-reprex %>% 
  pivot_longer(cols = c('IHT', 'TJ')) %>%
  group_by(status, name, daycode) %>%
  summarise(day = mean(value),
            sd = sd(value)
  ) %>%
  mutate(sd.hi = day + sd, 
         sd.lo = day - sd) %>%
  do(plots = ggplot(data = .) +
       aes(x = daycode, y = day, group = 1) +
       geom_col(fill = "red")+
       geom_text(aes(y = day + 2, label = signif(day, 2))) +
       geom_line(aes(y = sd.hi), size = 0.8) +
       geom_line(aes(y = sd.lo), size = 0.8)+
       labs(x = "Day", y = "Avg Jumps" ) +
       theme_bw() +
       ggtitle(unique(paste(.$status, .$name))))
d.plots$plots
#> [[1]]

#> 
#> [[2]]

#> 
#> [[3]]

#> 
#> [[4]]

This is my proposed visualization, it swap bars with point and make use of the facet feature:

reprex %>% 
  pivot_longer(cols = c('IHT', 'TJ')) %>%
  group_by(status, name, daycode) %>%
  summarise(day = mean(value),
            sd = sd(value)
  ) %>%
  mutate(sd.hi = day + sd, 
         sd.lo = day - sd) %>% 
  ggplot(aes(x = daycode, y = day, group = 1)) +
  facet_grid(status ~ name) +
  geom_point(color = 'red2') +
  geom_text(aes(label = signif(day, 2)), nudge_y = 10) +
  geom_ribbon(aes(ymin = sd.lo, ymax = sd.hi), alpha = .2) +
  theme_bw() 

Created on 2020-06-16 by the reprex package (v0.3.0)

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