Multiple plots

Posted on Updated on

Plotting multiple curves and datasets on the same graph

It is quite common that we want to display different curves on the same graph, and / or display different sets of data in order to investigate the parameter space being covered.

This is often done when looking at various galaxy cluster datasets and comparing, for instance, the scaling relations derived for each sample. Let's consider, as an example, 3 samples which I will call A, B and C. Let's assume further that the L-M scaling relation has been derived for each sample. The results for the normalisation Alm and slope blm are summarised in the table below.

Sample Alm blm
A 0.76\pm 0.08 1.34\pm 0.05
B 0.59\pm 0.11 1.64\pm 0.23
C 1.13\pm 0.35 2.42\pm 0.46


First, let's generate three data sets…

dat1 <- data.frame(

dat2 <- data.frame(

dat3 <- data.frame(

To display these three datasets easily with ggplot, we have to create a new data frame which holds all these data frames + an extra parameter (which I will call sample) which refers to the name of the data sample for each index value.

dat <- data.frame(

N.B. There is a simpler way to merge different data frames using dplyr but let's leave this for the moment. I might update the post later and/or explain how to do this in another post.

Fitting parameters

In the same way that we created a combined data frames to hold the data values, we are going to create a combined data frame containing the slope and normalisation values for each of our samples.

##plot limits
mmin <- 13.5
mmax <- 15.5
lmin <- 43.5
lmax <- 45.5

##create a function to hold errors in log space
err <- function(x,logAe,Be) {
  ##form is y = B*logx + logA
  err <- sqrt( (Be*x)**2 + logAe**2 )

##these lines are created from real data so I correct here for 
##the pivot mass and luminosity
xlinem <- seq(mmin,mmax,0.1)
ylinem1 <- 44.76 + 1.34*(xlinem - 15)
ylinem2 <- 44.77 + 1.64*(xlinem - 15)
ylinem3 <- 44.75 + 2.42*(xlinem - 14.7)
em1 <- err(xlinem-15,0.08/0.76/log(10),0.05)
em2 <- err(xlinem-15,0.11/0.59/log(10),0.23)
em3 <- err(xlinem-14.7,0.35/1.13/log(10),0.46)

fitdat <- data.frame(


Let's create the plot!!


p1 <- ggplot(data=fitdat, aes(x=x, y=y,group=fit))+
    geom_line(data=fitdat, aes(linetype=factor(fit)))+
    geom_ribbon(data=fitdat, aes(ymin=ylow, ymax=yup,fill=factor(fit)),
    xlab(expression(paste("log(",M,"/",h[70],M[sol],"])"))) +

plot of chunk unnamed-chunk-1

Add a nice legend

##add nice colours
mycolours <- c("#aaaaf0","#f0aaf0","#f0f0aa","#aaf0aa")
mycolours2 <- c("#7f7fe8","#e87fe8","#e8e87f","#7fe87f")

p1+theme_bw(16) +
    theme(legend.justification=c(1,0), legend.position=c(1,0))+

plot of chunk data+combined+fit+plot+legend


p>Note that in creating these plots, I have assumed that the L-M relations were not derived from the 3 datasets… which seems obvious considering the poor agreement between the data and the fits!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s