我如何才能得到ggplot总是利用相同的颜色映射为一个因素。例如:
library(ggplot)
## Filter for visual simplification
diamonds2 <- diamonds[1:10,]
ggplot(diamonds2, aes(carat, price, color = cut)) + geom_point()
## Now filtering removes some levels of cut
diamonds3 <- diamonds[1:5,]
ggplot(diamonds3, aes(carat, price, color = cut)) + geom_point()
在第一个散射因子级中,“公平”为红色。在第二张图中,因子级“好”变成红色。我想要保持映射,不管过滤是否删除因子级别,这样“公平”总是映射到红色,等等。
在现实中,我的图形要复杂得多。我最初的因素有11个层次。
MyPalette <- c("#5DD0B9", "#E1E7E9", "#1f78b4", "#a6cee3", "#de77ae", "#c51b7d", "#8e0152", "#6a3d9a", "#fbdf6f", "#ff7f00", "#fff99")
我在ggplot中所指的
... scale_fill_manual(values = MyPalette, name="") + ...
发布于 2016-03-03 19:59:51
只需为MyPalette
和scale_colour_manual()
使用命名向量
MyPalette <- c(Fair = "#5DD0B9", Good = "#E1E7E9", "Very Good" = "#1f78b4", Premium = "#a6cee3", Ideal = "#de77ae")
ggplot(diamonds2, aes(carat, price, color = cut)) + geom_point() +
scale_colour_manual(values = MyPalette)
ggplot(diamonds3, aes(carat, price, color = cut)) + geom_point() +
scale_colour_manual(values = MyPalette)
https://stackoverflow.com/questions/35781783
复制相似问题