ggplot2覆盖两个图,一个图例带有手动颜色选择

时间:2018-07-05 22:27:14

标签: r ggplot2

我有以下代码:

simulated.txt

Date        1        2          3
01-01-1992  1.856    2.889      2.365
02-01-1993  1.523    3.536      4.624
03-01-1994  2.536    2.452      6.352    
04-01-1902  3.486    4.267      3.685


File1.txt

Date        FLOW     Modelled
01-01-1992  1.856    NA
02-01-1992  1.523    NA
03-01-1992  2.623    NA    
04-01-1992  3.679    NA

File2.txt

Date        FLOW     Modelled
01-01-1993  11.856    NA
02-01-1993  11.523    NA
03-01-1993  12.623    NA    
04-01-1993  13.679    NA

File3.txt

Date        FLOW     Modelled
01-01-1994  111.856    NA
02-01-1994  111.523    NA
03-01-1994  112.623    NA    
04-01-1994  113.679    NA

这将导致以下绘图:Plot

enter image description here

现在如何更改条形的颜色?我知道当我从es中移出填充时,它将使我定义一种颜色,但是随后我松开了图例。...

1 个答案:

答案 0 :(得分:1)

我建议使用一种更类似于ggplot的方法:合并并整理两个data.frame,然后将一个data.frame传递给ggplot,并使用美学将组映射到视觉特性。

library(tidyverse)
full_join(
    FXX_03_Data_8To11_byAgeRange, FXX_03_Data_byAgeRange,
    by = "F20_AgeRange",
    suffix = c(".8To11", ".accumulated")) %>%
    gather(Group, Prozent, starts_with("Prozent")) %>%
    mutate(Group = sub("Prozent\\.", "", Group)) %>%
    ggplot(aes(F20_AgeRange, y = Prozent, fill = Group)) +
    geom_bar(stat = "identity", position = "dodge2") +
    scale_fill_manual(values = c("8To11" = "red", "accumulated" = "blue"))

enter image description here