我有一个数据集(如下生成)并制作了 3 个图表 G1、G2、G3(代码如下)。创建图表后打印图表时没有问题。但是,如果我在创建所有图表后打印它们,只有最后一张图没问题,但前两张图有问题。
我正在尝试找出问题所在。非常感谢任何评论。
亲切的问候
Seyit Ali KAYIS [email protected]
代码
##############################################
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
如果我删除线
“注释(geom=”text”,x=xcoor1,y=ycoor1,label=letters1,size=10)+”“注释(geom=”text”,x=xcoor2,y=ycoor2,label=letters2,size= 10 ) +" "annotate(geom="text", x=xcoor3, y=ycoor3, label=letters3, size=10 ) +" 就可以了。但我需要那些台词
本质上,问题就出在三个
y = MyData[,i]
调用中调用aes
的时候。 ggplot
在您调用 print
之前不会查找数据,因此当它第三次查找时,所有三个图表都在读取 i = 3
并绘制相同的数据。将它们更改为y = Var1
等,您会看到不同之处。
解决方案是不使用全局变量。如果您正在寻找一种迭代一系列值以获取图形的简单方法,那么您可以使用映射(如果您不熟悉
lapply
部分,则可以使用 purrr
或类似方法)并创建所有图形,这里作为对象一起输出在列表中:
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)