这是我的代码:
library(data.table)
library(ggplot2)
clist <- list(c(1:39), c(2:40), c(3:41))
clist.ts <- lapply(clist, function(x) ts(x, frequency = 1, start = 1978))
ind78_ymean_tsdf <- as.data.frame(clist.ts)
names(ind78_ymean_tsdf) <- c("name1", "name2", "name3")
ind78_ymean_tsdf$"Year" <- c(1978:2016)
setDT(ind78_ymean_tsdf)
ind78_ymean_melt <- melt(ind78_ymean_tsdf, id=c("Year"))
(ggplot(ind78_ymean_melt, aes(x=Year, y=value, color=variable))
+ geom_line()
+ geom_line(data=subset(ind78_ymean_melt, variable == "name1"), colour="black", size=1.5)
+ labs(title="Development of the indices", x="Year", y="Index")
+ scale_color_discrete(name="Individual replications")
+ theme_light())
# + guides(colour=guide_legend(override.aes=list(colour=c(hue_pal()(11)[1:10], "black"), size=c(rep(1, 10), 1.5))))
它基本上与以下问题相同,但有一个可重现的例子:manual color assignment
我现在的问题是,我不知道如何更改以下行,以便使图表的图例中的条目也变黑:
guides(colour=guide_legend(override.aes=list( colour=c(hue_pal()(11)[1:10], "black"), size=c(rep(1, 10), 1.5))))
也许有人可以解释上述行中的参数是什么意思,或者可以将问题发布到上面的链接,因为我没有足够的街头信誉。这样做.. :)如果有帮助,我在真实情节中有13个变量(不在上面的可重现的例子中)。提前谢谢!
答案 0 :(得分:1)
您的解决方案看起来过于复杂。你为什么不只是scale_color_manual
和scale_size_manual
。
ggplot(ind78_ymean_melt, aes(Year, value, color = variable, size = variable)) +
geom_line() +
labs(title = "Development of the indices",
x = "Year",
y = "Index",
color = "Individual replications") +
scale_color_manual(values = c("black", hue_pal()(2))) +
scale_size_manual(values = c(1.5, rep(0.5, 2))) +
theme_light() +
guides(size = FALSE)
答案 1 :(得分:0)
这是@drmariod的解决方案:
(ggplot(ind78_ymean_melt, aes(x=Year, y=value, color=variable), size=2)
+ geom_line()
+ geom_line(data=subset(ind78_ymean_melt, variable == "name3"), colour="black", size=1.5)
+ labs(title="Development of the indices", x="Year", y="Index")
+ scale_color_discrete(name="Individual replications")
+ theme_light()
+ guides(colour=guide_legend(override.aes=list( colour=c(hue_pal()(2), "black"), size=c(rep(1, 2), 1.0)))) )
最后一个代码行的解释:hue_pal()(11)从色调调色板中创建了11种颜色,但它只选择了1:10的颜色并为其添加了黑色。