How to get a scatter matrix consisting of just scatterplots with a 1:1 line, and good axis labels?

56 views Asked by At

I want to have a matrix of just scatterplots in which a 1:1 line is present. The data has the following structure:

bulk_SIC_25 <- data.frame(soilsample$bulk_SIC_scheibler_25,soilsample$bulk_SIC_LECO.CBLB_25,soilsample$bulk_SIC_RE6_25)
colnames(bulk_SIC_25)<-c("SIC_scheibler_25","SIC_LECO-CBLB_25","SIC_RE6_25")

The data inside the dataframe is:

dput(bulk_SIC_25) structure(list(SIC_scheibler_25 = c(8.292, 9.648, 9.072, 6.084, 10.944, 12.48, 4.368, 6.732, 5.592, 9.024, 8.7, 9.48, 9.588, 3.432, NA, 15.72), SIC_LECO-CBLB_25 = c(6.9, 9.5, 8, 6.6, 11.1, 13, NA, 7, 5.5, 8.95, 9.14, 9.6, 9.2, 3.5, NA, NA), SIC_RE6_25 = c(10.4, 10.7, 11.3, 8.6, 13.2, 15.5, 6.3, 9.6, 7.6, 10.5, 10.7, 11.3, 11.4, 5.1, 17.2, 17.9)), class = "data.frame", row.names = c(NA, -16L))

The pairs function produces a nice matrix with just scatterplots and visible x/y axes and variable names: example pairs

pairs(bulk_SIC_25, labels=colnames(bulk_SIC_25),gap=0.5, main=" Total Carbon in bulk soil ",lower.panel=NULL, cex.labels=1.5 ,pch=21, bg="Black")`

However, I cannot change the size of the points, nor add a 1:1 line. Therefore, I switched to ggpairs with internal labels:

Example ggpairs internal labels Code:

ggpairs(data=soilsample, columns = c("bulk_SIC_scheibler_25","bulk_SIC_LECO.CBLB_25","bulk_SIC_RE6_25"), upper="blank",axisLabels = "internal")+ggtitle(label="Soil Organic Carbon bulk soil")+theme_bw(base_size=15)+geom_abline(intercept=0,slope=1)+labs(x="g C/ kg soil", y= "g C / kg soil")+geom_point(size=3)+  theme(plot.title = element_text(hjust = 0.5))

However, as you can see, the gridlines of the internal axislabels do not match with the scatterplots, so that the scatterplots are very hard to read.

I have tried:

limitRangediag <- function(data, mapping, ...) {
      ggplot(data = data, mapping = mapping, ...) + 
        scale_y_continuous(limits = c(5, 16)) +
        scale_x_continuous(limits = c(5, 16)) 
}

ggpairs(data=soilsample, columns = c("bulk_SIC_scheibler_25","bulk_SIC_LECO.CBLB_25","bulk_SIC_RE6_25"), upper="blank",lower = list(continuous = limitRange),diag=list(continous=limitRangediag),axisLabels = "internal")+ggtitle(label="Soil Organic Carbon bulk soil")+theme_bw(base_size=15)+geom_abline(intercept=0,slope=1)+labs(x="g C/ kg soil", y= "g C / kg soil")+geom_point(size=3)+
      theme(plot.title = element_text(hjust = 0.5))

I know I do something wrong in writing the function, but I am very inexperienced in this and no clue what I do wrong.

I could also have peace in putting external labels on the right spot in ggpairs (so skipping the first blank row, and put the labels above the plots itself), but I would have no clue to do this...: example ggpairs external labels

ggpairs(data=df, columns = c("bulk_SIC_scheibler_25","bulk_SIC_LECO.CBLB_25","bulk_SIC_RE6_25"), upper="blank",diag="blank",axisLabels = "shown")+ggtitle(label="Soil Organic Carbon bulk soil")+theme_bw(base_size=15)+geom_abline(intercept=0,slope=1)+labs(x="g C/ kg soil", y= "g C / kg soil")+geom_point(size=3)+ theme(plot.title = element_text(hjust = 0.5))

If anybody could help me out in fixing one of these 3 options to get a nice matrix of scatterplots with a 1:1 line and points adjustable in size, with readable axis labels, I would be very grateful!!

1

There are 1 answers

1
Ben Bolker On BEST ANSWER
  • in pairs(), you can specify the panel= argument (which is points() by default) as a function that includes whatever you want, such as a call to abline() (in your case you would use upper.panel=, since you want the lower panel to be NULL)
  • the cex argument (which is passed to the panel function) will change the point size.
pairs(mtcars[,1:5],
   panel = function(...) { points(...); abline(a=0,b=1,lty=2,col="red") },
   cex=4,
   gap=0  ## you didn't ask for this but I prefer it
)

In this particular example the 1:1 line doesn't appear in every panel, but it's there in principle (you'd have to set x, y limits to ensure that it intersected with the panel area)

enter image description here