我有一个R脚本,我使用R Base Graphics在pdf输出文件中生成多页(4行和4列)直方图,每页有密度行。这个程序的剥离(以避免混乱和产生的分心)版本如下所示:
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()
麻烦的是:我无法使用mtext显示绘图标题,图例和复合左右轴标签。如果我在退出最外面的'for'循环后尝试放置标题,图例和标签,它只显示在最后一个直方图中并且没有正确定位。
如果我把它放在我要问的地方'我应该把Plot的标题和图例放在哪里?正如我在下面的源代码中所示,我收到以下消息:
标题出错(标题,行= -0.4,外部= TRUE): plot.new还没有被调用
我只是尝试将以下代码行放在我所评论的问题中:
title(title,line=-0.4,outer=TRUE)
知道如何解决这个问题吗?程序需要显示相当多的直方图,以便它们运行到下一页。这很好用,但标题,图例和Axes标签是个问题。根据反馈,这里是我的代码的完整脚本,实际上非常小:
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()
因为我无法上传csv文件。这是main.csv和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
这是lookup.csv文件:
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
提前感谢您的时间。