I have a modelling program prepared which produces 26 separate files that I need to contour plot in the same way in R - I am planning on running this program repeatedly, and hence have been trying to create a loop which creates and saves plots for all 26 files for each run.
I'm trying to save the files with their original file names (have put a placehold name in for now) hence why I have created the object 'names' and save each to a png or jpeg file. At the moment the loop is only saving 1 file (despite reading in 4 sets of data with list.files) and this is appearing as a white square with the correct dimensions rather than the graph produced by 'makeplot' (have tested on individual files).
Sample of my data:
Z X T
0 0 0
0 0.005 0
0 0.01 0
0 0.015 0
(and so on for 84k rows)
My code:
filenames <- list.files(path=".",
pattern="csv",
full.names=TRUE)
names <- as.vector(filenames)
# Creating a directory to save contour plots
dir.create("Contour plots")
#Creates a contour plot in ggplot of the variable in xz space
makeplot <- function(filename) {
data <- as.data.frame(read.csv(file = filename), header = FALSE)
ggplot(data=data, mapping = aes(x = data[,2],
y=data[,1],
z = data[,3])) +
geom_raster(data=data, aes(fill=data[,3]), show.legend=TRUE, interpolate
= FALSE) +
scale_fill_gradient(limits=range(data[,3]), high = 'red', low =
'white')+
geom_contour(bins = 30, colour = "black") +
xlab(label = "Distance from ridge axis") +
ylab(label = "Depth") +
theme_classic()+
coord_cartesian(
ylim = c(0,1), xlim = c(0,2))+
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
guides(fill=guide_legend(title="Yb concentration")) +
theme(legend.position="bottom")
}
for (f in filenames) {
png(filename="Rplot%03d.png", height = 600, width = 1200)
makeplot(f)
dev.off()
}
Any help would be appreciated!