繁体   English   中英

将 2 条 vlines 添加到 ggplot,并为这些线添加一个自定义图例

[英]Adding 2 vlines to a ggplot, with an additional custom legend for the lines

我正在尝试制作一个带有两条垂直线的 ggplot,并使用单独的自定义图例来解释这些线代表什么。 这是我的代码(使用 iris):

irate <- as.data.frame(iris)
irate$Species <- as.character(irate$Species)

irritating <- ggplot(irate) +
  geom_line(aes(y = Sepal.Length, x = Sepal.Width), color = "blue") +
  geom_point(aes(y = Sepal.Length, x = Sepal.Width, color = Species), size = 5) +
  theme(legend.position = "right", axis.text.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank(), panel.grid.major.y = element_blank())+
  labs(title = "The chart", x = "Sepal Width") +
  geom_vline(color = "black", linetype = "dashed", aes(xintercept = 3))+
  geom_vline(color = "purple", linetype = "dashed", aes(xintercept = 4))

irritating 

结果

我尝试过使用 scale_color_manual(等)之类的东西,但出于某种原因,这样做会干扰主要图例,而不会产生单独的图例。

使用以下问题的答案: 将图例添加到 geom_vline

我添加: +scale_color_manual(name = "still problematic", values = c("black", "purple", "red"))

在向量中添加“红色”是让它生成图表的唯一方法(否则会出现:“手动比例中的值不足。需要 3 个,但只提供了 2 个。”错误)。 在此处输入图像描述

实现您想要的结果的一种选择是使用不同的美学来为您的 vlines 创建 colro 图例。 在我下面的代码中,我在l.netype aes 上使用 map 并使用 guide_legend 的override.aes参数来分配正确的guide_legend

irate <- as.data.frame(iris)
irate$Species <- as.character(irate$Species)

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.2

base <- ggplot(irate) +
  geom_line(aes(y = Sepal.Length, x = Sepal.Width), color = "white") +
  geom_point(aes(y = Sepal.Length, x = Sepal.Width, color = Species), size = 5) +
  theme(legend.position = "right", axis.text.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank(), panel.grid.major.y = element_blank())+
  labs(title = "The chart", x = "Sepal Width") 

base +
  geom_vline(color = "black", aes(xintercept = 3, linetype = "Black Line"))+
  geom_vline(color = "purple", aes(xintercept = 4, linetype  = "Purple line")) +
  scale_linetype_manual(name = "still problematic", values = c("dashed", "dashed")) +
  guides(linetype = guide_legend(override.aes = list(color = c("black", "purple"))))

第二个也许更清洁的解决方案是使用ggnewscale package 允许具有相同美学的多个图例:


library(ggnewscale)

base +
  new_scale_color() +
  geom_vline(linetype = "dashed", aes(xintercept = 3, color = "Black Line"))+
  geom_vline(linetype = "dashed", aes(xintercept = 4, color  = "Purple line")) +
  scale_color_manual(name = "still problematic", values = c("black", "purple"))

这是 package ggnewscale的一种方法,可以非常轻松地为两种color映射绘制两个图例。
主要技巧是使用 x 截距值和 colors 创建一个 data.frame,然后将此数据集分配给geom_vlinedata参数。 如果在new_scale_color()之后运行,则 colors 将是正确的。

library(ggplot2)
library(ggnewscale)

irate <- iris
irate$Species <- as.character(irate$Species)
happy <- data.frame(xintercept = c(3, 4), color = c("black", "purple"))

delightful <- ggplot(irate) +
  geom_line(aes(y = Sepal.Length, x = Sepal.Width), color = "blue") +
  geom_point(aes(y = Sepal.Length, x = Sepal.Width, color = Species), size = 5) +
  theme(legend.position = "right", axis.text.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank(), panel.grid.major.y = element_blank())+
  labs(title = "The chart", x = "Sepal Width") +
  new_scale_color() +
  geom_vline(
    data = happy,
    mapping = aes(xintercept = xintercept, color = color),
    linetype = "dashed"
  ) +
  scale_color_manual(values = c(black = "black", purple = "purple"))

delightful 

创建于 2022-11-30,使用reprex v2.0.2

aes中使用l.netype将这些部分放入图例中,然后您可以覆盖指南显示颜色:

library(ggplot2)

irate <- as.data.frame(iris)
irate$Species <- as.character(irate$Species)

irritating <- ggplot(irate) +
  geom_line(aes(y = Sepal.Length, x = Sepal.Width), color = "white") +
  geom_point(aes(y = Sepal.Length, x = Sepal.Width, color = Species), size = 5) +
  theme(
    legend.position = "right",
    axis.text.y = element_blank(),
    axis.title.y = element_blank(),
    axis.ticks.y = element_blank(),
    panel.grid.major.y = element_blank()
  ) +
  labs(title = "The chart", x = "Sepal Width") +
  geom_vline(linewidth = 1.5,
             color = "black",
             aes(xintercept = 3, linetype = "Something")) +
  geom_vline(linewidth = 1.5,
             color = "purple",
             aes(xintercept = 4, linetype = "Another thing")) +
  scale_linetype_manual(
    "Things",
    values = c("dashed", "dashed"),
    guide = guide_legend(override.aes = list(colour = c("purple", "black")))
  )

irritating

暂无
暂无

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

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