我似乎无法让传奇工作。我试图改变数据格式,但让geom_ribbon复杂化了一些东西。 非常感谢任何帮助!
谢谢!
library(ggplot2)
Year <- c(2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012)
hi <- c(5.9, 5, 5, 4.9, 6.1, 5.9, 5.8, 6.2, 6.4)
lo <- c(4, 4, 3.8, 3.9, 4.5, 4.3, 4.1, 3.9, 3.7)
A <- c(4.4, 4.3, 4.7, 4.5, 5.5, 5.2, 5.4, 4.8, 4.4)
B <- c(4.7, 4.6, 4, 4.1, 4.7, 4.6, 4.4, 4.6, 4.8)
C <- c(0.61, 0.31, 0.13, 0.18, 0.24, 0.44, 0.32, 0.5, 0.4)
test <- data.frame(Year, hi, lo, A, B, C)
ggplot(test, aes(Year)) +
geom_ribbon(aes(ymin = lo , ymax = hi), fill = "grey90") +
geom_line(aes(y = A), colour= "black", size = 1) +
geom_line(aes(y = B), colour = "#55BF3B", size = 1) +
geom_line(aes(y = C), colour = "#f15c80", size = 1) +
ggthemes::theme_hc() +
scale_x_continuous(labels = abs, limits = c(2004, 2012), breaks = seq(2004, 2012, 4)) +
scale_colour_manual("", values = c("A" = "black", "B" = "#55BF3B", "C" = "#f15c80"))
答案 0 :(得分:1)
您可以尝试以下方法:
library(ggplot2)
library(dplyr)
library(tidyr)
# A named vector that represents the colors for each line (to be used in scale_color_manual)
cols <- c("A" = "black", "B" = "#55BF3B", "C" = "#f15c80")
test %>%
gather(key, value, -c(Year, hi, lo)) %>%
ggplot(aes(Year)) +
geom_ribbon(aes(ymin = lo, ymax = hi), fill = "grey90") +
geom_line(aes(y = value, group = key, color = key), size = 1) +
scale_colour_manual(values = cols) +
ggthemes::theme_hc()
该行:
cols <- c("A" = "black", "B" = "#55BF3B", "C" = "#f15c80")
不需要,因为ggplot2
会自动为每行提供自己的颜色,但如果您想手动设置它,您可以添加上面的行并包括:
scale_colour_manual(values = cols)
靠近ggplot2
链末端的地方。