I have a data set where I want to plot columns against each other i an facet wrap or grid arrange fashion. My data set called SardF.plot:
str(SardF.plot)
'data.frame': 42 obs. of 22 variables:
$ Sample : chr "Kx V17" "Mu V17" "Ob V17" "Vä V17" ...
$ Mill : chr "Kx" "Mu" "Ob" "Vä" ...
$ Halfyear: chr "V17" "V17" "V17" "V17" ...
$ pH : num 12.4 11.56 9.84 12.84 10.68 ...
$ Na : num 59199 22604 9095 30052 18014 ...
$ K : num 5547 1994 1345 8238 1276 ...
$ Ca : num 23.6 17.6 68.4 22.2 17.9 ...
$ Cr : chr "0.90659967379114681" "1.6064235870083998E-2" "
$ Ni : num 0.0314 0.036 0.1208 0.0396 0.041 ...
$ Cu : num 0.0786 0.4648 0.0656 0.4747 0.2705 ...
$ Zn : num 0.244 0.384 0.269 0.748 0.205 ...
$ Cd : num 0.00375 0.00339 0.0035 0.00216 0.00361 ...
$ Pb : num 0.000654 0.00148 0.000644 0.008429 0.000576 ...
$ Na Fast : num 70848 53117 22256 84498 27894 ...
$ K Fast : num 6392 4732 3238 9547 2158 ...
$ Ca Fast : num 175958 140652 150944 240352 141438 ...
$ Cr Fast : num 150 102 124 83 256 65 17 17 41 418 ...
$ Ni Fast : num 54.8 73.5 210.5 38.8 170.7 ...
$ Cu Fast : num 155 614 589 208 453 ...
$ Zn Fast : num 1493 5909 5145 2074 3582 ...
$ Cd Fast : num 6.02 14.25 12.67 7.36 14.47 ...
$ Pb Fast : num 27.2 47.2 11.1 23.4 16.5 9.6 3.1 8.2 12.5 30 ...
I want to plot column5 against column13, and column6 against column14 etc. I have successfully used facet_wrap when all the plots have the same x (Element is a vector containing Na-Pb):
ggplot(gather(Sardinia.plot, key=Element, value="value", -"pH", -"Sample", -"Mill", -"Halfyear"),
aes(x=pH, y=as.numeric(value), colour=Mill, group=Sample) ) +
geom_point(aes(shape=Halfyear)) +
facet_wrap(~ Element, scales = 'free') +
ggtitle("pH VS leached amount at LS 10") +
ylab("mg leached/kg GLD")+
xlab("pH")+
theme(plot.title = element_text(hjust = 0.5),
legend.title = element_blank())
and I can successfully generate the plots I want as individual plots and save them by using a for loop:
for (j in 0:1)
{
setwd("\\\\orunet\\dfs\\home07\\nse\\my documents\\LS lakningar\\R bilder\\mg ut per kg mot fast Sardinia")
for (i in 5:13)
{
Fast <- i+9
myplot<-ggplot(SardF.plot) +
geom_point( aes(x=SardF.plot[[Fast]], y=as.numeric(SardF.plot[[i]]), colour=Mill, shape=Halfyear), size=3 ) +
ggtitle(colnames(SardF.plot[i])) +
xlab("mg/kg in solid GLD") +
ylab("mg leached/kg GLD") +
theme(plot.title = element_text(hjust = 0.5),
legend.title = element_blank()
)
ID <-colnames(SardF.plot[i])
ggsave(myplot, filename=paste(ID,".jpeg",sep=""), width = 16, height = 15, units = "cm")
}
setwd("\\\\orunet\\dfs\\home07\\nse\\my documents\\LS lakningar")
}
But I can not figure out how to take the above plots and paste them all on a single figure. I can not use Grid.arrange since the plots generated by the loop can not be called as they all are stored into myplot and I have not figured out how to store them in individual names (and I really really do not want to use copy and paste with p1...n<- instead of a loop as I can have up to 40 graphs). Using the above facet_wrap with specifying x as and y as SardF["x-colums"] and SardF["y-colums"] did not work either. I am stuck, can anyone help me?