Giuseppe Petri
Giuseppe Petri

Reputation: 640

Why map function in purr is not working to create different plots with ggplot2?

I have the following data set:

library(ggplot2)
library(dplyr)

data.1 <-read.csv(text = "
                  location,crop,treat,rep,dap,response
                  Loc1,Specie1,Low,1,24,0.005540574
                  Loc1,Specie1,Low,1,70,0.104593523
                  Loc1,Specie1,Low,1,130,0.482193603
                  Loc1,Specie1,Low,2,24,0
                  Loc1,Specie1,Low,2,70,0.288444591
                  Loc1,Specie1,Low,2,130,0.531289679
                  Loc1,Specie1,High,1,24,0
                  Loc1,Specie1,High,1,72,0.614354225
                  Loc1,Specie1,High,1,130,0.640002419
                  Loc1,Specie1,High,2,24,0
                  Loc1,Specie1,High,2,72,0.765393308
                  Loc1,Specie1,High,2,130,0.642179092
                  Loc1,Specie2,Low,1,28,0.023203666
                  Loc1,Specie2,Low,1,67,0.334797936
                  Loc1,Specie2,Low,1,114,0.145432085
                  Loc1,Specie2,Low,2,28,0
                  Loc1,Specie2,Low,2,67,0.003371992
                  Loc1,Specie2,Low,2,114,0.029647931
                  Loc1,Specie2,High,1,27,0
                  Loc1,Specie2,High,1,69,0.041779323
                  Loc1,Specie2,High,1,114,0.260238095
                  Loc1,Specie2,High,2,27,0.01049002
                  Loc1,Specie2,High,2,69,0.773529033
                  Loc1,Specie2,High,2,114,0.139345238
                  Loc2,Specie1,Low,1,28,0.148713826
                  Loc2,Specie1,Low,1,76,0.879166667
                  Loc2,Specie1,Low,1,152,0.686124402
                  Loc2,Specie1,Low,2,28,0.245176849
                  Loc2,Specie1,Low,2,76,0.70625
                  Loc2,Specie1,Low,2,152,0.841148325
                  Loc2,Specie1,High,1,29,0.09495549
                  Loc2,Specie1,High,1,77,0.931372549
                  Loc2,Specie1,High,1,152,0.669856459
                  Loc2,Specie1,High,2,29,0.101879327
                  Loc2,Specie1,High,2,77,0.808823529
                  Loc2,Specie1,High,2,152,0.877511962
                  Loc2,Specie2,Low,1,28,0.524919614
                  Loc2,Specie2,Low,1,76,0.902083333
                  Loc2,Specie2,Low,1,118,0.134020619
                  Loc2,Specie2,Low,2,28,0.620578778
                  Loc2,Specie2,Low,2,76,0.311458333
                  Loc2,Specie2,Low,2,118,0.171391753
                  Loc2,Specie2,High,1,29,0.647378833
                  Loc2,Specie2,High,1,77,0
                  Loc2,Specie2,High,1,118,0.068943299
                  Loc2,Specie2,High,2,29,0.558358061
                  Loc2,Specie2,High,2,77,0
                  Loc2,Specie2,High,2,118,0.278350515")

I am able to create the following plot:

ggplot(data.1, group = rep) +
  geom_point(aes(x = dap, y=response, colour = as.factor(rep))) +
  geom_line(aes(x = dap, y=response, colour = as.factor(rep))) +
  geom_smooth(aes(x=dap, y=response), method = lm, color = "red") +
  theme_bw() + 
  theme(legend.position = "none",
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        strip.background=element_rect(colour="black", fill="white")) +
  facet_rep_grid(location~crop*treat,                 
                 scales= "free_x", 
                 repeat.tick.labels = FALSE)


enter image description here

However, want I want to do is to create two 4-panel (location x treat), one for each Specie, looping through crop. I want to do this using map function from library(purrr) because I have a large data set with several species. This is what I did:

library(purrr)
crop_list <- unique(data.1$crop)
crop_plot <- function(x ,y) {
  
ggplot(data=data.1, aes_string(x=x, y=y), group = rep) +
    geom_point(aes(colour = as.factor(rep))) +
    geom_line(aes(colour = as.factor(rep))) +
    geom_smooth(method = lm, color = "red") +
    theme_bw() + 
    theme(panel.grid.minor = element_blank(),
          panel.grid.major = element_blank(),
          strip.background=element_rect(colour="black", fill="white")) + 
    facet_rep_grid(location~treat,                 
                   scales= "free", 
                   repeat.tick.labels = FALSE) 
 
}

plots.preQC <- map(crop_list, ~ crop_plot("dap", "response"))

The problem is that the two plots are the same. It is like there is no different plots per crop level.

plots.preQC[[1]]

enter image description here

plots.preQC[[2]]

enter image description here

Any hint on why is this happening?

Upvotes: 0

Views: 104

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389047

You are getting the same result because you are passing the same parameters to the function crop_plot("dap", "response"). You may want to add variation by filtering data by crop.

library(tidyverse)
library(lemon)

crop_list <- unique(data.1$crop)

crop_plot <- function(x , y, value) {
  ggplot(data=data.1 %>% filter(crop == value), 
         aes(x=.data[[x]], y=.data[[y]]), group = rep) +
    geom_point(aes(colour = as.factor(rep))) +
    geom_line(aes(colour = as.factor(rep))) +
    geom_smooth(method = lm, color = "red") +
    theme_bw() + 
    theme(panel.grid.minor = element_blank(),
          panel.grid.major = element_blank(),
          strip.background=element_rect(colour="black", fill="white")) + 
    facet_rep_grid(location~treat,                 
                   scales= "free", 
                   repeat.tick.labels = FALSE) 
  
}


plot_list <- map(crop_list, ~ crop_plot("dap", "response", .x))

Now check plot_list[[1]] and plot_list[[2]].


By the way if dap and response are going to be constant you don't need to pass them as parameter as well. We can change the function to :

crop_plot <- function(value) {
  ggplot(data=data.1 %>% filter(crop == value), 
         aes(x=dap, y=response), group = rep) +
    geom_point(aes(colour = as.factor(rep))) +
    geom_line(aes(colour = as.factor(rep))) +
    geom_smooth(method = lm, color = "red") +
    theme_bw() + 
    theme(panel.grid.minor = element_blank(),
          panel.grid.major = element_blank(),
          strip.background=element_rect(colour="black", fill="white")) + 
    facet_rep_grid(location~treat,                 
                   scales= "free", 
                   repeat.tick.labels = FALSE) 
  
}

plot_list <- map(crop_list, crop_plot)

Upvotes: 1

Related Questions