我用R中的TidyVerse制作了一个我最满意的情节,但这个情节需要显示更多的信息,我还没有想出如何做到这一点.

这个情节的重点是展示来自三种不同动物的一堆细胞是如何根据它们的生物学通过算法分类和Bundle 在一起的.每种动物都有很多不同的细胞类型,并且有很多输出的细胞群;我正在绘制一个输出的群,在查看了每个动物被分类到这个群中的所有细胞后,我 Select 显示进入该图的源动物的前5个细胞类型名称.该图很好地显示了这一点(至少在我看来是这样),但它没有显示给定源细胞类型的所有细胞是否都Bundle 到了这个新的集群中,或者是一半,或者几乎没有,等等.

这是我使用的代码,以及我得到的情节(大部分都是喜欢的!)

library(tidyverse)
# create the contents of the toy dataset, then add together
species_organ <- c(rep("frog", 5),
                   rep("bat", 5),
                   rep("bird", 5)
)
annotation <- c("celltype1", "celltype2", "celltype3", "celltype4", "celltype5",
                "celltypeA", "celltypeB", "celltypeC", "celltypeD", "celltypeE",
                "celltypeAlpha", "celltypeBeta", "celltypeGamma", "celltypeDelta", "celltypeEpsilon"
)
count_in_integratedcluster <- c(253, 245, 226, 187, 185, 42, 18, 17, 11, 9, 58, 16, 8, 8, 7)
annotation_count_in_source_dataset <- c(413, 312, 349, 410, 233, 195, 198, 56, 166, 238, 82, 68, 270, 226, 81)
fraction_of_total_celltype_abundance <- count_in_integratedcluster / annotation_count_in_source_dataset

fake_dataframe <- data.frame(species_organ, annotation, count_in_integratedcluster, annotation_count_in_source_dataset, fraction_of_total_celltype_abundance)

# a few other things to decorate the plot with
how_many_cells_in_this_integrated_cluster <- 5056
cluster_name = "cluster6"

# now we make a lollipop plot
plot_lollipop_faceted.top5 <- ggplot(fake_dataframe) +
  geom_segment( aes(x=annotation, xend=annotation, y=0, yend=count_in_integratedcluster), color="grey") +
  geom_point( aes(x=annotation, y=count_in_integratedcluster, color=species_organ), size=3 ) +
  coord_flip()+
  theme(
    legend.position = "none",
    panel.border = element_blank(),
    panel.spacing = unit(0.1, "lines"),
    strip.text.x = element_text(size = 8)
  ) +
  xlab("") +
  ylab("How many times cells of this original annotation (y-axis)\nshowed up in this integrated cluster (plot title)") +
  facet_wrap(~species_organ, ncol=1, scale="free_y") +
  labs(title = paste(paste("integrated", cluster_name, sep = " "), ",", how_many_cells_in_this_integrated_cluster, "total cells"), 
       subtitle = "In this integrated cluster, see what cells contribute per species")

(plot I mostly like but which needs improvement)

一个简单的图形化解决办法是用一个可爱的小饼形图替换geom_point,用 colored颜色 填充来报告算法最终将90%的"鸟类肌肉细胞"还是只有10%的"鸟类肌肉细胞"分配到这个集群.

这里是一个铅笔素描的图形可能是什么样子,如果我做了我正在寻找的交换.

pencil sketch of improved plot

任何解决方案都必须是R的,我会喜欢基于TidyVerse的方法,但我愿意try 其他方法来传达所需的信息集.

我已经研究了其他相关问题,但不幸的是,我无法使建议的方法对我起作用,或者建议的解决方案在我的场景中似乎没有用;到目前为止,我已经判断了:

R::ggplot2::geom_points: how to swap points with pie charts?(杂乱无章的文档并没有帮助我理解该怎么做才能实现建议) ggplot use small pie charts as points with geom_point(馅饼很不错,但我不想失go 我的情节目前传达的其他信息) Plotting pie charts in ggplot2(标题听起来不错,但内容没有帮助) create floating pie charts with ggplot(这是我第二次看到coord_poll(),但在摆弄了一下它/阅读了它的文档后,我不知道如何使用它)

推荐答案

我们canscatterpie来得到你想要的剧情,但使用起来有点痛苦.它似乎不喜欢分类变量,因此需要通过因子将这些变量转换为数字,并重新标记为刻度.它也不能很好地处理coord_flip,因此您需要变换轴以使馅饼呈圆形.

因此,第一步是reshape 您的数据:

library(tidyverse)
library(scatterpie)

fake_dataframe <- fake_dataframe %>%
  rename(pos = fraction_of_total_celltype_abundance) %>%
  mutate(neg = 1 - pos) %>%
  mutate(annotation = fct_reorder(as.factor(annotation),
                                  as.factor(species_organ),
                                  ~mean(as.numeric(.x)))) %>%
  mutate(annotation2 = as.numeric(annotation)) %>%
  mutate(count_in_integratedcluster = count_in_integratedcluster/15)

那么绘图代码是:

ggplot(fake_dataframe,
       aes(x = annotation2, y = count_in_integratedcluster)) +
  geom_segment(aes(xend = annotation2, yend = 0), color = "grey") +
  geom_scatterpie(cols = c("pos", "neg"),
                  data = fake_dataframe,
                  aes(x = annotation2, y = count_in_integratedcluster)) +
  scale_fill_manual(values = c(pos = "black", neg = "white")) +
  coord_flip() +
  theme(
    legend.position = "none",
    panel.border = element_blank(),
    panel.spacing = unit(0.1, "lines"),
    strip.text.x = element_text(size = 8)
  ) +
  facet_grid(species_organ~., scale = "free_y", space = "free_y") +
  labs(title = paste(paste("integrated", cluster_name), ",", 
                     how_many_cells_in_this_integrated_cluster, "total cells"), 
       subtitle = paste0("In this integrated cluster, ",
                         "see what cells contribute per species"),
       y = "How many times cells of this original annotation (y-axis)
      showed up in this integrated cluster (plot title)",
       x = NULL) +
  scale_y_continuous(labels = ~.x * 15) +
  scale_x_continuous(labels = ~ levels(fake_dataframe$annotation)[.x])

enter image description here

R相关问答推荐

在ComplexHeatmap中,如何更改anno_barplot()标题的Angular ?

为什么当我try 在收件箱中使用合并功能时会出现回收错误?

terra nearest()仅为所有`to_id`列返回NA

带有叠加饼图系列的Highmap

如何在RMarkdown LaTex PDF输出中包含英语和阿拉伯语?

用derrr在R中查找组间的重复项

如何在R中添加截止点到ROC曲线图?

S用事件解决物质平衡问题

在R中,如何将变量(A,B和C)拟合在同一列中,如A和B,以及A和C在同一面板中?

如何显示准确的p值而不是<;0.001*?

TidyVerse中长度不等的列结合向量

如何将EC50值绘制在R中的剂量-react 曲线上?

我正在try 创建一个接近cos(X)的值的While循环,以便它在-或+1-E10范围内

如果满足条件,则替换列的前一个值和后续值

使用R、拼图和可能的网格包绘制两个地块的公共垂直线

有没有办法将勾选/审查标记添加到R中的累积关联图中?

如何在访问之前下载的输入时同时上传和处理所有指定的shiny 输入?

在不带max()的data.table中按组查找最后一个元素

如何计算多个变量的百分比与总和的百分比?

列间序列生成器的功能