平时我们在进行数据可视化的时候有时需要对主图进行注释,之前介绍了aplot及patchwork也能完成,但是如果遇到复杂情况就很棘手,那有没有更好的解决方法那,当然了ggside包你值得拥有。喜欢的小伙伴欢迎关注我的公众号R语言数据分析指南,持续分享更多优质资源
加载R包
install.packages("ggside")
library(tidyverse)
library(ggside)
构建数据集
summariseDiamond <- diamonds %>%
mutate(`Cut Clarity` = paste(cut, clarity)) %>%
group_by(`Cut Clarity`,cut, clarity, color) %>%
summarise(n = n(),
`mean Price` = mean(price),
sd = sd(price))
summariseDiamond
ggplot(summariseDiamond, aes(x = color, y = `Cut Clarity`)) +
geom_tile(aes(fill = `mean Price`))
(p <-ggplot(summariseDiamond, aes(x = color, y = `Cut Clarity`)) +
geom_tile(aes(fill = `mean Price`)) +
geom_tile(aes(x=0, fill = cut)))
尝试放置颜色条会导致错误,因为上一个geom_tile已经映射mean Price到fill,因此分类变量fill不再能够映射到美学;但是我们可以映射另一个连续变量,但这会将它们放置在同一参考线中,从而改变了界限并清洗了所有颜色。
使用ggside可以将美感映射到一个单独的比例尺,也可以通过scale_*fill_gradient功能进行控制(稍后对此进行详细介绍)
ggplot(summariseDiamond, aes(x = color, y = `Cut Clarity`)) +
geom_tile(aes(fill = `mean Price`)) +
geom_ysidetile(aes(x = "sd of means", yfill = `sd of means`)) +
scale_yfill_gradient(low ="#FFFFFF", high = "#0000FF")
ggplot(summariseDiamond, aes(x = color, y = `Cut Clarity`)) +
geom_tile(aes(fill = `mean Price`)) +
geom_ysidetile(aes(x = "max", yfill = after_stat(summarise),
domain = `mean Price`), stat = "summarise", fun = max) +
geom_ysidetile(aes(x = "mean",yfill = after_stat(summarise),
domain = `mean Price`), stat = "summarise", fun = mean) +
geom_ysidetile(aes(x = "median",yfill = after_stat(summarise),
domain = `mean Price`), stat = "summarise", fun = median) +
geom_ysidetile(aes(x = "min",yfill = after_stat(summarise),
domain = `mean Price`), stat = "summarise", fun = min) +
scale_yfill_gradient(low ="#FFFFFF", high = "#0000FF")
分面示例
i2 <- iris %>%
mutate(Species2 = rep(c("A","B"), 75))
p <- ggplot(i2, aes(Sepal.Width, Sepal.Length, color = Species)) +
geom_point()
p2 <- p + geom_xsidedensity(aes(y=stat(density))) +
geom_ysidedensity(aes(x=stat(density))) +
theme_bw()
p2 + labs(title = "FacetNull")
此图ggExtra包与ggplot2也可以绘制,但是ggside自然有它的过人之处,如下所示
p2 + facet_wrap(Species~Species2) +
labs(title = "FacetWrap") +
guides(guide_axis(check.overlap = T))
p2 + facet_grid(Species~Species2, space = "free", scale = "free_y")
p2 + facet_grid(Species~Species2, space = "free", scales = "free") +
labs(title = "FacetGrid", subtitle = "Collapsing All Side Panels") +
ggside(collapse = "all")
p + geom_xsidedensity(aes(y=stat(density)))+
geom_ysidedensity(aes(x=stat(density), ycolor = Species2)) +
theme_bw() +
facet_grid(Species~Species2, space = "free", scales = "free") +
labs(title = "FacetGrid", subtitle = "Collapsing All Side Panels") +
ggside(collapse = "all")
p + geom_xsidedensity(aes(y=stat(density), xfill = Species), position = "stack")+
geom_ysidedensity(aes(x=stat(density), yfill = Species2), position = "stack") +
theme_bw() +
facet_grid(Species~Species2, space = "free", scales = "free") +
labs(title = "FacetGrid", subtitle = "Collapsing All Side Panels") +
ggside(collapse = "all") +
scale_xfill_manual(values = c("darkred","darkgreen","darkblue")) +
scale_yfill_manual(values = c("black","gold"))
网友评论