R作图

R语言:常用的ggplot取色方法

2020-08-05  本文已影响0人  胡童远

导读

举例展示几种常用的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")
上一篇 下一篇

猜你喜欢

热点阅读