Lazar Šćekić
Lazar Šćekić

Reputation: 5

Changing the color bar font in R plots

I'm performing a plot of a raster in R, and I need all of the text and the number on a graph to have the same font and font size. The font should be Times New Roman (serif family) and its size should be 12. The code I'm using is:

GHI <- rasterFromXYZ(Solar_Potential)
cuts <- c(1300,1325,1350,1375,1400,1425,1450,1475,1500,1525,1550,1575,1600)
pal <- colorRampPalette(c("yellow","red"))
plot(GHI,breaks = cuts,col = pal(13),
xlab = "Longitude",ylab = "Latitude",family = "serif")
title("Global horizontal irradiation",family = "serif")

however, the color bar on the right stays on the default font, and the title is bold and has much larger letters in comparison with the rest of the graph. Could anyone help me with this?

Upvotes: 0

Views: 422

Answers (1)

user12728748
user12728748

Reputation: 8506

This might be difficult to achieve with the default plot function from raster, which does not appear to allow changes of the font family of the legend.

You could use ggplot2 to achieve that, using something like this (since you do not provide GHI you might have to adjust things):

library(ggplot2)
ggplot() +  
    geom_tile(data=as.data.frame(rasterToPoints(GHI)), aes(x=x, y=y, fill=layer)) +
    scale_fill_gradientn(colours=pal(13), breaks=seq(1300, 1600, length.out=13)) +
    theme_classic(base_size=12, base_family="serif") +
    theme(panel.border=element_rect(fill=NA, size=1),
          plot.title=element_text(hjust=0.5, size=12),
          axis.text=element_text(size=12)) +
    labs(x="Longitude", y="Latitude", title="Global horizontal irradiation")

Edit:

I was wrong; the font family can be passed globally, but the font size depends on the graphics device, I believe, hence with ggplot2 it might still be easier to get your target font size.

To get the same sizes with plot, you could try:

par(family="serif")
plot(GHI, breaks = cuts, col = pal(13),
     xlab = "Longitude", ylab = "Latitude", cex.lab=1)
title("Global horizontal irradiation", font.main = 1, cex.main=1)

Upvotes: 0

Related Questions