1

似乎是这样的,只要我在 ggplot2 中调用 theme(),我以前的 labs() 相同的情节就会被删除/覆盖。我现在上网了 5 个小时,但我没有看到解决方案。有谁知道发生这种情况的原因?你会节省我的一周。

这是我的代码:

#creating variables and store them in new data frame

rel_pred <- fitted(rel)
rel_resid <- residuals(rel)
data1 <- data.frame(rel_resid, rel_pred)`

#plot the data

plot1 <- ggplot(data1, aes(x=rel_pred,y=rel_resid)) + 
geom_point() + 
geom_hline(yintercept=0, colour='red') #so far so good, everything works

plot1 + labs(y="Residuals", x="Fitted Values SF-12 MCS", caption="Figure  
1. Residuals vs. fitted Values Model 1") #when I run this, it perfectly 
adds labels

问题来了:只要我用其中的任何元素运行 theme() ,它就会使我以前的标签消失。

plot1 + theme(panel.background = element_rect(fill='transparent'),
plot.background = element_rect(fill='transparent'),
panel.border = element_blank(),
axis.line = element_line(colour="black", size=1),
axis.title.x = element_text(colour='black',size=6),
axis.title.y = element_text(colour='black', size=6),
plot.caption = element_text('Figure 1. Residuals vs. fitted Values Model   
1')
)
4

1 回答 1

2

plot1正如理查德所说,由于向原始情节添加标签,您忘记了“更新” 。

这里:

plot1 <- ggplot(data1, aes(x=rel_pred,y=rel_resid)) + 
geom_point() + 
geom_hline(yintercept=0, colour='red') #so far so good, everything works

plot1 + labs(y="Residuals", x="Fitted Values SF-12 MCS", caption="Figure  
1. Residuals vs. fitted Values Model 1") #when I run this, it perfectly 
adds labels

试试吧

plot1 <- ggplot(data1, aes(x=rel_pred,y=rel_resid)) + 
geom_point() + 
geom_hline(yintercept=0, colour='red') #so far so good, everything works

plot1 <-  plot1 + labs(y="Residuals", x="Fitted Values SF-12 MCS", caption="Figure  
1. Residuals vs. fitted Values Model 1") # no output now, it will save the result as plot1

进而

plot1 + theme(panel.background = element_rect(fill='transparent'),
plot.background = element_rect(fill='transparent'),
panel.border = element_blank(),
axis.line = element_line(colour="black", size=1),
axis.title.x = element_text(colour='black',size=6),
axis.title.y = element_text(colour='black', size=6),
plot.caption = element_text('Figure 1. Residuals vs. fitted Values Model   
1')
)

将按预期工作

于 2018-06-11T22:27:41.490 回答