ggplot2添加数据的终结者之ggside

平时我们在进行数据可视化的时候有时需要对主图进行注释,之前介绍了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`))
ggplot2添加数据的终结者之ggside_第1张图片
(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") 
ggplot2添加数据的终结者之ggside_第2张图片
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") 
ggplot2添加数据的终结者之ggside_第3张图片

分面示例

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")

ggplot2添加数据的终结者之ggside_第4张图片

此图 ggExtra包与 ggplot2也可以绘制,但是ggside自然有它的过人之处,如下所示

p2 + facet_wrap(Species~Species2) +
  labs(title = "FacetWrap") +
  guides(guide_axis(check.overlap = T))
ggplot2添加数据的终结者之ggside_第5张图片
p2 + facet_grid(Species~Species2, space = "free", scale = "free_y") 
ggplot2添加数据的终结者之ggside_第6张图片
p2 + facet_grid(Species~Species2, space = "free", scales = "free") +
  labs(title = "FacetGrid", subtitle = "Collapsing All Side Panels") +
  ggside(collapse = "all")
ggplot2添加数据的终结者之ggside_第7张图片
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")
ggplot2添加数据的终结者之ggside_第8张图片
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")) 
ggplot2添加数据的终结者之ggside_第9张图片

你可能感兴趣的:(ggplot2添加数据的终结者之ggside)