gpt4 book ai didi

从指定行和列的 facet_grid 中删除未使用的因素

转载 作者:行者123 更新时间:2023-12-04 15:41:44 26 4
gpt4 key购买 nike

问题是什么?

ggplot2 中的每个方面可能不包含“名称”列中的所有因子水平,在这种情况下,我希望该方面忽略这些因子水平以避免图 block 中的间隙。

我尝试了什么

我尝试在 中添加和删除 scales = "free"drop = TRUEspace = "free" facet_grid 在 stackexchange 问题 here 和 stackoverflow 问题 here 中推荐。

如有任何帮助,我们将不胜感激!

示例数据切片

test_data <- structure(list(sample = c("1", "1", "1", "1", "1", "2", "2", 
"2", "2", "2", "3", "3", "3", "3", "3", "1", "1", "1", "1", "1",
"2", "2", "2", "2", "2", "3", "3", "3", "3", "3"), name = c("IV_1385127_1385127_1_+_3_1",
"IV_78222_78222_1_-_3_1", "XV_978130_978130_1_-_3_1", "XIV_574351_574351_1_+_3_1",
"XV_357215_357215_1_-_3_1", "XII_456601_456601_1_-_3_1", "V_423552_423552_1_+_3_1",
"XI_200191_200191_1_-_3_1", "XII_465717_465717_1_-_4_2", "XII_455342_455342_1_-_3_1",
"VII_84298_84298_1_-_3_1", "IV_229884_229884_1_+_4_2", "XII_633371_633371_1_-_4_2",
"XIII_9888_9888_1_-_4_2", "X_703096_703096_1_-_3_2", "IV_1385127_1385127_1_+_3_1",
"IV_78222_78222_1_-_3_1", "XV_978130_978130_1_-_3_1", "XIV_574351_574351_1_+_3_1",
"XV_357215_357215_1_-_3_1", "XII_456601_456601_1_-_3_1", "V_423552_423552_1_+_3_1",
"XI_200191_200191_1_-_3_1", "XII_465717_465717_1_-_4_2", "XII_455342_455342_1_-_3_1",
"VII_84298_84298_1_-_3_1", "IV_229884_229884_1_+_4_2", "XII_633371_633371_1_-_4_2",
"XIII_9888_9888_1_-_4_2", "X_703096_703096_1_-_3_2"), ntile = c("1",
"1", "1", "1", "1", "1", "1", "1", "2", "1", "1", "2", "2", "2",
"2", "1", "1", "1", "1", "1", "1", "1", "1", "2", "1", "1", "2",
"2", "2", "2"), position = c("-1", "-1", "-1", "-1", "-1", "-1",
"-1", "-1", "-1", "-1", "-1", "-1", "-1", "-1", "-1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"
), base = c("T", "T", "T", "A", "C", "A", "T", "T", "A", "T",
"T", "G", "C", "C", "A", "A", "G", "A", "G", "G", "A", "T", "A",
"G", "T", "A", "A", "A", "A", "A")), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -30L))

用于绘图的代码

  p <- ggplot(test_data, aes(x = position, y = factor(name))) +
geom_tile(aes(fill = base)) +
scale_fill_viridis_d() +
theme_bw() +
theme(
axis.title.y=element_blank(),
axis.text.y=element_blank(),
legend.title=element_blank(),
axis.title.x=element_text(margin = margin(t = 15)),
panel.grid=element_blank()
)

p <- p + facet_grid(ntile ~ sample, scales = "free", space = "free", drop = TRUE)

示例绘图输出

example plot

ggplot2 版本

ggplot2 3.1.1

最佳答案

您的示例数据中没有未使用的因素,并且参数没有给您想要的东西。可能的启动解决方法可以是:

# Your script
p <- ggplot(test_data, aes(x = position, y = factor(name))) +
geom_tile(aes(fill = base)) +
scale_fill_viridis_d() +
theme_bw() +
theme(
axis.title.y=element_blank(),
axis.text.y=element_blank(),
legend.title=element_blank(),
axis.title.x=element_text(margin = margin(t = 15)),
panel.grid=element_blank()
)
p + coord_flip() + facet_wrap(ntile ~ sample, scales = "free")

输出

unused_factor_updated

关于从指定行和列的 facet_grid 中删除未使用的因素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57616797/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com