ggplot2:如何为单个值设置颜色,而其他设置为自动颜色

时间:2018-11-30 10:54:00

标签: r ggplot2

我使用由ggplot2软件包制作的折线图。现在的问题是:如何为单个值定义颜色,而将其余颜色保留为“自动颜色”?

我正在谈论的图形:Graph (link to imgur)

我想要的是“ Summe”为黑色,而其余部分保持不变。

图形代码:

ggplot(data = ICU_Cephalosporine_SU_Per100PatientDays_norest_nototal_long, aes(x = Monat, y = `SUs pro 100 Pflegetage`, colour = `API`, group = API)) + geom_line(size = 1) + geom_point(size = 2)

我尝试添加的内容:

+ scale_colour_manual(values = c(name = "Summe", values = "#000000"))

+ scale_colour_manual(values = c("Summe" = "#E08214"))

错误中的第一个结果集:

  

错误:手动刻度中的值不足。需要4个,但仅提供2个。

第二个:

  

错误:手动刻度中的值不足。需要4个,但仅提供1个。

我找不到解决方案,有人知道我该怎么做吗?

编辑: 到目前为止,我创建了一些示例数据并应用了我的新发现:

mydata <- data.frame("Letter" = c("A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D",
"A", "B", "C", "D"), "Month" = c("Jan", "Jan", "Jan", "Jan", "Feb", "Feb", "Feb", "Feb",
"Mar", "Mar", "Mar", "Mar", "Apr", "Apr", "Apr", "Apr"), "Number" = c(1, 2, 3, 4, 4, 5, 1, 3,
6, 4, 2, 4, 1, 2, 5, 7))


ggplot(data = mydata, aes(x = Month, y = Number, colour = Letter, group = Letter))
+ theme(legend.position="bottom", legend.title = element_blank())
+ geom_line(data=subset(mydata, Letter == "A"), colour="black", size = 1)
+ geom_line(data=subset(mydata, Letter != "A"), size = 1)
+ geom_point(data=subset(mydata, Letter == "A"), colour="black", size = 1.5)
+ geom_point(data=subset(mydata, Letter != "A"), size = 1.5)

ggplot(data = mydata, aes(x = Month, y = Number, colour = Letter, group = Letter))
+ theme(legend.position="bottom", legend.title = element_blank())
+ geom_line()
+ geom_point(size = 1.5)

我的新问题是,现在图例中不再显示“ A”。为什么?数据是aes()之后的子集?!

1 个答案:

答案 0 :(得分:1)

这是ggplot使用的标准调色板

gg_color_hue <- function(n) {
  hues = seq(15, 375, length = n + 1)
  hcl(h = hues, l = 65, c = 100)[1:n]
}

您可以做类似的事情

pal <- gg_colur_hue(4)
pal[4] <- "#E08214"
ggplot(...) +
   ... +
   scale_color_manual(values=pal)