0

I have a data set (generated as below) and made 3 graphs G1, G2, G3 (codes are below). When I print a graph after creating it there is no problem. However, If I print them after all of them created, only the last graphs is ok but the first 2 have problem.

I am trying to find what is going wrong. Any comment is deeply appreciated.

Kind Regards

Seyit Ali KAYIS seyitali.kayis@ibu.edu.tr

codes

#################################################

library(tidyverse)

set.seed <-10

Var1 <- rnorm (90, 25, 3)
Var2 <- rnorm (90, 15, 3)
Var3 <- rnorm (90, 5, 1)
Gr   <- c(rep("C", 15), rep("T", 15), rep("C", 15), rep("T", 15), rep("C", 15), rep("T", 15) )  
Time <- c(rep(1, 30), rep(2, 30), rep(3, 30) )   

MyData <- data.frame(Var1, Var2, Var3, Gr, Time)

MyData <- within(MyData, {
   Gr <- factor( Gr ) 
   Time <- factor( Time ) 
    
 }
  )

str(MyData)

name2 <- names(MyData)  


####################     Graphs    ##################################

 Tsize <- 30

 My_Theme1 = theme_classic()+
 theme(
 panel.border=element_rect(color = "black", fill=NA, size=2),
 axis.line=element_line(size=0.5, color="black"),
 axis.ticks=element_line(size=1.5, color="black"),
 axis.title.x = element_text(size = Tsize, color = "black"),
 axis.text.x = element_text(size = Tsize, color = "black"), 
 axis.title.y = element_text(size = Tsize, color = "black"),
 axis.text.y = element_text(size = Tsize, color = "black"),
 plot.title = element_text(size = Tsize, hjust=0.5, color = "black"),
legend.title =  element_text(size = Tsize, color = "black"),
legend.text =   element_text(size = Tsize, color = "black"),
strip.background = element_rect(colour= "black", fill=NA), 
panel.grid.major = element_line(colour = "white")   ) 


################    Graph Var1  ##########################################
i<- 1

xcoor1 = c(0.8, 1.8, 2.8, 1.2, 2.2, 3.2)
ycoor1 =  c(40, 40, 40, 37, 37, 37)
letters1 = c("a", "b", "c", "A", "A", "B")

G1 <-  ggplot(data = MyData, mapping = aes(x = Time  , y = MyData[,i], fill=Gr)) +  
      stat_boxplot(geom = "errorbar", width = 0.4, lwd=1.5, position = position_dodge(width = 0.75) ) +  
      geom_boxplot(lwd=1.5) +    
      ggtitle("A" ) +  xlab("Time")  +   ylab(paste(name2[i])) +  
      annotate(geom="text", x=xcoor1, y=ycoor1, label=letters1, size=10 ) + 
      My_Theme1
      
print(G1) # No problem
     
################    Graph Var2  ##########################################
     
 i<- 2

xcoor2 = c(0.8, 1.8, 2.8, 1.2, 2.2, 3.2)
ycoor2 =  c(28, 28, 28, 25, 25, 25)
letters2 = c("a", "b", "c", "A", "A", "B")

G2 <-  ggplot(data = MyData, mapping = aes(x = Time  , y = MyData[,i], fill=Gr)) +  
     stat_boxplot(geom = "errorbar", width = 0.4, lwd=1.5, position = position_dodge(width = 0.75) ) +  
     geom_boxplot(lwd=1.5) +    
      ggtitle("B" ) +  xlab("Time")  +   ylab(paste(name2[i])) +  
      annotate(geom="text", x=xcoor2, y=ycoor2, label=letters2, size=10 ) + 
      My_Theme1

print(G2) # No problem
      
################    Graph Var3  ##########################################
   
 i<- 3

xcoor3 = c(0.8, 1.8, 2.8, 1.2, 2.2, 3.2)
ycoor3 =  c(10, 10, 10, 8.5, 8.5, 8.5)
letters3 = c("a", "b", "c", "A", "A", "B")

G3 <-  ggplot(data = MyData, mapping = aes(x = Time  , y = MyData[,i], fill=Gr)) +  
      stat_boxplot(geom = "errorbar", width = 0.4, lwd=1.5, position = position_dodge(width = 0.75) ) +  
      geom_boxplot(lwd=1.5) +    
      ggtitle("C" ) +  xlab("Time")  +   ylab(paste(name2[i])) +  
      annotate(geom="text", x=xcoor3, y=ycoor3, label=letters3, size=10 ) + 
      My_Theme1

print(G3) # No problem         
        
print(G1) # PROBLEM: GRAPH CHANGED
print(G2) # PROBLEM: GRAPH CHANGED
print(G3) # No problem

If I remove the lines
"annotate(geom="text", x=xcoor1, y=ycoor1, label=letters1, size=10 ) +" "annotate(geom="text", x=xcoor2, y=ycoor2, label=letters2, size=10 ) +" "annotate(geom="text", x=xcoor3, y=ycoor3, label=letters3, size=10 ) +" it is ok. But I need that lines

1 Answers1

0

Essentially, the problem comes when calling y = MyData[,i] in the three aes calls. ggplot doesn't look up the data until you call print, so when it looks up the third time, all three graphs are reading i = 3 and plotting the same data. Change them to y = Var1 etc. and you'll see the difference.

A solution is not to use global variables. If you're looking for a simple way of iterating over a series of values to get graphs, then you can use mapping (or lapply or similar if you're unfamiliar with the purrr parts) and create all your graphs, here outputting as objects together in a list:

xcoor1 = c(0.8, 1.8, 2.8, 1.2, 2.2, 3.2)
ycoor1 =  c(40, 40, 40, 37, 37, 37)
letters1 = c("a", "b", "c", "A", "A", "B")

xcoor2 = c(0.8, 1.8, 2.8, 1.2, 2.2, 3.2)
ycoor2 =  c(28, 28, 28, 25, 25, 25)
letters2 = c("a", "b", "c", "A", "A", "B")

xcoor3 = c(0.8, 1.8, 2.8, 1.2, 2.2, 3.2)
ycoor3 =  c(10, 10, 10, 8.5, 8.5, 8.5)
letters3 = c("a", "b", "c", "A", "A", "B")

Gs <- list(
  i = 1:3,
  xcoor = list(xcoor1, xcoor2, xcoor3),
  ycoor = list(ycoor1, ycoor2, ycoor3),
  letters = list(letters1, letters2, letters3)
) |>
  pmap(function(i, xcoor, ycoor, letters) {

    # This is your ggplot code you've used above, but this time
    # it loops over each combo of data+coords+labels

    ggplot(data = MyData,
           mapping = aes(x = Time  , y = MyData[, i], fill = Gr)) +
      stat_boxplot(
        geom = "errorbar",
        width = 0.4,
        lwd = 1.5,
        position = position_dodge(width = 0.75)
      ) +
      geom_boxplot(lwd = 1.5) +
      ggtitle("C") +  xlab("Time")  +   ylab(paste(name2[i])) +
      annotate(
        geom = "text",
        x = xcoor,
        y = ycoor,
        label = letters,
        size = 10
      ) +
      My_Theme1
  })

print(Gs)

Andy Baxter
  • 5,833
  • 1
  • 8
  • 22