Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colouring points by factor within the margin of a faceted ggplot2 plot in R

Tags:

r

ggplot2

facets

I'd like to create a faceted plot with margins in ggplot2. However, I'd like the margin plot to have colours according to from which facet the particular point has been derived. It's probably best illustrated with an example:

library(ggplot2)

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + facet_grid(.~gear, margins = TRUE)

Within the margin plot labelled as "(all)", I want those dots that have "gear = 3" to be plotted with one colour, those with "gear = 4" with a second colour, and those with "gear = 5" with a third one.

This one doesn't do the job:

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point(aes(col=gear))
p + facet_grid(.~gear, margins = TRUE)

Is there a way to achieve what I want?

like image 856
AnjaM Avatar asked May 06 '13 12:05

AnjaM


People also ask

How do I assign default colors to a ggplot2 plot based on species?

The following code shows how to assign default colors to the points in a ggplot2 plot based on the factor variable Species: Since we didn’t specify a color scale or a list of custom colors, ggplot2 simply assigned a list of default red, green, and blue colors to the points.

How do I change the margins in ggplot2?

Margins in ggplot2. Increase margins. Remove margins. The margins of the plots made with ggplot2 will adjust automatically to new layers, e.g. if you add a title. We have added a black box around the sample plot so you can see how margins change.

How many rows and columns are in a ggplot2 plot?

Let’s start right away. Table 1 shows the structure of our example data: It is constructed of six rows and three columns. For the examples of this tutorial, we’ll also have to install and load the ggplot2 package to RStudio:

How do I remove the margins from a plot?

The margins are measured with points ( "pt" ), but you can use other unit measure in the unit argument, like centimeters. Type ?unit to see all the possible measures. To remove the margins set all values to 0. Note that there is still space to fit all the elements of the plot.


1 Answers

How about creating a new variable as a reference and colour the points by that? Seems to work provided you don't mind the points in the first 3 facets being coloured too.

mtcars$ref <- as.factor(mtcars$gear)

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point(aes(col=as.factor(gear)))
p + facet_grid(.~ref, margins = TRUE)

All points coloured by gear

EDIT: I have managed to get it to remove the colour key from the first 3 facets but not without playing around with the original data;

Duplicate the original data (so there are 2 of each record) then instead of using a margin plot to produce the "all" facet, using the redundant records instead.

library(ggplot2)

mtcars$ref <- (mtcars$gear)

# create the duplicate
dat <- do.call("rbind", replicate(2, mtcars, simplify = FALSE)) 

# give the duplicates a false value for "gear" so they can be plotted together 
#This value can then be used for faceting, grouping everything with "all".

dat$ref[1:32] <- "all" 


# where not in the "all" facet, change "gear" to one (so they are plotted with the same colour)
dat$gear[dat$ref != "all"] <- 1

# then plot using ref as the facet and gear to colour points.

p <- ggplot(dat, aes(mpg, wt)) + geom_point(aes(col=as.factor(gear)))
p + facet_grid(.~ref, margins = F)

Points only coloured by gear in final facet

I'm not sure that's the best way to go about it but perhaps someone with more expertise might be able to advise?

like image 137
Adam Kimberley Avatar answered Nov 03 '22 10:11

Adam Kimberley