|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
气泡图、散点图想必大家都会画,今天内容主要是relax一下,把你的散点图/气泡图施加猫猫变身之术!实属给爱猫人士送福利了……
[AppleScript] 纯文本查看 复制代码 #相关R包安装和载入:
remotes::install_github("R-CoderDotCom/ggcats@main")
install.packages('Ecdat')
library(ggcats) #画猫的
library(ggplot2)
library(tidyverse)
library(Ecdat) #会用到一个数据集
library(gganimate) #制作动图的,使用可戳往期:《汇报一绝!让你的静态图表动起来!》
1. 如何调用猫猫
- #首先,该R包中包含15只可调用的猫咪,下面是它们的调用名:
- cat <- c("nyancat", "bongo", "colonel", "grumpy", "hipster",
- "lil_bub", "maru", "mouth", "pop", "pop_close",
- "pusheen", "pusheen_pc", "toast", "venus", "shironeko")
- #创建一个5*3的矩阵一探这15只猫咪容颜:
- grid <- expand.grid(1:5, 3:1)
- dt <- data.frame(x = grid[, 1],
- y = grid[, 2],
- z = grid[, 2]-0.4, #用于文字标签的坐标
- image = cat)
- head(dt)
复制代码
- #ggplot2绘制:
- p <- ggplot(dt) +
- geom_cat(aes(x, y, cat = image), size = 5) + #使用geom_cat添加猫咪
- xlim(c(0.25, 5.5)) +
- ylim(c(0.25, 3.5)) +
- geom_text(aes(x = x,y = z, label = image))+
- theme_bw()
- p
复制代码
其实这15只不同模样的猫咪就等同于不同的散点类型,画图时选择喜欢的猫咪即可。
2. 猫咪气泡图绘制
- #读入本地丰度气泡图测试数据:
- df <- read.csv('bubble_test.csv',header = T)
- head(df)
复制代码
- #先绘制常规丰度气泡图:
- mytheme <- theme_bw() +
- theme(axis.title.x = element_blank(),
- axis.title.y = element_blank(),
- axis.text.x = element_text(size = 12, angle = -45),
- axis.text.y = element_text(size = 10),
- legend.text = element_text(size = 10),
- legend.text.align = 0.5,
- legend.title = element_text(size = 12, hjust = 0))
- p1 <- ggplot(df, aes(x = cluster,y = interacting_pair)) +
- geom_point(aes(size = proportion, color = exp), shape = 16) + #圆形bubble
- scale_size(range = c(3, 10)) +
- scale_color_gradient(low = "#f5c6c6", high = "#cf0000") +
- mytheme
- p1
复制代码
- #下面绘制猫猫气泡图:
- ##用猫猫展示表达分布占比/或显著P值等数据:
- p2 <- ggplot(df) +
- geom_cat(aes(cluster, interacting_pair, size = proportion), cat = 'grumpy') +
- scale_size(range = c(0.5, 2.5)) +
- mytheme
- p2
复制代码
- ##用猫猫展示表达量/丰度等数据:
- p3 <- ggplot(df) +
- geom_cat(aes(cluster, interacting_pair, size = exp), cat = 'lil_bub') +
- scale_size(range = c(0.5, 3)) +
- mytheme
- p3
复制代码
- #也可以同时用颜色表示表达量,猫猫表示分布占比/或P值等:
- p4 <- ggplot(df, aes(x = cluster,y = interacting_pair)) +
- geom_point(aes(color = exp), shape = 15, size = 14) +
- scale_color_gradient(low = "#fcc648", high = "#f76c80") +
- geom_cat(aes(size = proportion), cat = 'bongo') +
- scale_size(range = c(0.5, 2.5)) +
- mytheme
- p4
复制代码
3. 猫咪动态折线图绘制
- #读取测试数据
- data(incomeInequality) #来自Ecdat包,美国家庭收入分位数;
- dat <-incomeInequality %>%
- select(Year, P99, median) %>%
- rename(income_median = median,
- income_99percent = P99) %>%
- pivot_longer(cols = starts_with("income"),
- names_to = "income",
- names_prefix = "income_")
- head(dat)
复制代码
- #新增猫咪列和数据列对应:
- dat$cat <- rep(NA, 132)
- dat$cat[which(dat$income == "median")] <- "nyancat"
- dat$cat[which(dat$income == "99percent")] <- rep(c("pop_close", "pop"), 33)
- head(dat)
复制代码
- #先绘制折线图:
- p5 <- ggplot(dat, aes(x = Year, y = value, group = income, color = income)) +
- geom_line(size = 2) +
- mytheme
- p5
复制代码
- #制作猫猫动态图:
- p6 <- ggplot(dat, aes(x = Year, y = value, group = income, color = income)) +
- geom_line(size = 2) +
- geom_cat(aes(cat = cat), size = 5) +
- mytheme +
- transition_reveal(Year)
- p6
- anim_save("ggcats.gif")
复制代码
有丢子搞笑……另外,这个R包作者似乎还有一个dog派的类似包,感兴趣可以自行去瞅两眼。
如果大家对使用gganimate包制作动态图感兴趣表,可戳往期:《汇报一绝!让你的静态图表动起来!》
好啦,我们今天的分享就到这里!
参考资料
https://github.com/R-CoderDotCom/ggcats
本文作者:基迪奥-喵酱
|
|