Created
          February 4, 2025 02:08 
        
      - 
      
 - 
        
Save SachaEpskamp/9ed29da6bfdbd6e008d9a6422e6e3449 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | nSample <- 100 | |
| gender <- sample(c("male","female"),nSample,TRUE) | |
| coffee <- pmax(0, | |
| round(rnorm(nSample,mean = ifelse(gender=="male",3,2)))) | |
| heartrate <- 80 + 20 * coffee + rnorm(nSample, 0, 20) | |
| Data2 <- data.frame(gender=gender, coffee = coffee, | |
| heartrate = heartrate) | |
| head(Data2) | |
| library("ggplot2") | |
| ggplot(Data2, aes(x = coffee, y = heartrate)) | |
| ggplot(Data2, aes(x = coffee, y = heartrate)) + | |
| geom_point() | |
| ggplot(Data2, aes(x = coffee, y = heartrate, colour = gender)) + | |
| geom_point() | |
| ggplot(Data2, aes(x = heartrate, y = coffee, colour = gender)) + | |
| geom_point() | |
| ggplot(Data2, aes(x=gender, y=heartrate, colour=coffee)) + | |
| geom_point() | |
| ggplot(Data2, | |
| aes(x=gender, y=heartrate, colour=factor(coffee))) + | |
| geom_point() | |
| ggplot(Data2, | |
| aes(x=gender, y=heartrate, colour=factor(coffee))) + | |
| geom_boxplot() | |
| ggplot(Data2, | |
| aes(x = gender, y = heartrate, fill = factor(coffee))) + | |
| geom_boxplot() | |
| ggplot(Data2, | |
| aes(x = gender, y = heartrate, fill = factor(coffee))) + | |
| geom_boxplot() + xlab("Gender") + ylab("Heartrate") + | |
| scale_fill_discrete("Coffee") | |
| ggplot(Data2, | |
| aes(x = gender, y = heartrate, fill = factor(coffee))) + | |
| geom_boxplot() + xlab("Gender") + ylab("Heartrate") + | |
| scale_fill_discrete("Coffee") + theme_bw() | |
| ggplot(Data2, | |
| aes(x = factor(coffee), y = heartrate, fill = gender)) + | |
| geom_boxplot() + xlab("Coffee") + ylab("Heartrate") + | |
| scale_fill_discrete("Gender") + theme_bw() | |
| ggplot(Data2, | |
| aes(x = coffee, y = heartrate, colour = gender)) + | |
| geom_point() + xlab("Coffee") + ylab("Heartrate") + | |
| scale_fill_discrete("Gender") + theme_bw() + | |
| geom_smooth() | |
| ggplot(Data2, | |
| aes(x = coffee, y = heartrate, colour = gender)) + | |
| geom_point() + xlab("Coffee") + ylab("Heartrate") + | |
| scale_fill_discrete("Gender") + theme_bw() + | |
| geom_smooth(method = "lm") | |
| ggplot(Data2, | |
| aes(x = coffee, y = heartrate, colour = gender)) + | |
| geom_point() + xlab("Coffee") + ylab("Heartrate") + | |
| scale_fill_discrete("Gender") + theme_bw() + | |
| geom_smooth(method = "lm", se = FALSE) | |
| ggplot(Data2, | |
| aes(x = coffee, y = heartrate)) + | |
| geom_point() + xlab("Coffee") + ylab("Heartrate") + | |
| scale_fill_discrete("Gender") + theme_bw() + | |
| geom_smooth(method = "lm", se = FALSE) + | |
| facet_grid(gender ~ .) | |
| ggplot(Data2, aes(x = coffee, y = heartrate)) + | |
| geom_point() | |
| ggplot(Data2, aes(x = coffee, y = heartrate)) + | |
| geom_point() + theme_bw() | |
| ggplot(Data2, aes(x = coffee, y = heartrate)) + | |
| geom_point() + theme_classic() | |
| ggplot(Data2, aes(x = coffee, y = heartrate)) + geom_point() + | |
| theme( | |
| plot.background = element_rect(fill = "#E2E2E3", | |
| colour = "#E2E2E3"), | |
| panel.background = element_rect(fill = "#E2E2E3"), | |
| axis.text = element_text(colour = "#E7A922"), | |
| plot.title = element_text(colour = "#552683", face = "bold", | |
| size = 18, vjust = 1), | |
| axis.title = element_text(colour = "#552683", face = "bold", | |
| size = 13), | |
| panel.grid.major.x = element_line(colour = "#E7A922"), | |
| panel.grid.minor.x = element_blank(), | |
| panel.grid.major.y = element_blank(), | |
| panel.grid.minor.y = element_blank(), | |
| strip.text = element_text(colour = "white"), | |
| strip.background = element_rect(fill = "#E7A922"), | |
| axis.ticks = element_line(colour = "#E7A922") | |
| ) | |
| p <- ggplot(Data2, aes(x = coffee, y = heartrate)) + geom_point() + | |
| theme( | |
| panel.background = element_rect(fill='transparent'), | |
| plot.background = element_rect(fill='transparent', color=NA), | |
| legend.background = element_rect(fill='transparent'), | |
| legend.box.background = element_rect(fill='transparent') | |
| ) | |
| ggsave("plot_trans.png", p, width=10, height=6, dpi=300) | |
| p <- ggplot(Data2, aes(x = coffee, y = heartrate)) + geom_point() + | |
| theme_classic() + | |
| theme( | |
| panel.background = element_rect(fill='transparent'), | |
| plot.background = element_rect(fill='transparent', color=NA), | |
| legend.background = element_rect(fill='transparent'), | |
| legend.box.background = element_rect(fill='transparent') | |
| ) | |
| ggsave("plot_trans_2.png", p, width=4, height=2, dpi=300) | |
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # Long format data: | |
| df_long <- data.frame( | |
| day = c("mon", "mon", "mon", "mon", "tue", "tue", | |
| "tue", "tue", "wed", "wed", "wed", "wed"), | |
| subject = c(1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2), | |
| variable = c("anxious", "sad", "anxious", "sad", | |
| "anxious", "sad", "anxious", "sad", | |
| "anxious", "sad", "anxious", "sad"), | |
| value = c(1, 5, 1, 1, 2, 4, 2, 2, 1, 4, 1, 5) | |
| ) | |
| # Subject data: | |
| subject_data <- data.frame( | |
| id = c(1,2,3), | |
| name = c("Peter","Anne","Richard"), | |
| gender = c("male","female","male"), | |
| age = c(28,22,38) | |
| ) | |
| subject_data | |
| library("dplyr") | |
| df_long %>% left_join(subject_data, by = c("subject" = "id")) | |
| # Day data: | |
| day_data <- data.frame( | |
| day = c("mon","tue","thu","fri"), | |
| temp = c(25,24,28,29) | |
| ) | |
| day_data | |
| df_long %>% left_join(day_data, by = "day") | |
| df_long %>% inner_join(day_data, by = "day") | |
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | df_long <- data.frame( | |
| day = c("mon", "mon", "mon", "mon", "tue", "tue", | |
| "tue", "tue", "wed", "wed", "wed", "wed"), | |
| subject = c(1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2), | |
| variable = c("anxious", "sad", "anxious", "sad", | |
| "anxious", "sad", "anxious", "sad", | |
| "anxious", "sad", "anxious", "sad"), | |
| value = c(1, 5, 1, 1, 2, 4, 2, 2, 1, 4, 1, 5) | |
| ) | |
| print(df_long) | |
| library("ggplot2") | |
| ggplot(df_long, aes(x = day, y = value, colour = factor(subject))) + | |
| geom_point(size = 3) + | |
| facet_grid( ~ variable) + | |
| theme_bw() + | |
| scale_colour_discrete("Subject") + | |
| ylab("") + xlab("") | |
| library("dplyr") | |
| library("tidyr") | |
| df_wide <- df_long %>% pivot_wider( | |
| id_cols = c(day,subject), # The columns that will represent the rows in the new data | |
| names_from = variable, # Where to take the variable names from? | |
| values_from = value # Where to take the variable values from? | |
| ) | |
| print(df_wide) | |
| df_very_wide <- df_wide %>% | |
| # Make Anxious label shorter (for slide): | |
| select(day,subject,anx=anxious,sad) %>% | |
| # Make the data wider: | |
| pivot_wider( | |
| id_cols = subject, | |
| names_from = day, | |
| values_from = c(sad,anx)) | |
| print(df_very_wide) | |
| df_long %>% | |
| # Make Anxious label shorter (for slide): | |
| mutate(variable = substr(variable,1,3)) %>% | |
| # Make the data wider: | |
| pivot_wider( | |
| id_cols = subject, | |
| names_from = c(day,variable), | |
| values_from = value) | |
| df_very_wide %>% pivot_longer(sad_mon:anx_wed) | |
| df_very_wide %>% pivot_longer(sad_mon:anx_wed) %>% | |
| separate(name,c("variable","day")) | |
| df_very_wide %>% | |
| pivot_longer(sad_mon:anx_wed, | |
| names_to = c("variable","day"), | |
| names_pattern = "(.*)_(.*)" | |
| ) | |
| # First make a data for the sad variables: | |
| df_wide_sad <- df_very_wide %>% | |
| # Select only the sad varibles: | |
| select(subject,sad_mon:sad_wed) %>% | |
| # Make longer: | |
| pivot_longer(sad_mon:sad_wed, | |
| names_to = "day", | |
| values_to = "sad") %>% | |
| # Remove the first 4 letters from the day string: | |
| mutate(day = substr(day,5,7)) | |
| print(df_wide_sad) | |
| # Next the same for the anxious data: | |
| df_wide_anx <- df_very_wide %>% | |
| # Select only the sad varibles: | |
| select(subject,anx_mon:anx_wed) %>% | |
| # Make longer: | |
| pivot_longer(anx_mon:anx_wed, | |
| names_to = "day", | |
| values_to = "anxious") %>% | |
| # Remove the first 4 letters from the day string: | |
| mutate(day = substr(day,5,7)) | |
| print(df_wide_anx) | |
| df_wide_sad %>% left_join(df_wide_anx) | |
| library("reshape2") | |
| df_long %>% dcast(day + subject ~ variable) | |
| df_wide %>% melt(id.vars = c("subject","day"), | |
| measure.vars = c("anxious","sad")) | |
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment