首页 > 解决方案 > ggplot2:根据图形类型显示图例形状

问题描述

我有以下小标题数据:

d1<-structure(list(Date = structure(c(18413, 18414, 18415, 18416, 
  18417, 18418, 18419, 18420, 18421, 18422), class = "Date"), F = c(27240.15794, 
  28169.30859, 29175.23888, 30136.86232, 31124.0537, 32096.49071, 
  33077.44196, 34053.47994, 35032.35319, 36009.5903), L95 = c(27075.72202, 
  27869.77696, 28635.16841, 29389.18107, 30126.3899, 30842.16607, 
  31542.19984, 32223.65431, 32889.86495, 33539.92631), H95 = c(27404.59386, 
  28468.84022, 29715.30936, 30884.54358, 32121.71751, 33350.81536, 
  34612.68407, 35883.30556, 37174.84142, 38479.25429), A = c(27043L, 
  27762L, 28649L, 29359L, 29921L, 30644L, 31131L, 31848L, 32510L, 
  33140L)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
 "data.frame")) 

这是我的代码:

 colors <- c("A" = "black", "F" = "blue", "B" = "red")
 d1 %>% 
  ggplot(aes(x = Date, y = A, color="A")) +
  geom_point(shape=8,fill="blue", size=1.5) + 
  geom_line() + 
  geom_point(aes(x=Date, y = F,color="F"),shape=24,size=1.5) +
  geom_line() +
  geom_ribbon(aes(x = Date, y = F,ymax = L95,ymin = H95,color="B"),
              fill = "gray95", 
              alpha = 0.6,
              linetype=6,
              show.legend = TRUE)+
  theme_bw() +
  theme(legend.position = "bottom",
        #legend.box = "vertical", 
        legend.title=element_blank(),
        legend.box = NULL,
        legend.box.background = element_blank(),
        #legend.background = element_blank(),
        legend.key.size = unit(0.5, "cm"),
        legend.key.width = unit(0.5,"cm"),
        legend.direction = "horizontal",
        #legend.background = element_rect(color = "steelblue", linetype = "solid"),
        axis.text.x = element_text(color="#000000",
                                   size=10, 
                                   angle=45,
                                   hjust = 1),
        axis.text.y = element_text(color="#000000",
                                   size=10,
                                   angle=45,
                                   hjust = 1),
        axis.title.x =element_blank(),
        axis.title.y=element_text(size=14),
        panel.border = element_blank(), 
        #panel.grid.major = element_blank(),
        panel.background = element_rect(fill="#FFFFFF"),
        axis.line = element_line(colour = "black",
                                 size = 0.5, 
                                 linetype = "solid"),
        #legend.position = c(0, 160000),legend.justification = c(0, 1)
  ) + 
  
  #scale_shape_manual(values = colors)+
     scale_color_manual(values = colors)+
      scale_y_continuous("Test",expand = expansion(mult = c(0.01,0.03))) +
      scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week",
               date_labels = "%b/%d",
               expand = expansion(mult = c(0.01,0.03)))

输出:

在此处输入图像描述

我想将图例的形状与图中的形状相匹配,并去掉每个图例周围的小框。此外,由于某种原因,在我使用了一些推荐的图例自定义命令后,F数据的连接线变为仅点。

我从前面的问题中找到了一些融化数据的建议(使用reshape2包)。我确实尝试过,但我无法获得相同的带状图形效果,尤其是 L95 和 H95 之间的填充区域。因此,我排除了这个建议。另一个建议是创建一个形状列表作为与我的代码相同的变量(用于颜色),然后使用scale_shape_manual. 我试过了,但结果没有任何变化。我不确定我错过了什么。有什么建议么?

标签: rggplot2tidyverselegendribbon

解决方案


尝试将此数据重塑为 long 以避免潜在的陷阱:

library(dplyr)
library(tidyr)
library(ggplot2)
#Inputs
colors <- c("A" = "black", "F" = "blue", "B" = "red")
#Code
d1 %>%
  pivot_longer(-c(Date,L95,H95)) %>%
  ggplot(aes(x = Date, y = value, color=name)) +
  geom_point(aes(shape=name), size=1.5) + 
  geom_line()+
  geom_ribbon(aes(x = Date,ymax = L95,ymin = H95,color="B",shape='B'),
              fill = "gray95", 
              alpha = 0.6,
              linetype=6,
              show.legend = F)+
  scale_shape_manual('Var',values = c('A'=8,'F'=24,'B'=1))+
  scale_color_manual('Var',values = colors)+
  scale_y_continuous("Test",expand = expansion(mult = c(0.01,0.03))) +
  scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week",
               date_labels = "%b/%d",
               expand = expansion(mult = c(0.01,0.03)))+
  theme_bw() +
  theme(legend.position = "bottom",
        legend.title=element_blank(),
        legend.box = NULL,
        legend.box.background = element_blank(),
        legend.key.size = unit(0.5, "cm"),
        legend.key.width = unit(0.5,"cm"),
        legend.direction = "horizontal",
        axis.text.x = element_text(color="#000000",
                                   size=10, 
                                   angle=45,
                                   hjust = 1),
        axis.text.y = element_text(color="#000000",
                                   size=10,
                                   angle=45,
                                   hjust = 1),
        axis.title.x =element_blank(),
        axis.title.y=element_text(size=14),
        panel.border = element_blank(), 
        panel.background = element_rect(fill="#FFFFFF"),
        axis.line = element_line(colour = "black",
                                 size = 0.5, 
                                 linetype = "solid"),
        legend.key = element_rect(colour = NA, fill = NA)
  )+
  guides(color = guide_legend(override.aes = list(shape = c(8,NA,24))))

输出:

在此处输入图像描述


推荐阅读