我正在尝试生成几个甘特图,每个图都有不同数量的条目。我将线的颜色映射到因子(源)。在下面的示例中,“大”和“小”图表的因子水平相同,但绘制时,每个级别的颜色会发生变化(即'radio'在第一个中为绿色,在第二个中为红色) )!
如何确保所有绘图具有相同的颜色映射?
# Generate vectors:
name <- paste("person", seq(10), sep = '_')
start <- sample(seq(5), size = 10, replace = T)
end <- sample(seq(6,10), size = 10, replace = T)
source <- factor(c('radio','tv','radio','tv','radio','tv','book','wordofmouth','book','book'))
# Generate data frames:
big_chart <- data.frame(name = c(name,name), value = c(start,end), source)
small_chart <- big_chart[c(1:2,11:12),]
library(ggplot2)
ggplot(big_chart, aes(value, name, colour = as.factor(source))) +
geom_line()
ggplot(small_chart, aes(value, name, colour = as.factor(source))) +
geom_line()
答案 0 :(得分:0)
ggplot(small_chart, aes(value, name, colour = source)) + geom_line() + scale_colour_discrete(drop = FALSE)