导读
举例展示几种常用的ggplot取色方法。
一、输入数据
library(ggplot2)
library(RColorBrewer)
df = data.frame(x=1:10, y=seq(1, 20, 2), z=seq(1000, 100, -100), c=paste("color", seq(1, 10, 1)))

二、无色
ggplot(df, aes(x=x, y=y)) +
geom_bar(stat="identity")

三、默认颜色
ggplot(df, aes(x=x, y=y, fill=c)) +
geom_bar(stat="identity") +
labs(x="x axis", y="y axis", fill="legend")

四. 数字取色-正序
ggplot(df, aes(x=x, y=y, fill=x)) +
geom_bar(stat="identity") +
labs(x="x axis", y="y axis", fill="legend")

五、 数字取色-倒序
ggplot(df, aes(x=x, y=y, fill=z)) +
geom_bar(stat="identity") +
labs(x="x axis", y="y axis", fill="legend")

六、渐变色:色1-色2,取色
colors <- colorRampPalette(c("red", "orange"))(10)
ggplot(df, aes(x=x, y=y)) +
geom_bar(stat="identity", fill=colors[rank(10:1)]) +
labs(x="x axis", y="y axis", fill="legend")

七、RColorBrewer取色:set3为例
ggplot(df, aes(x=x, y=y)) +
geom_bar(stat="identity", fill=brewer.pal(10, "Set3")) +
labs(x="x axis", y="y axis", fill="legend")

网友评论