Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the colour of ablines on ggplot

Tags:

r

ggplot2

Using this data I am fitting a plot:

p <- ggplot(dat, aes(x=log(Explan), y=Response)) + 
      geom_point(aes(group=Area, colour=Area))+
      geom_abline(slope=-0.062712, intercept=0.165886)+
      geom_abline(slope= -0.052300, intercept=-0.038691)+
      scale_x_continuous("log(Mass) (g)")+
      theme(axis.title.y=element_text(size=rel(1.2),vjust=0.2),
            axis.title.x=element_text(size=rel(1.2),vjust=0.2),
            axis.text.x=element_text(size=rel(1.3)),
            axis.text.y=element_text(size=rel(1.3)),
            text = element_text(size=13)) +
      scale_colour_brewer(palette="Set1")

The two ablines represent the phylogenetically adjusted relationships for each Area trend. I am wondering, is it possible to get the ablines in the same colour palette as their appropriate area data? The first specified is for Area A, the second for Area B.

I used:

g <- ggplot_build(p) 

to find out that the first colour is #E41A1C and the second is #377EB8, however when I try to use aes within the +geom_abline command to specify these colours i.e.

p <- ggplot(dat, aes(x=log(Explan), y=Response)) + 
      geom_point(aes(group=Area, colour=Area))+
      geom_abline(slope=-0.062712, intercept=0.165886,aes(colour='#E41A1C'))+
      geom_abline(slope= -0.052300, intercept=-0.038691,aes(colour=#377EB8))+
      scale_x_continuous("log(Mass) (g)")+
      theme(axis.title.y=element_text(size=rel(1.2),vjust=0.2),
            axis.title.x=element_text(size=rel(1.2),vjust=0.2),
            axis.text.x=element_text(size=rel(1.3)),
            axis.text.y=element_text(size=rel(1.3)),
            text = element_text(size=13)) +
      scale_colour_brewer(palette="Set1")

It changes the colour of the points and adds to the legend, which I don't want to do.

Any advice would be much appreciated!

like image 297
Sarah Avatar asked Jul 03 '13 16:07

Sarah


2 Answers

As your are setting colors directly then you don't need aes() just use colour='#E41A1C'.

+geom_abline(slope=-0.062712, intercept=0.165886,colour='#E41A1C')
like image 114
Didzis Elferts Avatar answered Nov 15 '22 13:11

Didzis Elferts


Given that you are reading colour for the lines to correspond with those set for the points which are mapped from Area, you can map these using the appropriate values for area.

eg

 geom_abline(slope=-0.062712, intercept=0.165886,aes(colour='A')) +
  geom_abline(slope= -0.052300, intercept=-0.038691,aes(colour='B'))

This has the added bonus that it will be consistent if you change the colour scheme.

a second approach would be pass a data.frame containing the slopes and intercepts and Area, eg

 cc <- data.frame(sl = c(-0.062712,-0.052300), 
                  int = c(0.165886,-0.038691), 
                  Area = c('A','B'))

Then you could map the slope, intercept and colour within a single call to geom_abline

eg

p <- ggplot(dat, aes(x=log(Explan), y=Response)) + 
    geom_point(aes(group=Area, colour=Area))+
    geom_abline(data = cc, aes(slope =sl, intercept = int,colour = Area)) +
    scale_x_continuous("log(Mass) (g)")+
    theme(axis.title.y=element_text(size=rel(1.2),vjust=0.2),
          axis.title.x=element_text(size=rel(1.2),vjust=0.2),
          axis.text.x=element_text(size=rel(1.3)),
          axis.text.y=element_text(size=rel(1.3)),
          text = element_text(size=13)) +
    scale_colour_brewer(palette="Set1")
p 

enter image description here

like image 22
mnel Avatar answered Nov 15 '22 15:11

mnel