1

I have a dataframe like this testframe, but much larger.

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

I want to make contourplots for each data-column (columns 3:5 in the testframe) I found multiple question on how to save multiple plots automatially. the best way in my opinion is to save all plots in a list and then save them later from this list. My try is down below.

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

I dont know what I am doing wrong. I am getting this error:

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

Does anyone know why this is not working?

Mr.Spock
  • 511
  • 2
  • 13
  • Possible duplicate: https://stackoverflow.com/questions/22309285/how-to-use-a-variable-to-specify-column-name-in-ggplot – MrFlick Oct 17 '19 at 14:44

1 Answers1

1

You can't insert variables into regular aes() statements because those are evaluated at print time. You need to pass sumbols and not strings, and variables aren't enclosed in the plot object. So something like

aes(x = Lon, y = Lat, z = colnames(testframe)[i])

won't work. With the latest version of ggplot2, you can insert symbols into the expression with a combination of rlang::sym() and the !! (bang bang) operator. Try

aes(x = Lon, y = Lat, z = !!rlang::sym(colnames(testframe)[i]))
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thanks for the answer. I still get errors: Error: unexpected symbol in: " labs(title= paste("Plot", colnames(testframe)[i], sep = " ")) plot_list" > > plot_list[1:2] = NULL > names(plot_list) = colnames(testframe)[3:ncol(testframe)] Error in names(plot_list) = colnames(testframe)[3:ncol(testframe)] : 'names' attribute [3] must be the same length as the vector [0] > > > for (i in 1:3) { + file_name = paste("Plot", i, ".tiff", sep="") + tiff(file_name) + print(plot_list[[i]]) + dev.off() + } Error in plot_list[[i]] : subscript out of bounds – Mr.Spock Oct 17 '19 at 14:48
  • Are you sure you get that with the code and data above? I just ran it with the change I suggested and didn't get that error. – MrFlick Oct 17 '19 at 14:50
  • Ok, seems like I did a mistake! Now its running! Thank you so much! – Mr.Spock Oct 17 '19 at 15:34