如何将 ggplot 棒棒糖图的 geom_points 交换为 lil 饼图,以显示为该图评估的数据点的比例

问题描述 投票:0回答:1

我使用 R 中的 Tidyverse 制作了一个我最满意的情节,但该情节需要显示更多信息,但我还没有设法弄清楚如何做到这一点。

该图的重点是展示来自三种不同动物的一堆细胞如何根据其生物学特性通过算法进行排序并聚集在一起。每只动物都有很多不同的细胞类型,并且输出了很多细胞簇;我正在绘制一个输出的簇,在查看了分类到该簇中的每只动物的所有细胞后,我选择显示来自进入该图的源动物的前 5 个细胞类型名称。该图很好地显示了这一点(至少对我来说),但它没有显示给定源细胞类型的所有细胞是否都捆绑到这个新簇中,或者是否有一半或几乎没有,等等......

这是我使用的代码,以及我得到的情节(而且最喜欢!)。

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 替换为一个可爱的小饼图,并用颜色填充来报告是否 90% 的“鸟肌肉细胞”或仅 10% 的“鸟肌肉细胞”最终分配到该簇通过算法。

如果我进行了我想要的交换,这是图表的铅笔草图。

pencil sketch of improved plot

任何解决方案都必须采用 R 语言,我很欣赏基于 Tidyverse 的方法,但我愿意尝试其他方法来传达所需的信息集。

我查看了其他相关问题,不幸的是无法使建议的方法对我有用,否则建议的解决方案在我的场景中似乎没有用;到目前为止,我已经检查过:

R::ggplot2::geom_points:如何用饼图交换点?(scatterpie 文档没有帮助我理解如何实施建议) ggplot 使用小饼图作为 geom_point 的点(饼很好,但我不想丢失当前由我的图传达的其他信息) 在ggplot2中绘制饼图(标题听起来不错,但内容没有帮助) 使用 ggplot 创建浮动饼图(这是我第二次看到 coord_polar() 但在摆弄它/阅读它的文档后我不知道如何使用它)

r ggplot2 visualization pie-chart
1个回答
0
投票

我认为每一端的馅饼都会有点困难,而且无法即时想出一些东西,我希望其他人能有一个好主意。 (挑战包括当其中一个轴是有序的而不是连续的时保持 1:1 的纵横比。)

在那之前,有两个选择:

文字

添加

geom_text
和百分比。 (我添加标题是为了好玩。)

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 ) +
  # BEGIN: addition
  geom_text(aes(x=annotation, y=count_in_integratedcluster,
                label=sprintf("%0.02f%%", 100 * fraction_of_total_celltype_abundance)),
            hjust = -0.2) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
  # END: addition
  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",
       caption = "Percentage indicates the amount cells of a given source celltype that are bundled into the new clusters")

填充矩形

我建议垂直填充的矩形也可能有助于以图形方式展示包含的内容。不过,由于

geom_tile
沿着其高度填充,这意味着我们需要撤消
coord_flip()
并在其自然域中处理 x 和 y。这涉及(字面上)交换代码中的字母
x
y
并删除翻转(包括
xlab
ylab
)。 (不知道为什么你首先需要翻转......)

# swap x/y (un-flip coord)
barwid <- 5; barht <- 0.8
ggplot(fake_dataframe) +
  geom_segment( aes(y=annotation, yend=annotation, x=0, xend=count_in_integratedcluster-(barwid/2)), color="grey") +
  geom_tile(aes(y=annotation, x=count_in_integratedcluster, color=species_organ), width=barwid, height=barht, fill=NA) +
  geom_tile(aes(y=annotation, x=count_in_integratedcluster,
                height = barht * fraction_of_total_celltype_abundance,
                color=species_organ, fill=species_organ),
            width=barwid) +
  theme(
    legend.position = "none",
    panel.border = element_blank(),
    panel.spacing = unit(0.1, "lines"),
    strip.text.x = element_text(size = 8)
  ) +
  ylab(NULL) +
  xlab("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",
       caption = "Filled rectangles indicates the amount cells of a given source celltype that are bundled into the new clusters; full-color means 100% used")

此方法可以根据发布机制和您的喜好进行更多修饰。您可以在

size=1
中添加
geom_tile
(或某个数字)来更改图块轮廓上的笔画/线宽,使其变厚。另一种方法是使第一个图块为一种颜色(白色),第二个图块为真实填充颜色,并且您可以删除轮廓/描边。

除了添加

size=
之外,还可以使用
barwid
barht
来获得您想要的外观。

仅供参考,

ylab("")

ylab(NULL)
 不同,因为前者保留了空标签的空间。不确定这对你是否重要,我在这里用 
NULL
 进行了演示,但它会按照你想要的方式工作。

© www.soinside.com 2019 - 2024. All rights reserved.