首页 > 解决方案 > 使用带有`geom_line()`的不同行并在`aes()`之外指示特定颜色时如何获取图例?

问题描述

我在一个图中绘制了四列与时间的关系。这些列中的每一列都被绘制为不同的Y变量,而时间在X轴上。下面我分享一个假示例数据框:

  df <- data.frame(date = as.Date(c("2020-08-05","2020-08-06","2020-08-07","2020-08-08","2020-08-09","2020-08-10","2020-08-11","2020-08-12")),
                   State.1_day=c(0.8,0.3,0.2,0.5,0.6,0.7,0.8,0.7),
                   State.2_day=c(0.4,0.2,0.1,0.2,0.3,0.4,0.5,0.6),
                   State.1_night=c(0.7,0.8,0.5,0.4,0.3,0.2,0.3,0.2),
                   State.2_night=c(0.5,0.6,0.7,0.4,0.3,0.5,0.6,0.7))
  
  df

        date State.1_day State.2_day State.1_night State.2_night
1 2020-08-05         0.8         0.4           0.7           0.5
2 2020-08-06         0.3         0.2           0.8           0.6
3 2020-08-07         0.2         0.1           0.5           0.7
4 2020-08-08         0.5         0.2           0.4           0.4
5 2020-08-09         0.6         0.3           0.3           0.3
6 2020-08-10         0.7         0.4           0.2           0.5
7 2020-08-11         0.8         0.5           0.3           0.6
8 2020-08-12         0.7         0.6           0.2           0.7

library(RColorBrewer)

line_colors_a <- RColorBrewer::brewer.pal(6, "Blues")[c(3,6)]
  line_colors_a
  
  line_colors_b <- RColorBrewer::brewer.pal(6, "Greens")[c(3,6)]
  line_colors_b
  
  line_colors <- c(line_colors_a,line_colors_b)

 Plot <- ggplot() +
    geom_line(data=df,aes(y = State.1_day, x=date), colour = line_colors[1] ,size=1) +
    geom_line(data=df,aes(y = State.1_night, x=date), colour = line_colors[2],size=1) +
    geom_line(data=df,aes(y = State.2_day, x=date), colour = line_colors[3],size=1) +
    geom_line(data=df,aes(y = State.2_night, x=date), colour = line_colors[4],size=1) +
    scale_x_date(labels = date_format("%Y-%m-%d")) +
    scale_color_discrete(name = "States", labels = c("Active_day", "Active_night", "Resting_day", "Resting_night")) +
    theme_bw() +
    labs(y = "% time", x = "Date") +
    theme(strip.text = element_text(face="bold", size=18),
          strip.background=element_rect(fill="white", colour="black",size=2),
          axis.title.x =element_text(margin = margin(t = 10, r = 0, b = 0, l = 0),size = 20),
          axis.title.y =element_text(margin = margin(t = 0, r = 10, b = 0, l = 0),size = 20),
          axis.text.x = element_text(angle = 70, hjust = 1,size = 15),
          axis.text.y = element_text(angle = 0, hjust = 0.5,size = 15),
          axis.line = element_line(),
          panel.grid.major= element_blank(),
          panel.grid.minor = element_blank(),
          legend.text=element_text(size=18),
          legend.title = element_text(size=19, face = "bold"),
          legend.key=element_blank(),
          legend.position = "top",
          panel.border = element_blank(),
          strip.placement = "outside")
  Plot
  

在此处输入图像描述

我非常接近我需要的东西,但是,我不知道情节中显示了图例。colour我在外面使用参数是aes()因为如果我在里面使用它aes(),颜色不是我想要的。我试图使用它来获取它scale_color_discrete(),但是,我没有得到任何传说。

我怎样才能得到一个图例,指示哪个变量对应于这些行中的每一行?

提前致谢!!

标签: rggplot2

解决方案


同时解决图例和调色板问题。首先,您可以使用 将数据框转换为长格式pivot_longer(),然后添加一列,使用关联变量指定您想要的颜色。您可以使用 映射这些颜色scale_colour_manual()。不是最优雅的解决方案,但我发现它在处理手动设置的调色板时很有用。

library(ggplot2)
library(dplyr)
library(tidyr)
library(tibble)

df <- data.frame(date = as.Date(c("2020-08-05","2020-08-06","2020-08-07","2020-08-08","2020-08-09","2020-08-10","2020-08-11","2020-08-12")),
                 State.1_day=c(0.8,0.3,0.2,0.5,0.6,0.7,0.8,0.7),
                 State.2_day=c(0.4,0.2,0.1,0.2,0.3,0.4,0.5,0.6),
                 State.1_night=c(0.7,0.8,0.5,0.4,0.3,0.2,0.3,0.2),
                 State.2_night=c(0.5,0.6,0.7,0.4,0.3,0.5,0.6,0.7))

line_colors_a <- RColorBrewer::brewer.pal(6, "Blues")[c(3,6)]
line_colors_a

line_colors_b <- RColorBrewer::brewer.pal(6, "Greens")[c(3,6)]
line_colors_b

line_colors <- c(line_colors_a,line_colors_b)

df1 <- df %>% 
  pivot_longer(-date) %>% 
  mutate(colour = case_when(
    name == "State.1_day" ~ line_colors[1],
    name == "State.1_night" ~ line_colors[2],
    name == "State.2_day" ~ line_colors[3],
    name == "State.2_night" ~ line_colors[4]
  ))

ggplot(df1, aes(x = date, y = value, colour = name)) +
  geom_line(size = 1) +
  scale_x_date(date_labels = "%Y-%m-%d") +
  scale_colour_manual(values = tibble::deframe(distinct(df1, colour, name))) +
  theme_bw() +
  labs(y = "% time", x = "Date") +
  theme(strip.text = element_text(face="bold", size=18),
        strip.background=element_rect(fill="white", colour="black",size=2),
        axis.title.x =element_text(margin = margin(t = 10, r = 0, b = 0, l = 0),size = 20),
        axis.title.y =element_text(margin = margin(t = 0, r = 10, b = 0, l = 0),size = 20),
        axis.text.x = element_text(angle = 70, hjust = 1,size = 15),
        axis.text.y = element_text(angle = 0, hjust = 0.5,size = 15),
        axis.line = element_line(),
        panel.grid.major= element_blank(),
        panel.grid.minor = element_blank(),
        legend.text=element_text(size=18),
        legend.title = element_text(size=19, face = "bold"),
        legend.key=element_blank(),
        legend.position = "top",
        panel.border = element_blank(),
        strip.placement = "outside")

在此处输入图像描述


推荐阅读