图例更改为geom_bar指定的颜色

时间:2018-07-20 23:09:27

标签: r ggplot2 legend geom-bar

我有以下绘图,其中包含来自两个不同数据帧的数据:

RecyclerView

initial plot

我在下面的代码中添加了图例,但此刻,我失去了填满geom_bar()条形的原始颜色。

ggplot(one100, aes(x=hour, y=countPerHour)) +
  ylab("Times reached") +
  xlab("Hour of the day") +
  geom_bar( stat="identity",
            color="turquoise3", fill="turquoise2", alpha=0.3) +
  geom_bar( stat="identity", 
            aes(x=noventa99$hour, y=noventa99$countPerHour), 
            color="green3", fill="green3", alpha=0.3) +
  theme_bw()

这是我得到的结果:

with lengend

one100数据帧如下,其中“小时”是一个因素

ggplot(one100 ) +
  ylab("Times reached") +
  xlab("Hour of the day") +
  geom_bar( stat="identity",
            aes(x=one100$hour, y=one100$countPerHour, colour="100%"), 
            alpha=0.3) +
  geom_bar( stat="identity", 
        aes(x=noventa99$hour, y=noventa99$countPerHour, colour="80-99%"), 
        alpha=0.3) +
  theme_bw() +
  scale_colour_manual("Critical % Occupation", 
                      values=c("100%" = "turquoise2", "80-90%" = "green3"))

noventa99包含以下数据:

"hour"  "countPerHour"
"9"     30
"11"    24
 "8"    21
"10"    18
"13"    17
"12"    15
 "7"    15
"14"    14
 "5"    12
"17"    7
"15"    7
"16"    6
 "4"    6
 "6"    5
"21"    4
"20"    3
"19"    2
 "3"    2
 "2"    2
"23"    1
"22"    1
"18"    1
 "0"    1
 "1"    0

如何保持指定的原始颜色填充条形图?

1 个答案:

答案 0 :(得分:3)

问题仅是因为您要将值传递给color,后者控制条形的轮廓,而不是fill。就是说,生成图例的最佳方法是合并数据,以便您可以将变量映射到这些外观,而不是硬编码的值:

library(ggplot2)

one100 <- data.frame(hour = c(9L, 11L, 8L, 10L, 13L, 12L, 7L, 14L, 5L, 17L, 15L, 16L, 4L, 6L, 21L, 20L, 19L, 3L, 2L, 23L, 22L, 18L, 0L, 1L), 
                     countPerHour = c(30L, 24L, 21L, 18L, 17L, 15L, 15L, 14L, 12L, 7L, 7L, 6L, 6L, 5L, 4L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 0L))
noventa99 <- data.frame(hour = c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 21L, 22L, 23L, 20L, 2L, 0L), 
                        countPerHour = c(1L, 2L, 1L, 3L, 1L, 6L, 5L, 7L, 6L, 3L, 4L, 4L, 5L, 4L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L))

# rbind and add ID column for which data frame it came from
combined_data <- dplyr::bind_rows(`100%` = one100, `80-90%` = noventa99, .id = "critical")

str(combined_data)
#> 'data.frame':    48 obs. of  3 variables:
#>  $ critical    : chr  "100%" "100%" "100%" "100%" ...
#>  $ hour        : int  9 11 8 10 13 12 7 14 5 17 ...
#>  $ countPerHour: int  30 24 21 18 17 15 15 14 12 7 ...

# set aes once with fill and color, without `$` subsetting
ggplot(combined_data, aes(hour, countPerHour, fill = critical, color = critical)) + 
    geom_col(alpha = 0.3) +    # geom_col is short for `geom_bar(stat = "identity)
    # set scale for fill and color aesthetics
    scale_fill_manual("Critical % Occupation", values = c("100%" = "turquoise2", "80-90%" = "green3")) + 
    scale_color_manual("Critical % Occupation", values = c("100%" = "turquoise2", "80-90%" = "green3")) + 
    labs(x = "Hour of the day", y = "Times reached") +    # set multiple labels at once
    hrbrthemes::theme_ipsum_rc()    # pretty theme

barplot with correct fill colors