所以我想在我的散点图中引入一个更多样的颜色图案,因为我的n很高,而且标准集的判别性还不够高。所以,我已经生成了一个颜色向量,但是现在我不能正确地理解这个传说。我不是没有传说,就是有颜色的传说。我很肯定我混淆了美学和属性,但我不知道我做错了什么。
下面是我的代码和三次尝试。我想要实现的是与我创建的颜色向量(col_sample)匹配的颜色,但是图例的名称与dataframe中的name列匹配。
#dataframe
df1 <- data.frame(name = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e"),
n = rep(1:31, 1),
value = rep(31:1, 1))
df1$name <- as.factor(df1$name)
#produce color vector
color <- grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)]
col_sample <- sample(color, 31)
col_sample <- as.vector(col_sample)
#scatterplot
median_scatter <- ggplot(data = df1,
aes(x = n,
y = value,
col = name))
#try 1: these colors are too similar
median_scatter +
geom_point()
#try 2: t he legend dissappears
median_scatter +
geom_point(col = col_sample)
#try 3: t he legend dissappears
median_scatter +
geom_point(aes(col = col_sample))发布于 2017-10-26 09:24:37
使用scale_colour_manual手动定义颜色刻度。
median_scatter <- ggplot(data = df1,
aes(x = n,
y = value,
colour = name))
median_scatter +
geom_point() +
scale_colour_manual(values=col_sample)注意,legend是绑定到aes的。在“尝试2”中,通过将颜色向量分配给aes(col=name)中的col,您重写了父ggplot中的颜色美学col。name和col_sample之间没有关联,因此没有传说。
在第三次尝试中,您重新分配了aes(col=col_sample),因此颜色名称现在成为分配给默认颜色的变量。
https://stackoverflow.com/questions/46950357
复制相似问题