我想使用 gg_vistime 为不同组中的相同变量分配颜色,但我似乎无法弄清楚
这是我的代码
library(RColorBrewer)
myColors <- brewer.pal(12, "Set3")
names(myColors) <- levels(tambah$abb)
colScale <- scale_color_manual(name = "indicator", values = myColors)
gg_vistime(test1, title = "Test 1", col.event = "abb", col.group = "brand",
col.start = "start_year", col.end = "end_year",
optimize_y = TRUE, show_labels = TRUE) +
scale_x_datetime(date_breaks = "1 year", date_labels = "%Y") +
theme(
plot.title = element_text(hjust = 0, size=15),
axis.text.x = element_text(size = 11, color = "black", angle = 0),
axis.text.y = element_text(size = 11, color = "black", angle = 0)) + scale_color_manual(name = "issue", values = myColors)
我的数据框采用 dput() 的形式
structure(list(start_year = structure(c(1356998400, 1230768000,
1199145600, 1230768000, 1325376000), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), end_year = structure(c(1388534400, 1262304000,
1230768000, 1262304000, 1356998400), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), brand = c("DELL", "DELL", "BREE", "DELL",
"IBG"), issue = c("Materials", "Site", "Materials", "Water",
"Site"), abb = c("M", "S", "M", "W", "S")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -5L))
这是结果,它在“S”和“M”上显示不同的颜色,我不知道应该采取哪一步来实现我的目标(所有“S”使用相同的颜色,与“相同” M"s)。
如有帮助,我们将不胜感激:)
从更近但不深入的角度来看,
gg_vistime
似乎在color
aes 上映射了一列文字颜色代码并使用scale_color_identity
。要使用命名向量根据手动比例进行着色,您可以使用 aes()
更改映射,即在 event
aes 上映射 color
,其中 event
是 gg_vistime
在引擎盖下分配给包含事件的列。
library(RColorBrewer)
library(vistime)
library(ggplot2)
myColors <- brewer.pal(12, "Set3")
names(myColors) <- levels(factor(test1$abb))
colScale <- scale_color_manual(name = "indicator", values = myColors)
gg_vistime(test1,
title = "Test 1", col.event = "abb", col.group = "brand",
col.start = "start_year", col.end = "end_year",
optimize_y = TRUE, show_labels = TRUE
) +
aes(color = event) +
scale_x_datetime(date_breaks = "1 year", date_labels = "%Y") +
theme(
plot.title = element_text(hjust = 0, size = 15),
axis.text.x = element_text(size = 11, color = "black", angle = 0),
axis.text.y = element_text(size = 11, color = "black", angle = 0)
) +
scale_color_manual(name = "issue", values = myColors, guide = "none")