0
votes

My dataframe (see below for link) contains output from several models, which I want to "categorize" according to:

  • model name (9 models) -> 9 shape fill colors
  • model domain (each model has one of either of these two: "MED" or "EURO") -> two hollow shape types
  • native resolution (each model has two "versions": "HR" and "LR") -> 2 shape contour colors

Additionally, a single observational dataset is also included, which should be shown with it's own color and filled shape.

So all in all I have 9x2=18 point types, each of which also has a "domain" tag, plus a single observational point type.

The following, which is the simplest thing one may start from, does not work at all. Also, I don't want to manually specify colors and shapes for each item using scale_*_manual.

library(ggplot2)

#Sorry for the wget, I can't seem to make load() read the url directly
system("wget https://copy.com/LrEznd8QCmzVBNyz/df_pr_PDF_alps_044.Rdata?download=1") 
load("df_pr_PDF_alps_044.Rdata?download=1") #This loads df
colnames(df) #Take a look at the columns

ggplot(data=df[which(df$PDF > 0),], aes(x=x, y=PDF)) +
geom_point(aes(shape=domain, colour=nativeresolution, fill=model), alpha=0.7, size=2)+
scale_shape_manual(values=c(21,22,23))

I have tried some fixes such as those suggested here:

ggplot2: One legend with two visual properties derived from common variable

Combine legends for color and shape into a single legend

How to merge colour and shape?

But I can't seem to find a solution to this seemingly simple problem.

1
Regaring shapes with 'fill' and 'contour' and "I don't want to manually specify colors and shapes for each item using scale_*_manual", please read ?pch: "21:25 can be colored and filled with different colors". Thus, I think you need to map the levels of your variable to pch:s which you define manually.Henrik
@Henrik Yes, you are completely right, 21 to 25 are the only ones that are affected by fill so I should use those. I forgot. I'll edit the example to use those shapes. Still it's not working well: all the legends are messed up.AF7

1 Answers

1
votes

You can use override.aes to specify one of the hollow shapes in the guides for fill and colour. Additionally, if you specify colour = NA for fill, you will get "just the fill" in the guide.

library(ggplot2)

system("wget https://copy.com/LrEznd8QCmzVBNyz/df_pr_PDF_alps_044.Rdata?download=1") 
load("df_pr_PDF_alps_044.Rdata?download=1")

ggplot(data=df[which(df$PDF > 0),],
       aes(x = x, y = PDF, shape = domain, colour = nativeresolution, fill = model)) +
  geom_point(alpha=0.7, size=2) +
  scale_shape_manual(values=c(21,22,23)) +
  guides(fill = guide_legend(override.aes = list(shape = 21, colour = NA)),
         colour = guide_legend(override.aes = list(shape = 21)))

This fixes the legend but it may not be easy to read a scatter plot with three variables mapped to the points... have you considered using facets instead?