当geom_hline在颜色图例中有一个单独的(附加)条目时,如何合并颜色和形状的图例?

时间:2015-06-14 12:53:17

标签: r ggplot2

我有以下代码,它产生以下图:

cols <- brewer.pal(n = 3, name = 'Dark2')

p4 <- ggplot(all.m, aes(x=xval, y=yval, colour = Approach, ymax = 0.95)) + theme_bw() + 
  geom_errorbar(aes(ymin= yval - se, ymax = yval + se), width=5, position=pd) + 
  geom_line(position=pd) + 
  geom_point(aes(shape=Approach, colour = Approach), size = 4) + 
  geom_hline(aes(yintercept = cp.best$slope, colour = "C2P"), show_guide = FALSE) + 
  scale_color_manual(name="Approach", breaks=c("C2P", "P2P", "CP2P"), values =  cols[c(1,3,2)]) + 
  scale_y_continuous(breaks = seq(0.4, 0.95, 0.05), "Test AUROC") +
  scale_x_continuous(breaks = seq(10, 150, by = 20), "# Number of Patient Samples in Training")
p4 <- p4 + theme(legend.direction = 'horizontal', 
      legend.position = 'top', 
      plot.margin = unit(c(5.1, 7, 4.5, 3.5)/2, "lines"), 
      text = element_text(size=15), axis.title.x=element_text(vjust=-1.5), axis.title.y=element_text(vjust=2))   
p4 <- p4 + guides(colour=guide_legend(override.aes=list(shape=c(NA,17,16))))

p4

enter image description here 当我在show_guide = FALSE中尝试geom_point时,上图例中点的形状都设置为默认实心圆。

如何在不影响上层传奇的情况下让较低的图例消失?

2 个答案:

答案 0 :(得分:2)

这是一个完整的数据解决方案:

library("ggplot2")
library("grid")
library("RColorBrewer")

cp2p <- data.frame(xval = 10 * 2:15, yval = cumsum(c(0.55, rnorm(13, 0.01, 0.005))), Approach = "CP2P", stringsAsFactors = FALSE)
p2p <- data.frame(xval = 10 * 1:15, yval = cumsum(c(0.7, rnorm(14, 0.01, 0.005))), Approach = "P2P", stringsAsFactors = FALSE)

pd <- position_dodge(0.1)
cp.best <- list(slope = 0.65)

all.m <- rbind(p2p, cp2p)
all.m$Approach <- factor(all.m$Approach, levels = c("C2P", "P2P", "CP2P"))
all.m$se <- rnorm(29, 0.1, 0.02)
all.m[nrow(all.m) + 1, ] <- all.m[nrow(all.m) + 1, ] # Creates a new row filled with NAs
all.m$Approach[nrow(all.m)] <- "C2P"
cols <- brewer.pal(n = 3, name = 'Dark2')

p4 <- ggplot(all.m, aes(x=xval, y=yval, colour = Approach, ymax = 0.95)) + theme_bw() + 
  geom_errorbar(aes(ymin= yval - se, ymax = yval + se), width=5, position=pd) + 
  geom_line(position=pd) + 
  geom_point(aes(shape=Approach, colour = Approach), size = 4, na.rm = TRUE) + 
  geom_hline(aes(yintercept = cp.best$slope, colour = "C2P")) + 
  scale_color_manual(values = c(C2P = cols[1], P2P = cols[2], CP2P = cols[3])) + 
  scale_shape_manual(values = c(C2P = NA, P2P = 16, CP2P = 17)) +
  scale_y_continuous(breaks = seq(0.4, 0.95, 0.05), "Test AUROC") +
  scale_x_continuous(breaks = seq(10, 150, by = 20), "# Number of Patient Samples in Training")
p4 <- p4 + theme(legend.direction = 'horizontal', 
                 legend.position = 'top', 
                 plot.margin = unit(c(5.1, 7, 4.5, 3.5)/2, "lines"), 
                 text = element_text(size=15), axis.title.x=element_text(vjust=-1.5), axis.title.y=element_text(vjust=2))   
p4

Plot of example

诀窍是确保all.m$Approach中出现all.m的所有所需级别,即使其中一个级别从图表中删除也是如此。有关省略点的警告被na.rm = TRUE的{​​{1}}参数抑制。

答案 1 :(得分:1)

简答:
只需添加一个虚拟geom_point图层(透明点),其中shape映射到与level中相同的geom_hline

geom_point(aes(shape = "int"), alpha = 0) 

更长的回答:
只要有可能,ggplot会合并/组合不同aes主题的传说。例如,如果colourshape映射到同一个变量,则两个图例合并为一个。

我使用简单的数据集来说明这一点,&#39; x&#39;&#39; y&#39;和分组变量&#39; grp&#39;有两个层次:

df <- data.frame(x = rep(1:2, 2), y = 1:4, grp = rep(c("a", "b"), each = 2))

首先,我们将colorshape映射到&#39; grp&#39;

ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) +
  geom_line() +
  geom_point(size = 4)

enter image description here

很好,aes thetics,colorshape的图例合并为一个。

然后我们添加geom_hline。我们希望它与geom_line 具有单独的颜色,以显示在图例中。因此,我们 color映射到变量,即将color放在aes的{​​{1}}内。在这种情况下,我们不会将颜色映射到数据集中的变量,而是映射到常量。我们可以为常量命名,因此我们不需要在之后重命名图例条目。

geom_hline

enter image description here

现在出现两个图例,一个用于ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) + geom_line() + geom_point(size = 4) + geom_hline(aes(yintercept = 2.5, color = "int")) color aesgeom_line的主题,另一个用于geom_hline shape }秒。原因是&#34;变量&#34; geom_point映射到的现在包含三个级别:&#39; grp&#39;在原始数据中,加上级别&#39; int&#39;这是color geom_hline中引入的。因此,aes刻度中的级别与color刻度中的级别不同,默认情况下ggplot不能将两个刻度合并为一个图例。

如何结合两个传说?

一种可能性是通过使用带透明点的虚拟shape图层为shape引入与color相同的附加级别({{1} }} = 0)这样两个geom_point理论包含相同的级别:

alpha

enter image description here

另一种可能性是将原始分组变量转换为aes,并添加&#34; ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) + geom_line() + geom_point(size = 4) + geom_hline(aes(yintercept = 2.5, color = "int")) + geom_point(aes(shape = "int"), alpha = 0) # <~~~~ a blank geom_point 级别&#34;到原来的水平。然后使用factor中的geom_hline来包含&#34;未使用的因子级别&#34;:

drop = FALSE

然后,正如您所知,您可以将scale_shape_discrete功能用于&#34; datadf$grp <- factor(df$grp, levels = c(unique(df$grp), "int")) ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) + geom_line() + geom_point(size = 4) + geom_hline(aes(yintercept = 2.5, color = "int")) + scale_shape_discrete(drop = FALSE) &#34;图例中的guides override主题,并通过将shape条目设置为aes来删除geom_hline条目中的形状:

NA