Search code examples
rplotexportaxis-labels

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


I am trying to export a graph in R so that I can put it on a poster, but my y-axis labels are not clearly visible. They look like they're squished between the axis and the edges of the graphs. When I export the image as a PNG file, it does not look good. How do I go about adjusting the code so that it displays these labels?

If you run the code in this example, you should see the problem.

Also, why are my R^2 values not bold, as I specified??

Any help would be great, thanks everyone!

(Note: This is obviously fake data, made up for this example).

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()

Solution

  • I assume you want to keep the plot size and margin size the same.

    Set ylab to be blank in your plot:

    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)
    

    Then specify it manually, using the mgp parameter. The default is c(3,1,0).

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

    For your second question, you need to add bold() within the expression().

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