ggplot2 中的三重图例,具有点形状、填充和颜色
Triple legend in ggplot2 with point shape, fill and color
我的数据框(link 见下文)包含多个模型的输出,我想 "categorize" 根据:
- 模型名称(9 个模型)-> 9 种形状填充颜色
- 模型域(每个模型都有以下两者之一:"MED" 或 "EURO")-> 两种空心形状类型
- 原始分辨率(每个模型有两个"versions":"HR"和"LR")-> 2种形状轮廓颜色
此外,还包括一个单独的观测数据集,它应该以自己的颜色和填充形状显示。
总而言之,我有 9x2=18 个点类型,每个点类型也有一个 "domain" 标签,外加一个观测点类型。
以下是最简单的开始,但根本不起作用。另外,我不想使用 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))
我已经尝试了一些修复,例如此处建议的修复:
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?
但是我似乎找不到解决这个看似简单的问题的方法。
您可以使用 override.aes
在指南中指定其中一个空心形状的填充和颜色。此外,如果您为填充指定 colour = NA
,您将在指南中得到 "just the fill"。
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)))
这修复了图例,但读取三个变量映射到点的散点图可能并不容易...您是否考虑过改用分面?
我的数据框(link 见下文)包含多个模型的输出,我想 "categorize" 根据:
- 模型名称(9 个模型)-> 9 种形状填充颜色
- 模型域(每个模型都有以下两者之一:"MED" 或 "EURO")-> 两种空心形状类型
- 原始分辨率(每个模型有两个"versions":"HR"和"LR")-> 2种形状轮廓颜色
此外,还包括一个单独的观测数据集,它应该以自己的颜色和填充形状显示。
总而言之,我有 9x2=18 个点类型,每个点类型也有一个 "domain" 标签,外加一个观测点类型。
以下是最简单的开始,但根本不起作用。另外,我不想使用 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))
我已经尝试了一些修复,例如此处建议的修复:
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?
但是我似乎找不到解决这个看似简单的问题的方法。
您可以使用 override.aes
在指南中指定其中一个空心形状的填充和颜色。此外,如果您为填充指定 colour = NA
,您将在指南中得到 "just the fill"。
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)))
这修复了图例,但读取三个变量映射到点的散点图可能并不容易...您是否考虑过改用分面?