调整 R 注释中的 y 轴,使其可见(使用 'plot' 命令)

Adjusting y-axis in R annotations so that they are visible (using the 'plot' command)

我正在尝试在 R 中导出图表以便将其放在海报上,但我的 y 轴标签不清晰可见。它们看起来像是在轴和图表的边缘之间被压扁了。当我将图像导出为 PNG 文件时,它看起来不太好。我该如何调整代码以显示这些标签?

如果您 运行 此示例中的代码,您应该会看到问题。

此外,为什么我的 R^2 值不是我指定的粗体?

如有帮助将不胜感激,谢谢大家!

(注:这明显是假数据,为了这个例子编的)

data1 <- data.frame(height = c(1,2,3,4,5,6,7,8,9), camera = c(1,2,3,4,5,6,7,8,9))
data2 <- data.frame(height = c(1,2,3,4,5,6,7,8,9), camera = c(1,2,3,4,5,6,7,8,9))
data3 <- data.frame(height = c(1,2,3,4,5,6,7,8,9), camera = c(1,2,3,4,5,6,7,8,9))
data4 <- data.frame(height = c(1,2,3,4,5,6,7,8,9), camera = c(1,2,3,4,5,6,7,8,9))
data5 <- data.frame(height = c(1,2,3,4,5,6,7,8,9), camera = c(1,2,3,4,5,6,7,8,9))

###############PNG Settings#############
png("example.png", width=1000, height=700)

##############dataset1#####################
par(mfrow=c(2,3))

plot(data1$height, 
     data1$camera, main = "9 DAT",
     xlab = "GT Height (m)", ylab = "DTM Height (m)", las=1, pch=19,
     cex=2, cex.main=2, cex.lab=2, cex.axis=2)



abline(lm(data1$camera~data1$height), col='red', lwd = 3)

mod1<- lm(data1$camera~data1$height)
summary(mod1)

text(4, 8, label = "y = x", font=2, cex = 2)
text(4, 7, font=2, label = expression("R"^"2"* "= 1"), cex=2)


###########dataset2#################

plot(data2$height, data2$camera, main = "15 DAT", xlab = "GT Height (m)",
     ylab = "DTM Height (m)", las = 1, pch = 19,  cex=2, cex.main=2, cex.lab=2, cex.axis=2)

mod2 <- lm(data2$camera ~ data2$height)
summary(mod2)
abline(mod2, col='red', lwd=3)

text(x=4, y=8, labels = "y = x", font=2, cex=2)
text(x=4, y=7, font=2, labels = expression('R'^'2'* '= 1'), cex=2)

#################dataset3##############################


plot(data3$height, data3$camera, main = "21 DAT", xlab = "GT Height (m)",
     ylab = "DTM Height (m)", las = 1, pch = 19,  cex=2, cex.main=2, cex.lab=2, cex.axis=2)

mod3 <- lm(data3$camera ~ data3$height)

summary(mod3)
abline(mod3, col='red', lwd=3)

text(x=4, y=8, labels = "y = x", font=2, cex=2)
text(x=4, y=7, font=2,labels = expression('R'^'2'* '= 1'), cex=2)

############dataset4##################


plot(data4$height, data4$camera, main = "27 DAT", xlab = "GT Height (m)",
     ylab = "DTM Height (m)", las = 1, pch = 19,  cex=2, cex.main=2, cex.lab=2, cex.axis=2)

mod <- lm(data4$camera ~ data4$height)

summary(mod)

abline(mod, col='red', lwd=3)

text(x=4, y=8, labels = "y = x", font = 2, cex=2)
text(x=4, y=7, labels = expression('R'^'2'* '=1'), font = 2, cex=2)

####################dataset5#######################

plot(data5$height, data5$camera, main = "35 DAT", xlab = "GT Height (m)",
     ylab = "DTM Height (m)", las = 1, pch = 19,  cex=2, cex.main=2, cex.lab=2, cex.axis=2)

mod <- lm(data5$camera ~ data5$height)

summary(mod)
abline(mod, col='red', lwd=3)

text(x=4, y=8, labels = "y = x", font = 2, cex=2)
text(x=4, y=7, labels = expression('R'^'2'* '=1'), font = 2, cex=2)

############End of Image###############
dev.off()

我假设您希望绘图大小和边距大小保持相同。

在您的 plot 中将 ylab 设置为空白:

plot(data1$height, 
     data1$camera, main = "9 DAT",
     xlab = "GT Height (m)", ylab = '', las=1, pch=19,
     cex=2, cex.main=2, cex.lab=2, cex.axis=2)

然后使用mgp参数手动指定。默认值为 c(3,1,0)

title(ylab = "DTM Height (m)", mgp=c(2.3,1,0), cex.lab=2)

对于你的第二个问题,你需要在expression()中添加bold()

text(x=4, y=7, font=2,labels = expression(bold('R'^'2'* '= 1')), cex=2)