我有一个像这样的测试框架,但是更大。
lat_area = seq (50, 40, -0.5)
lon_area = seq (-10, 10, 0.5)
grid_area = matrix(0, ncol=2, nrow=length(lon_area)*length(lat_area))
grid_area[,2] = rep(lat_area, each=length(lon_area))
grid_area[,1] = lon_area
testframe = as.data.frame(grid_area)
colnames(testframe) = c("Lon", "Lat")
testframe$FDsw = rep(c(0.5,0.7), length.out= nrow(testframe))
testframe$Thgf = rep(c(0.2,0.3), length.out= nrow(testframe))
testframe$Igbff = rep(c(0.8,0.9), length.out= nrow(testframe))
我想为每个数据列创建轮廓图(测试框架中的列3:5) 我发现了关于如何自动保存多个图的多个问题。我认为最好的方法是将所有图解保存到列表中,然后再从列表中保存它们。我的尝试在下面。
library(ggplot2)
library(metR)
plot_list = list()
for (i in 3:length(testframe)) {
wd = map_data("world")
LP = ggplot()
LP = LP +
geom_contour_fill(data = testframe, aes(x = Lon, y = Lat, z = colnames(testframe)[i]), breaks = MakeBreaks(0.1)) +
geom_contour(data = testframe, aes(x = Lon, y = Lat, z = colnames(testframe)[i]), color = "blue", size = 0.6) +
geom_text_contour(data = testframe, aes(x = Lon, y = Lat, z = colnames(testframe)[i]), stroke = 0.1) +
scale_fill_divergent() +
geom_polygon(data = wd , aes(x = long, y = lat, group = group), colour="black", fill = NA) +
coord_cartesian(xlim = c(-10, 10), ylim = c(50, 40)) +
scale_x_longitude(ticks = 10)+ scale_y_latitude(ticks = 10) +
labs(title= paste("Plot", colnames(testframe)[i], sep = " "))
plot_list[[i]] = LP}
plot_list[1:2] = NULL
names(plot_list) = colnames(testframe)[3:ncol(testframe)]
for (i in 1:3) {
file_name = paste("Plot", i, ".tiff", sep="")
tiff(file_name)
print(plot_list[[i]])
dev.off()
}
我不知道我在做什么错。我收到此错误:
Error in UseMethod("fullseq") :
no applicable method for 'fullseq' applied to an object of class "character"
In addition: Warning message:
In pretty.default(range(data$z, na.rm = TRUE), 10) :
NAs introduced by coercion
有人知道为什么这不起作用吗?
答案 0 :(得分:1)
您不能在常规aes()
语句中插入变量,因为这些变量是在打印时评估的。您需要传递sumbol而不是字符串,并且变量不包含在plot对象中。所以像
aes(x = Lon, y = Lat, z = colnames(testframe)[i])
不起作用。使用最新版本的ggplot2,您可以使用rlang::sym()
和!!
(bang bang)运算符的组合将符号插入表达式。试试
aes(x = Lon, y = Lat, z = !!rlang::sym(colnames(testframe)[i]))