I have an R script that where I produce multiple (4 rows and 4 columns) histograms with density lines per page in a pdf output file using R Base Graphics. The stripped down (to avoid clutter and resulting distraction) version of this program is shown below:
pdf("multiple.pdf", width = 17, height = 11)
par(mfrow = c(4,4),mar=c(1.5,1.5,1.5,1.5),oma=c(4,5,7,3))
# Where should I put the Plot's title and Legend???
for (hra_color in df_hra_colors$hra) {
l <- density(...)
p <- hist(...)
# plot the first five tests for starters
for (current_test_index in 1:5) {
...
for (x in test_vector) {
points(...)
}
}
}
dev.off()
Trouble is: I cannot display the plot title, legend and the composite left and right axes labels using mtext. If I try to put the title, legend, and labels after exiting the outermost 'for' loop, it only shows up in the last histogram and does not position properly.
If I put it where I ask 'where should I put the Plot's title and legend??? as I show in the source code below, I get the following message:
Error in title(title, line = -0.4, outer = TRUE) : plot.new has not been called yet
I just tried to put the following line of code where I have the commented question:
title(title,line=-0.4,outer=TRUE)
Any idea how to get around this problem? There are a quite a few histograms that the program needs to show such that they run onto the next page. This works fine, but the title, legend, and the Axes labels are a problem. Based on the feedback, here is full script of my code which is quite small really:
setwd("~/sampling_ricardo")
df <- read.csv("main.csv")
df <- df[df$hra != -9999, ]
# read in the hra colors file
df_hra_colors <- read.csv('lookup.csv')
df_hra_colors$hex <- rgb(df_hra_colors$red, df_hra_colors$green, df_hra_colors$blue,
maxColorValue = 255, alpha = 200)
df_hra_colors <- df_hra_colors[df_hra_colors$hra %in% df$hra, c(1,5)] # 1 = hra, 5 = hex
df$hra <- factor(df$hra, levels = df_hra_colors$hra)
reset <- function() {
par(mfrow=c(1, 1), oma=rep(2, 4), mar=rep(0, 4), new=TRUE)
plot(0:1, 0:1, type="n", xlab="", ylab="", axes=FALSE)
}
pdf("multiple.pdf", width = 17, height = 11)
reset()
legend(x="top",legend = c("MRP","SSK","TXC","BZT","FT"),pch = c(15,7,6,16,17),cex=0.8,horiz = TRUE,box.col=NA,y.intersp = 0.7,inset = 0)
title(title,line=-0.4,outer=TRUE)
mtext("HRA PCA 1",side = 1,outer = TRUE,line = 0,font = 2)
mtext("FREQUENCY",side = 2,outer = TRUE,line = 0,font = 2)
par(mfrow = c(4,4),mar=c(1.5,1.5,1.5,1.5),oma=c(4,5,7,3))
current_component <- 'pc1'
title <- current_component
df_columns <- colnames(df)
# loop thru each hra color in the hra_colors dataframe and create one histogram for each color
for (hra_color in df_hra_colors$hra) {
df_current_color <- df[df$hra == hra_color, ]
if (nrow(df_current_color) == 1) {
next
}
l <- density(df_current_color[ , current_component])
p <- hist(df_current_color[ , current_component],
main = hra_color,
col.main = df_hra_colors$hex[df_hra_colors$hra == hra_color],
xlab = NA, ylab = NA,
ylim = c(0, max(l$y)), border = "black",
col = df_hra_colors$hex[df_hra_colors$hra == hra_color],
las=1,
freq = FALSE)
lines(l, lwd = 2)
size.shape <- 1.3
n <- 0.1
pch_vector <- c(15, 7, 6, 16, 17)
# plot the first five tests for starters
for (current_test_index in 1:5) {
test_name <- df_columns[5 + current_test_index]
test_vector <- df_current_color[df_current_color[test_name] == 1, current_component]
for (x in test_vector) {
if (current_test_index == 1) {
points(x, l$y[max(which(x > l$x))] + 0,
cex = size.shape, pch = pch_vector[current_test_index])
} else {
points(x, l$y[max(which(x > l$x))] - par("yaxp")[2] * (current_test_index - 1) * n,
cex = size.shape, pch = pch_vector[current_test_index])
}
}
}
}
dev.off()
And since I cannot upload the csv files. here is a snapshot of both main.csv and lookup.csv
core_depth,hra,pc1,pc2,rhob,MRP ,XRD ,Geochem ,GC ,OP ,SSK ,BET ,TS,SEM ,NMR ,TXC ,BZT ,FT ,PE,CST ,MICP,OR,RW,TPGP,TGA ,DS,KF,CST,CO,FA
8305.03,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.06281,Green3,1.311306,-2.641727,2.62,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.09562,Green3,1.310027,-2.57709,2.62,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.12843,Green3,1.299473,-2.526686,2.61,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.16123,Green3,1.250434,-2.562131,2.64,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.19404,Green3,1.308886,-2.505513,2.634925,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.22685,Green3,1.285146,-2.477449,2.63,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.25966,Green3,1.202284,-2.5237,2.64,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.29247,Green3,1.142869,-2.566874,2.64,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.32528,Green3,1.138197,-2.601127,2.64,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.35808,Green3,1.13805,-2.626172,2.66,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.39089,Green3,1.19202,-2.600554,2.66,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.4237,Green3,1.202864,-2.626277,2.66,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.45651,Green3,1.109449,-2.658444,2.66,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.48932,Green3,1.069722,-2.663563,2.66,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.52213,Green3,1.043733,-2.65977,2.66,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.55493,Green3,1.020991,-2.636292,2.66,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.58774,Green3,0.975608,-2.646655,2.67,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.62055,Green3,0.876026,-2.692267,2.67,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
8305.65336,Green3,0.850114,-2.696088,2.67,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999,-9999
And here is the lookup.csv file:
hra,red,green,blue
Purple0,128,0,128
Purple1,165,0,165
Purple2,191,0,191
Black0,0,0,0
Black1,34,34,34
Black2,58,58,58
Black3,81,81,81
Black4,104,104,104
Black5,128,128,128
Black6,151,151,151
Black7,174,174,174
Black8,197,197,197
Black9,221,221,221
Brown0,128,0,0
Brown1,141,41,5
Brown2,154,82,11
Brown3,173,115,58
Brown4,192,148,104
Olive0,128,128,0
Olive1,144,144,34
Olive2,156,156,58
Olive3,168,168,81
Olive4,179,179,104
Olive5,191,191,128
Thanks in advance for your time.