Search code examples
ggplot2stackbar-chart

How can I remove vertical white lines in a ggplot line graph?


#Relative abundance bar plot#

I've generated a ggplot graph using data from three tables: OTU table, OTU_taxonomy, and metadata. Despite the graph's overall quality, I'm encountering vertical white spaces within the lines. How can I eliminate these lines from the plot? Any insights would be valuable. Below is my code, and I've attached the graph for reference.

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())

Please you can provide the answer if anything I am missing here in my code and any sugession.

Relative abundance bar plot: https://i.sstatic.net/4IECt.png


Solution

  • It could be easier if you provide a reproducible example, but my guess on what's happening is the following:

    1. Those white lines are probably the result of grouping in the original dataset.
    2. You have NA's or NULL values or something in those variables.

    Solution 1: Add the color parameter to the geom_bar() function

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

    Solution 2: Get rid of the NA's in your dataset.