首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在图例上制作虚线geom_vline

在图例上制作虚线geom_vline
EN

Stack Overflow用户
提问于 2022-05-12 12:25:05
回答 1查看 83关注 0票数 1

我想为一系列的情节创造一个传奇。我只需要图例,而不需要实际的图形,因为我正在分别创建它们,并且已经有了正确的格式。然后我将加入这个普遍的传说。

目前,我有两条不同颜色线的代码分开,但理想情况下,我会有一条红色直线和一条红色虚线。然而,我不能使线冲向其中一个,并有两个红色。目前代码有一个是红色的,另一个是紫色的。都是直线。

看上去是这样的:

代码语言:javascript
运行
复制
# libraries
library(readr)  # for function file.path
library(dplyr)
library(ggplot2)
library(tidyverse)
library(cowplot)
library(gridExtra)
library(extrafont)

legend <- data %>%
  ggplot(aes_string(x='DateTime', y='Rel_mAOD')) +
  geom_line() + 
  theme_classic() +
  geom_vline(aes(xintercept=as.POSIXct('2020-11-03 01:00:00'), 
                 col='red'), size=2) +
  geom_vline(aes(xintercept=as.POSIXct('2021-11-01 01:00:00'), 
                 col='purple'),linetype=2, size=2, showguide=TRUE) +
  scale_color_manual(
    name=NULL, 
    values=c('red','purple'), 
    labels=c('Release', 'Main')) +
  theme(legend.direction='horizontal',
        legend.position='top', 
        legend.text=element_text(size=30, margin=margin(r=1, unit='inch')),
        legend.spacing.x=unit(0.5, 'inch')) +
  guides(fill=guide_legend(
    keywidth=6, 
    keyheight=6, 
    default.unit='inch'))

my_legend<-get_legend(legend)
plot(my_legend)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-12 12:49:33

使用scale_linetype_manual,只需两行颜色为红色:

代码语言:javascript
运行
复制
  ggplot() +
  theme_void() +
  geom_vline(aes(xintercept=as.POSIXct('2020-11-03 01:00:00'), 
                 linetype = '1', ), size=1.5, color = "red") +
  geom_vline(aes(xintercept=as.POSIXct('2021-11-01 01:00:00'), 
                 linetype = '2'), size=1.5, color = "red") +
  scale_linetype_manual(name = NULL,
                        values = c(1, 2), labels = c('Release', 'Main')) +
  theme(legend.direction='horizontal',
        legend.position='top', 
        legend.text=element_text(size=40, margin=margin(r=1, unit='inch')),
        legend.spacing.x=unit(0.5, 'inch'),
        legend.box.margin = margin(50, 20, 50, 20))+
  guides(fill=guide_legend(
    keywidth=8, 
    keyheight=6, 
    default.unit='inch'))

附加标签:

代码语言:javascript
运行
复制
 ggplot() +
  theme_void() +
  geom_vline(aes(xintercept=as.POSIXct('2020-11-03 01:00:00'), 
                 linetype = '1', color = '1' ), size=1.5) +
  geom_vline(aes(xintercept=as.POSIXct('2021-11-01 01:00:00'), 
                 linetype = '2', color = '2'), size=1.5) +
  geom_vline(aes(xintercept=as.POSIXct('2020-11-03 01:00:00'), 
                 linetype = '3', color = '3'), size=1.5) +
  geom_vline(aes(xintercept=as.POSIXct('2021-11-01 01:00:00'), 
                 linetype = '4', color = '4'), size=1.5) +
  scale_color_manual(values = c("red", "red", "blue", "purple"),
                     name = NULL,
                     labels = c('Release', 'Main', "Next", "Last")) +
  scale_linetype_manual(name = NULL,
                        values = c(1, 2, 1, 1),
                        labels = c('Release', 'Main', "Next", "Last")) +
  theme(legend.direction='horizontal',
        legend.position='top', 
        legend.text=element_text(size=40, margin=margin(r=1, unit='inch')),
        legend.spacing.x=unit(0.5, 'inch'),
        legend.box.margin = margin(50, 20, 50, 20))

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72215564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档