向图例添加缩写 [英] Add abline to legend

查看:141
本文介绍了向图例添加缩写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对没有可复制数据的发布表示抱歉.希望你们能理解我的问题.这是我的代码.在代码末尾,我尝试添加abline.使用该代码,我试图将abline的名称添加到图例中,但它不起作用.

First, sorry for posting without reproducible data. Hope you guys understand my question. This is my code. At the end of the code, I am trying to add abline. With the code, I am trying to add the name of abline to the legend but it does not work.

ggplot(aes(x = week_id2, y = Index, color = Chain2, linetype = Chain2, group = Chain2), 
       data = data00 +
  geom_point(aes(shape=Chain2), size = 3) +  
  geom_line() + 
  scale_linetype_manual(values=c("twodash", "dashed", "dotted", "dotdash", "longdash")) + 
  scale_shape_manual(values=c(1:5)) +
  xlab("Week") + 
  ylab("Index") + 
  geom_hline(aes(yintercept=1)) 

如图所示,我仅在图例中添加缩写的名称(假设名称为"add").我应该如何使用当前代码执行此操作?

As shown, I just simply add a name of the abline (let's say the name is "add") in the legend. How should I do it with my current code?

推荐答案

您可以将colorlinetype添加到aes,然后使用scale_color_xxxscale_linetype_xxx来微调图例.这是使用economics数据集

You can add either color or linetype to aes then use scale_color_xxx or scale_linetype_xxx to fine tune the legend. Here is an example using economics dataset

library(tidyverse)

df <- economics %>%
  select(date, psavert, uempmed) %>%
  gather(key = "variable", value = "value", -date)

ggplot(df, aes(x = date, y = value)) + 
  geom_line(aes(color = variable), size = 1) + 
  geom_hline(aes(yintercept = 10, color = "My line")) +
  scale_color_brewer(palette = "Dark2", 
                     breaks = c("psavert", "uempmed", "My line")) +
  theme_minimal()

ggplot(df, aes(x = date, y = value)) + 
  geom_line(aes(color = variable, linetype = variable), size = 1) + 
  geom_hline(aes(yintercept = 10, color = "My line", linetype = "My line")) +
  scale_color_brewer(palette = "Dark2", 
                     breaks = c("psavert", "uempmed", "My line")) +
  scale_linetype_manual(values = c("twodash", "dashed", "dotted"),
                     breaks = c("psavert", "uempmed", "My line")) +
  theme_minimal()

编辑:根据OP的要求,我们将linetype和& color/shape传说

Edit: per OP's request, we separate linetype & color/shape legends

ggplot(df, aes(x = date, y = value)) + 
  geom_line(aes(color = variable), size = 0.75) + 
  geom_point(aes(color = variable, shape = variable)) +
  geom_hline(aes(yintercept = 10, linetype = "My line")) +
  scale_color_brewer(palette = "Dark2", 
                     breaks = c("psavert", "uempmed")) +
  scale_linetype_manual("", values = c("twodash"),
                        breaks = c("My line")) +
  scale_shape_manual(values = c(17, 19)) +
  # Set legend order
  guides(colour = guide_legend(order = 1), 
         shape = guide_legend(order = 1),
         linetype = guide_legend(order = 2)) + 
  theme_classic() +
  # Move legends closer to each other 
  theme(legend.title = element_blank(), 
        legend.justification = "center", 
        legend.spacing = unit(0.1, "cm"), 
        legend.spacing.y = unit(0.05, "cm"), 
        legend.margin = margin(0, 0, 0, 0), 
        legend.box.margin = margin(0, 0, 0, 0))

reprex软件包(v0.2.0)创建于2018-05-08.

Created on 2018-05-08 by the reprex package (v0.2.0).

这篇关于向图例添加缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆