今天小编给大家推荐一个优秀的颜色映射R包-ggchromatic,该包提供了额外的fill和colour等选项,可将变量值映射到不用的颜色值上,且其作为ggplot2的拓展包,用起来也十分方便。接下来小编就通过几个具体例子带你领略一下ggchromatic包的颜色魅力。
「cmy_spec()」:
library(tidyverse)
library(ggtext)
library(hrbrthemes)
library(wesanderson)
library(LaCroixColoR)
library(RColorBrewer)
library(ggchromatic)
#可视化绘制
ggplot(mtcars, aes(mpg, disp)) +
geom_point(aes(colour = cmy_spec(mpg, drat, wt)),size=4) +
labs(
title = "Example of <span style='color:#D20F26'>ggchromatic::cmy_spec function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>cmy_spec()</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12)
)
Colour Example01 Of cmy_spec()
「rgb_spec()」
+
geom_point(aes(colour = rgb_spec(mpg, drat, wt)),size=4)
+
Colour Example02 Of rgb_spec()
或者绘制一下效果:
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point(aes(colour = rgb_spec(Species, sample(Species), sample(Species))),size=4) +
labs(
title = "Example of <span style='color:#D20F26'>ggchromatic::rgb_spec function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>rgb_spec(-sample-)</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12)
)
Colour Example02-1 Of rgb_spec()
「hsv_spec()」:
+
geom_point(aes(colour = hsv_spec(mpg, drat, wt)),size=4)
+
Colour Example03 Of hsv_spec()
「guide_colourrect()」:
ggplot(mtcars, aes(mpg, wt)) +
geom_point(aes(colour = cmy_spec(as.factor(cyl), drat, qsec)),size=4) +
guides(
colour = guide_colourrect(title = c("# Cylinders", "Rear axle ratio"))
) +
labs(
title = "Example of <span style='color:#D20F26'>ggchromatic::guide_colourrect function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>guide_colourrect()</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12)
)
Colour Example04 Of guide_colourrect()
「guide_chromatic()」:
df <- data.frame(
x = c(row(volcano)), y = c(col(volcano)), z = c(volcano)
)
ggplot(df, aes(x, y)) +
guides(fill = guide_chromatic()) +
geom_raster(aes(fill = cmy_spec(x, z))) +
labs(
title = "Example of <span style='color:#D20F26'>ggchromatic::guide_chromatic function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>guide_chromatic()</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12)
)
Colour Example05 Of guide_chromatic()
总计
今天的推文我们简单介绍了R-ggchromatic包的颜色映射功能,较常规映射,其提供的颜色样式更加多样,且图例也更加丰富,虽然色彩空间可能不是解释数据可视化的最直观的工具,但对于可视化作品来说,合理的颜色设置对于数据所表现的内容来说,还是蛮重要的,希望本期的推文能够对大家有所帮助~~