如何使用自己想要的颜色让图例出现在我的情节中? [复制]
Posted
技术标签:
【中文标题】如何使用自己想要的颜色让图例出现在我的情节中? [复制]【英文标题】:How can I get a legend to appear on my plot using my own desired colours? [duplicate] 【发布时间】:2019-01-04 13:53:09 【问题描述】:我想在其中包含一个图例。
我尝试了一些提供给其他人的解决方案,但我的问题是我无法使用我想要的颜色,因为解决方案需要我将 colour
放在 aes()
中,这会产生与我指定的。
我确定解决方案在scale_fill_manual
之内,但我无法正确实施。
因此,我怎样才能包含图例并保持相同的线条颜色?
我的数据框:
grouped <- structure(list(date = c("2018-07-16", "2018-07-17", "2018-07-18",
"2018-07-19", "2018-07-20", "2018-07-21", "2018-07-22", "2018-07-23",
"2018-07-24", "2018-07-25", "2018-07-26", "2018-07-27", "2018-07-28",
"2018-07-29", "2018-07-30", "2018-07-31"), homepage_opens = c(5L,
0L, 0L, 3L, 1L, 2L, 0L, 1L, 0L, 2L, 5L, 0L, 0L, 0L, 0L, 0L),
sitewide_opens = c(39L, 34L, 19L, 62L, 46L, 44L, 16L, 51L,
25L, 66L, 75L, 0L, 0L, 0L, 0L, 0L), chats_started = c(10L,
16L, 9L, 8L, 13L, 13L, 5L, 13L, 4L, 8L, 11L, 0L, 0L, 0L,
0L, 0L), chats_completed = c(7L, 13L, 8L, 4L, 5L, 9L, 6L,
13L, 2L, 7L, 5L, 0L, 0L, 0L, 0L, 0L)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -16L))
我的剧情代码:
ggplot(grouped) +
geom_line(aes(x = date, y = sitewide_opens, group = 1),
linetype = "dashed",
colour = "forestgreen",
alpha = 0.5) +
geom_line(aes(x = date, y = homepage_opens, group = 1),
colour = "blue") +
geom_vline(aes(xintercept = 8),
linetype = 2,
colour = "black") +
geom_text(aes(x = date, y = homepage_opens, label = homepage_opens),
hjust = -0.15,
vjust = -1.5,
size = 3,colour = "black") +
geom_text(aes(x = date, y = sitewide_opens, label = sitewide_opens),
hjust = -0.15,
vjust = -1.5,
size = 3,
colour = "black") +
labs(title = "Title",
x = "Date",
y = "Count") +
theme(plot.title = element_text(size = 25,
face = "bold",
colour = "black"),
legend.position = "top",
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
axis.text = element_text(colour = "black"))
【问题讨论】:
【参考方案1】:您几乎是对的,您需要调整比例,但不填充颜色(因为您使用颜色来设置线条的颜色并使用填充来设置填充对象的颜色。这是解决方案。
ggplot(grouped) +
geom_line(aes(x = date, y = sitewide_opens, colour = "Sitewide opens",group = 1),
linetype = "dashed",
alpha = 0.5) +
geom_line(aes(x = date, y = homepage_opens, group = 1,
colour = "Homepage opens")) +
geom_vline(aes(xintercept = 8),
linetype = 2) +
geom_text(aes(x = date, y = homepage_opens, label = homepage_opens),
hjust = -0.15,
vjust = -1.5,
size = 3) +
geom_text(aes(x = date, y = sitewide_opens, label = sitewide_opens),
hjust = -0.15,
vjust = -1.5,
size = 3,colour = "black") +
theme(legend.position = "top",
legend.title=element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
axis.text = element_text(colour = "black")) +
scale_colour_manual(values=c("Sitewide opens"="forestgreen", "Homepage opens"="blue"), labels=c("Sitewide opens", "Homepage opens"))
【讨论】:
重塑数据不是更健壮吗? @AlienDeg 谢谢,这很好用,但是如何从图例中删除单词colour
并将图例文本从 sitewide_opens
更改为 Sitewide Opens
?
主题(legend.title=element_blank())
@MusTheDataGuy 调整代码。
@AlienDeg 谢谢,这很好,但图例文本不太正确,因为它包含下划线;是否可以将文本更改为不带下划线的 Sitewide opens
和 Homepage opens
?【参考方案2】:
每当您使用ggplot2
时,您都应该为它提供long 数据。这使得添加更多行、文本或其他内容变得更加容易(正如@MusTheDataGuy 已经提到的)。因此,这里有一个最小的ggplot2
样式示例(进一步,将日期转换为它的样子......日期 - ggplot2
知道如何处理它,您不需要group
):
grouped_long <- tidyr::gather(data = grouped, key = key, value = value, -date)
ggplot(data = subset(grouped_long, key %in% c("homepage_opens", "sitewide_opens")),
aes(x = as.Date(date),
y = value,
label = value,
color = key,
lty = key)) +
geom_line() +
geom_text(color = "black") +
geom_vline(xintercept = 8, lty = 2) +
scale_linetype_manual(values = c(sitewide_opens = 2,
homepage_opens = 1)) +
scale_color_manual(values = c(sitewide_opens = "forestgreen",
homepage_opens = "blue"))
【讨论】:
以上是关于如何使用自己想要的颜色让图例出现在我的情节中? [复制]的主要内容,如果未能解决你的问题,请参考以下文章