如何删除 ggplot 线图中的垂直白线?

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

#Relative abundance bar plot#

我使用三个表中的数据生成了 ggplot 图:OTU 表、OTU_taxonomy 和元数据。尽管图表的整体质量很高,但我还是在线条内遇到了垂直空白。如何从图中消除这些线条?任何见解都是有价值的。下面是我的代码,我附上了图表以供参考。

library(ggplot2)
library(tidyverse)

Tab <- read_tsv("otu_table_sat.tsv")

Tab <- read_tsv("otu_table_sat.tsv",
                col_types = cols(otu_id = col_character(),
                                 .default = col_number()))
dat <- Tab %>%
  pivot_longer(-otu_id, names_to = "sample_id", values_to = "count")

dat


Tax <- read_tsv("otu_taxonomy_sat.tsv",
                col_types = cols(.default = "character"))
Tax

dat <- dat %>%
  left_join(Tax, by = "otu_id")

dat

Meta <- read_tsv("sample_metadata_sat.tsv",
                 col_types = cols(.default = col_character()))
Meta

dat <- dat %>%
  left_join(Meta, by = "sample_id")
dat


#this is the final code for generating bar graph:
dat %>%
    ggplot(aes(x = sample_id, y = count)) +
  facet_grid(~ Group, scales = "free_x", space = "free_x") +
  geom_bar(aes(fill = Phylum), stat = "identity", position = "fill") +
  scale_y_continuous(name = "Relative abundance",
                     labels = scales::percent) +
  theme(axis.text.x = element_text(angle = 90, size = 10, face = "bold"),
        axis.text.y = element_text(color = "black"),
        strip.text = element_text(face = "bold"),
        strip.background = element_blank())

如果我的代码中缺少任何内容以及任何建议,请您提供答案。

相对丰度条形图:https://i.stack.imgur.com/4IECt.png

ggplot2 stack bar-chart
1个回答
0
投票

如果您提供一个可重现的示例,可能会更容易,但我对正在发生的事情的猜测如下:

  1. 那些白线可能是原始数据集中分组的结果。
  2. 这些变量中有 NA 或 NULL 值或某些内容。

解决方案1:

geom_bar()
函数中添加颜色参数

geom_bar(aes(fill = Phylum, color = Phylum), stat = "identity", position = "fill")

解决方案 2: 删除数据集中的 NA。

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