ggplot 自定义图例而不是默认图例

ggplot custom legend instead of default

我搜索并尝试了一系列建议,以便能够在分组散点图 ggplot 中显示自定义图例而不是默认图例。我试过 this and this and following this 等等。

例如,假设我有这样一个 df

df = data.frame(id = c("A", "A", "B", "C", "C", "C"), 
                value = c(1,2,1,2,3,4), 
                ref = c(1.5, 1.5, 1, 2,2,2), 
                min = c(0.5, 0.5, 1,2,2,2))

并且我想将每个 idvalue 显示为圆点,而且还为每个 reference 值和 minimum 值显示 id作为不同形状的点,如下:

p = ggplot(data = df) +
  geom_point(aes(x = id, y = value, color = factor(id)), shape = 19, size = 6) +
  geom_point(aes(x = id, y = ref, color = factor(id)), shape = 0, size = 8) +
  geom_point(aes(x = id, y = min, color = factor(id)), shape = 2, size = 8) +
  xlab("") +
  ylab("Value")
#print(p) 

现在一切都很好,但我的图例并没有为情节的解释添加任何内容,因为X轴和颜色足以理解它。我知道我可以通过 theme(legend.position = "none") 删除图例。 相反,我想要一个图例,说明每个点的实际 形状 代表什么(例如,实心圆点 = value、三角形 = min、正方形= ref).

在尝试通过 scale_fill_manual 和类似的方法手动设置比例值时

override.shape = shapes$shape
override.linetype = shapes$pch
guides(colour = guide_legend(override.aes = list(shape = override.shape, linetype = override.linetype)))...
....

我也试过制作第二个情节,但没有显示它,使用上面粘贴的链接之一中建议的内容:

shapes  = data.frame(shape = c("value", "reference", "minimum"), pch = c(19,0,2), col = c("gray", "gray", "gray"))
p2 = ggplot(shapes, aes(shape, pch)) + geom_point()  
#print(p2)

g_legend <- function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)
}
legend <- g_legend(p2)
library(gridExtra)
pp <- arrangeGrob(p1 ,legend,
                  widths=c(5/4, 1/4),
                  ncol = 2)

但是我得到了错误:

> legend <- g_legend(p2)
Error in tmp$grobs[[leg]] : 
  attempt to select less than one element in get1index

我没有找到可行的解决方案.. 所以是的.. 欢迎任何关于我如何只能显示与不同点形状相关的图例的建议。 谢谢

您可以使用 scale_shape_manual 手动构建形状图例:

library(ggplot2)

ggplot(data = df) +
  geom_point(aes(x = id, y = value, color = factor(id), shape = 'value'), size = 6) +
  geom_point(aes(x = id, y = ref, color = factor(id), shape = 'ref'), size = 8) +
  geom_point(aes(x = id, y = min, color = factor(id), shape = 'min'), size = 8) +
  scale_shape_manual(values = c('value' = 19, 'ref' = 0, 'min' = 2)) +
  xlab("") +
  ylab("Value")

reprex package (v0.3.0)

于 2020-04-15 创建

但更好的方法是将 df 重塑为长格式,并将每个 aes 映射到一个变量:

library(dplyr)
library(tidyr)

df %>% 
  pivot_longer(-id) %>% 
  ggplot() +
  geom_point(aes(x = id, y = value, color = factor(id), shape = name, size = name)) +
  scale_shape_manual(values = c('value' = 19, 'ref' = 0, 'min' = 2)) +
  scale_size_manual(values = c('value' = 6, 'ref' = 8, 'min' = 8)) + 
  xlab("") +
  ylab("Value")

reprex package (v0.3.0)

于 2020-04-15 创建

要删除颜色的图例,请使用 guide_none()

library(tidyr)
library(ggplot2)
df %>% 
  pivot_longer(-id) %>% 
  ggplot() +
  geom_point(aes(x = id, y = value, color = factor(id), shape = name, size = name)) +
  scale_shape_manual(values = c('value' = 19, 'ref' = 0, 'min' = 2)) +
  scale_size_manual(values = c('value' = 6, 'ref' = 8, 'min' = 8)) + 
  guides(color = guide_none()) +
  xlab("") +
  ylab("Value")

reprex package (v0.3.0)

于 2020-04-16 创建

数据:

df = data.frame(id = c("A", "A", "B", "C", "C", "C"), 
                value = c(1,2,1,2,3,4), 
                ref = c(1.5, 1.5, 1, 2,2,2), 
                min = c(0.5, 0.5, 1,2,2,2))

您可以先使用 tidyr 整理数据,然后将 aes shape 映射到新变量

library(tidyr)
df2 <- pivot_longer(df, -id)

ggplot(data = df2) +
  geom_point(aes(x = id, y = value, shape = name), size = 6) +
  xlab("") +
  ylab("Value")