删除图例符号的边框 [英] Removing the border of legend symbol

查看:277
本文介绍了删除图例符号的边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #一些随机数据$ b $<  -  seq(1:10)
y_pred < - runif(10,min = -10,max = 10)
y_obs <-y_pred + rnorm(10)
#伪造CI
Lo.95< - y_pred - 1.96
Hi.95< - y_pred + 1.96
my_df< - data.frame(x,y_pred,y_obs,Lo .95,Hi.95)

ggplot(my_df,aes(x = x,y = y_pred))+
geom_line(aes(color =预测数据),size = 1.2 )+
geom_point(aes(x = x,y = y_obs,color =Actual Data),size = 3)+
geom_ribbon(aes(ymin = Lo.95,ymax = Hi.95) ,x = x,linetype = NA,color =Confidence Interval),alpha = 0.2)+
theme_grey()+
scale_colour_manual(
values = c(gray30,blue ,red),
guide = guide_legend(override.aes = list(
border = c(NA,NA,NA),
fill = c(gray30,white ,white),
linetype = c(blank,blank,solid),
shape = c(N A,19,NA))))

该图如下所示:





我对这幅图的唯一问题是围绕该行的图例项目符号(即预测数据)。有没有什么办法可以在不破坏剩余的情节的情况下将其删除?

解决方案

我认为 geom_ribbon 是问题所在。如果我们把它的颜色& fill 出于 aes ,一切看起来都很好

library(ggplot2)

#一些随机数据
x< - seq(1: 10)
_pred < - runif(10,min = -10,max = 10)
y_obs <-y_pred + rnorm(10)
#伪造CI
Lo .95 < - y_pred - 1.96
Hi.95 < - y_pred + 1.96
my_df< - data.frame(x,y_pred,y_obs,Lo.95,Hi.95)

m1 < - ggplot(my_df,aes(x = x,y = y_pred))+
geom_point(aes(x = x,y = y_obs,color =Actual),size = 3)+
geom_line(aes(color =预测),size = 1.2)+
geom_ribbon(aes(x = x,ymin = Lo.95,ymax = Hi.95),
fill =grey30,alpha = 0.2)+
scale_color_manual(Legend,
values = c(blue,red),
labels = c(Actual ,预测))+
指南(color = guide_legend(
order = 1,
override.aes = list(
color = c(blue,red),
fill = c(white,white),
linetype = c(blank,solid),
形状= c(19,NA))))+
theme_bw()+
#删除图例关键边框颜色&背景
主题(legend.key = element_rect(color = NA,fill = NA),
legend.box.background = element_blank())
m1



当我们离开 aes 中的置信区间时,我们不再有它的图例。一种解决方法是创建一个看不见的点,并将一个未使用的 geom 手动创建图例键。在这里,我们可以使用 size / shape (归功于此(v0.2.0)。


I was trying to plot some predicted vs. actual data, something that resembles the following:

# Some random data
x <- seq(1: 10)
y_pred <- runif(10, min = -10, max = 10)
y_obs <- y_pred + rnorm(10)
# Faking a CI
Lo.95 <- y_pred - 1.96
Hi.95 <- y_pred + 1.96
my_df <- data.frame(x, y_pred, y_obs, Lo.95, Hi.95)

ggplot(my_df, aes(x = x, y = y_pred)) +
  geom_line(aes(colour = "Forecasted Data"), size = 1.2) +
  geom_point(aes(x = x, y = y_obs, colour = "Actual Data"), size = 3) +
  geom_ribbon(aes(ymin=Lo.95, ymax=Hi.95, x=x, linetype = NA,  colour = "Confidence Interval"), alpha=0.2) +
  theme_grey() +
  scale_colour_manual(
    values = c("gray30", "blue", "red"),
    guide = guide_legend(override.aes = list(
      border=c(NA, NA, NA), 
      fill=c("gray30", "white", "white"),
      linetype = c("blank", "blank", "solid"),
      shape = c(NA, 19, NA)))) 

The plot looks like this:

The only issue I have with this plot is the red border surrounding the legend item symbol for the line (i.e. the forecasted data). Is there any way I can remove it without breaking the rest of my plot?

解决方案

I think geom_ribbon was the problem. If we take its color & fill out of aes, everything looks fine

library(ggplot2)

# Some random data
x <- seq(1: 10)
y_pred <- runif(10, min = -10, max = 10)
y_obs <- y_pred + rnorm(10)
# Faking a CI
Lo.95 <- y_pred - 1.96
Hi.95 <- y_pred + 1.96
my_df <- data.frame(x, y_pred, y_obs, Lo.95, Hi.95)

m1 <- ggplot(my_df, aes(x = x, y = y_pred)) +
  geom_point(aes(x = x, y = y_obs, colour = "Actual"), size = 3) +
  geom_line(aes(colour = "Forecasted"), size = 1.2) +
  geom_ribbon(aes(x = x, ymin = Lo.95, ymax = Hi.95), 
              fill = "grey30", alpha = 0.2) +
  scale_color_manual("Legend", 
                     values = c("blue", "red"),
                     labels = c("Actual", "Forecasted")) +
  guides( color = guide_legend(
    order = 1,
    override.aes = list(
                        color = c("blue", "red"),
                        fill  = c("white", "white"),
                        linetype = c("blank", "solid"),
                        shape = c(19, NA)))) +
  theme_bw() +
  # remove legend key border color & background
  theme(legend.key = element_rect(colour = NA, fill = NA),
    legend.box.background = element_blank())
m1

As we leave Confidence Interval out of aes, we no longer have its legend. One workaround is to create an invisible point and take one unused geom to manually create a legend key. Here we can use size/shape (credit to this answer)

m2 <- m1 +
  geom_point(aes(x = x, y = y_obs, size = "Confidence Interval", shape = NA)) +
  guides(size = guide_legend(NULL, 
                             order = 2,
                             override.aes = list(shape = 15, 
                                                 color = "lightgrey",
                                                 size = 6))) +
  # Move legends closer to each other
  theme(legend.title = element_blank(),
        legend.justification = "center",
        legend.spacing.y = unit(0.05, "cm"),
        legend.margin = margin(0, 0, 0, 0),
        legend.box.margin = margin(0, 0, 0, 0)) 
m2

Created on 2018-03-19 by the reprex package (v0.2.0).

这篇关于删除图例符号的边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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