简体   繁体   English

ggplot2中的三重图例,具有点形状,填充和颜色

[英]Triple legend in ggplot2 with point shape, fill and color

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 型号名称(9个型号)-> 9种形状填充颜色
  • 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 原始分辨率(每个模型都有两个“版本”:“ HR”和“ LR”)-> 2个形状轮廓颜色

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. 因此,总的来说,我有9x2 = 18个点类型,每个点类型都有一个“域”标记,以及一个观察点类型。

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. 另外,我不想使用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 ggplot2:具有来自公共变量的两个视觉特性的图例

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. 但是我似乎找不到解决这个看似简单的问题的方法。

You can use override.aes to specify one of the hollow shapes in the guides for fill and colour. 您可以使用override.aes在填充和颜色指南中指定空心形状之一。 Additionally, if you specify colour = NA for fill, you will get "just the fill" in the guide. 另外,如果您指定colour = NA作为填充,您将在指南中得到“仅填充”。

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? 这可以修复图例,但要读取具有三个映射到点的变量的散点图可能并不容易...您是否考虑过使用分面?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM