Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据阴影颜色向R绘图添加图例_R_Plot_Ggplot2_Legend - Fatal编程技术网

如何根据阴影颜色向R绘图添加图例

如何根据阴影颜色向R绘图添加图例,r,plot,ggplot2,legend,R,Plot,Ggplot2,Legend,如何创建一个表示星期天为红色的自定义图例(在单词“星期一”旁边有一个红色框,星期二为蓝色?以下是我的MWE。它所需要的只是创建图例的代码 library(ggplot2) library(scales) set.seed(1) data <- data.frame(Date = seq(as.Date('2000-01-01'), len= 23, by="1 day"), Value = sample(1:50, 23, r

如何创建一个表示星期天为红色的自定义图例(在单词“星期一”旁边有一个红色框,星期二为蓝色?以下是我的MWE。它所需要的只是创建图例的代码

library(ggplot2)
library(scales)
set.seed(1)
data <- data.frame(Date = seq(as.Date('2000-01-01'), 
               len= 23, by="1 day"), 
               Value = sample(1:50, 23, replace=TRUE))
sunday <- data.frame(date = seq(as.Date('2000-01-01 12:00:00'), len = 4, by="7 days"))
monday <- data.frame(date = seq(as.Date('2000-01-02 12:00:00'), len = 4, by="7 days"))
ggplot() + geom_line(data = data, aes(x = Date, y = Value)) +
    geom_rect(data = sunday, aes(xmin = date-.5, xmax = date+.5, ymin = -Inf, ymax = Inf), alpha = 0.4, fill="red") +
    geom_rect(data = monday, aes(xmin = date-.5, xmax = date+.5, ymin = -Inf, ymax = Inf), alpha = 0.4, fill="blue") +
    scale_x_date(breaks = date_breaks("1 day"), labels = date_format("%d")) 
库(ggplot2)
图书馆(比例尺)
种子(1)

数据根据上面的评论,我这里有星期六和星期天。我结合了两个数据框,创建了一个包含日期的列(即星期六和星期天)。我在
geom\u rect
中使用此列填充
。这样,您可以看到一个图例

library(ggplot2)
library(scale)
library(dplyr)

set.seed(1)
data <- data.frame(Date = seq(as.Date('2000-01-01'), 
           len= 23, by="1 day"), 
           Value = sample(1:50, 23, replace=TRUE))

saturday <- data.frame(date = seq(as.Date('2000-01-01 12:00:00'), len = 4, by="7 days"))
sunday <- data.frame(date = seq(as.Date('2000-01-02 12:00:00'), len = 4, by="7 days"))

# Combine Saturday and Sunday data. If you use lubridate, there will be better way of
# doing this kind. But, for this demonstration, I chose this way.

combined <- mutate(rbind_list(saturday, sunday),
                   day = rep(c("Saturday", "Sunday"), each = 4))

ggplot() + geom_line(data = data, aes(x = Date, y = Value)) +
geom_rect(data = combined, aes(xmin = date-.5, xmax = date+.5,
          ymin = -Inf, ymax = Inf, fill = day), alpha = 0.4) +
scale_fill_manual(values = c("red", "blue")) +
scale_x_date(breaks = date_breaks("1 day"), labels = date_format("%d")) 
库(ggplot2)
图书馆(比例尺)
图书馆(dplyr)
种子(1)

数据根据上面的评论,我这里有星期六和星期天。我结合了两个数据框,创建了一个包含日期的列(即星期六和星期天)。我在
geom\u rect
中使用此列填充
。这样,您可以看到一个图例

library(ggplot2)
library(scale)
library(dplyr)

set.seed(1)
data <- data.frame(Date = seq(as.Date('2000-01-01'), 
           len= 23, by="1 day"), 
           Value = sample(1:50, 23, replace=TRUE))

saturday <- data.frame(date = seq(as.Date('2000-01-01 12:00:00'), len = 4, by="7 days"))
sunday <- data.frame(date = seq(as.Date('2000-01-02 12:00:00'), len = 4, by="7 days"))

# Combine Saturday and Sunday data. If you use lubridate, there will be better way of
# doing this kind. But, for this demonstration, I chose this way.

combined <- mutate(rbind_list(saturday, sunday),
                   day = rep(c("Saturday", "Sunday"), each = 4))

ggplot() + geom_line(data = data, aes(x = Date, y = Value)) +
geom_rect(data = combined, aes(xmin = date-.5, xmax = date+.5,
          ymin = -Inf, ymax = Inf, fill = day), alpha = 0.4) +
scale_fill_manual(values = c("red", "blue")) +
scale_x_date(breaks = date_breaks("1 day"), labels = date_format("%d")) 
库(ggplot2)
图书馆(比例尺)
图书馆(dplyr)
种子(1)

数据根据上面的评论,我这里有星期六和星期天。我结合了两个数据框,创建了一个包含日期的列(即星期六和星期天)。我在
geom\u rect
中使用此列填充
。这样,您可以看到一个图例

library(ggplot2)
library(scale)
library(dplyr)

set.seed(1)
data <- data.frame(Date = seq(as.Date('2000-01-01'), 
           len= 23, by="1 day"), 
           Value = sample(1:50, 23, replace=TRUE))

saturday <- data.frame(date = seq(as.Date('2000-01-01 12:00:00'), len = 4, by="7 days"))
sunday <- data.frame(date = seq(as.Date('2000-01-02 12:00:00'), len = 4, by="7 days"))

# Combine Saturday and Sunday data. If you use lubridate, there will be better way of
# doing this kind. But, for this demonstration, I chose this way.

combined <- mutate(rbind_list(saturday, sunday),
                   day = rep(c("Saturday", "Sunday"), each = 4))

ggplot() + geom_line(data = data, aes(x = Date, y = Value)) +
geom_rect(data = combined, aes(xmin = date-.5, xmax = date+.5,
          ymin = -Inf, ymax = Inf, fill = day), alpha = 0.4) +
scale_fill_manual(values = c("red", "blue")) +
scale_x_date(breaks = date_breaks("1 day"), labels = date_format("%d")) 
库(ggplot2)
图书馆(比例尺)
图书馆(dplyr)
种子(1)

数据根据上面的评论,我这里有星期六和星期天。我结合了两个数据框,创建了一个包含日期的列(即星期六和星期天)。我在
geom\u rect
中使用此列填充
。这样,您可以看到一个图例

library(ggplot2)
library(scale)
library(dplyr)

set.seed(1)
data <- data.frame(Date = seq(as.Date('2000-01-01'), 
           len= 23, by="1 day"), 
           Value = sample(1:50, 23, replace=TRUE))

saturday <- data.frame(date = seq(as.Date('2000-01-01 12:00:00'), len = 4, by="7 days"))
sunday <- data.frame(date = seq(as.Date('2000-01-02 12:00:00'), len = 4, by="7 days"))

# Combine Saturday and Sunday data. If you use lubridate, there will be better way of
# doing this kind. But, for this demonstration, I chose this way.

combined <- mutate(rbind_list(saturday, sunday),
                   day = rep(c("Saturday", "Sunday"), each = 4))

ggplot() + geom_line(data = data, aes(x = Date, y = Value)) +
geom_rect(data = combined, aes(xmin = date-.5, xmax = date+.5,
          ymin = -Inf, ymax = Inf, fill = day), alpha = 0.4) +
scale_fill_manual(values = c("red", "blue")) +
scale_x_date(breaks = date_breaks("1 day"), labels = date_format("%d")) 
库(ggplot2)
图书馆(比例尺)
图书馆(dplyr)
种子(1)

使用“图例”的数据设施没有真正的意义。你需要建立一个反映这些矩形颜色的注释结构。我有点困惑。2000-01-01实际上是星期六。我遗漏了什么吗?是的,我的情节在这方面是不正确的。我只是把它作为一个假例子放在一起,以说明真正的问题是什么-如何获得一个图例(或注释结构可能也适用)。使用“图例”设施没有真正的意义。你需要建立一个反映这些矩形颜色的注释结构。我有点困惑。2000-01-01实际上是星期六。我遗漏了什么吗?是的,我的情节在这方面是不正确的。我只是把它作为一个假例子放在一起,以说明真正的问题是什么-如何获得一个图例(或注释结构可能也适用)。使用“图例”设施没有真正的意义。你需要建立一个反映这些矩形颜色的注释结构。我有点困惑。2000-01-01实际上是星期六。我遗漏了什么吗?是的,我的情节在这方面是不正确的。我只是把它作为一个假例子放在一起,以说明真正的问题是什么-如何获得一个图例(或注释结构可能也适用)。使用“图例”设施没有真正的意义。你需要建立一个反映这些矩形颜色的注释结构。我有点困惑。2000-01-01实际上是星期六。我遗漏了什么吗?是的,我的情节在这方面是不正确的。我只是把它作为一个假例子放在一起,以说明真正的问题是什么-如何获得一个图例(或注释结构可能也适用)。像符咒一样有效。非常感谢。@bill999愉悦,伙计。:)像符咒一样有效。非常感谢。@999很高兴,伙计。:)工作起来很有魅力。非常感谢。@999很高兴,伙计。:)工作起来很有魅力。非常感谢。@999很高兴,伙计。:)