annotate_geom_text problem

49 views
Skip to first unread message

Seyit Ali KAYIŞ

unread,
Feb 24, 2023, 6:06:48 AM2/24/23
to ggplot2
  Dear all, I have a data set (generated as below) and I make 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 seyital...@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 line "annotate(geom="text", x=xcoor3, y=ycoor3, label=letters3, size=10 ) +" it is ok. But I need that line   
Message has been deleted

Kaori Ito

unread,
Mar 3, 2023, 2:17:38 PM3/3/23
to ggplot2
I think using "i" is the problem; looks the last i = 3 is called when you "print" the previous plots (G1, G2).
To avoid this happening, one way is to create a long format data.

#change the data to long format
d <- MyData %>% pivot_longer(Var1:Var3, values_to="values", names_to = "Var")
#for annotation
d$name <- ifelse(d$Gr=="C" & d$Time==1, "a",
                 ifelse(d$Gr=="C" & d$Time==2, "b",
                        ifelse(d$Gr=="C" & d$Time==3, "c",
                               ifelse(d$Gr=="T" & d$Time==1, "A",
                                      ifelse(d$Gr=="T" & d$Time==2, "A",
                                             ifelse(d$Gr=="T" & d$Time==3, "B", NA))))))

#create non-duplicate name
tab <- d %>% group_by(Gr,Time, Var, name) %>% slice(1)

ggplot(data =d, mapping = aes(x = Time , y = values, fill=Gr)) +
  stat_boxplot(geom = "errorbar", width = 0.4, lwd=1, position = position_dodge(width = 0.75) ) +
  geom_boxplot(lwd=1) + facet_wrap(~Var) +
  geom_text(data=tab, aes(y=rep(c(40,40,40,38,38,38), each=3), label=name),
            position = position_dodge(width = 0.75)) +
  labs(title="Title", y="value (unit)") + My_Theme1 

Rplot.png
Reply all
Reply to author
Forward
0 new messages