Standardize bar width between distinct graphs ggplot2

Asked

Viewed 155 times

3

Hello, I would like to know how I can standardize the width of my bars from ggplot2 between distinct charts. I would like to leave the bars of all charts at the same width. For example, I have these two charts with bars of different widths, I would like to leave the bars of the two charts with the same width.

To try to resolve this issue I was trying to put the argument wide, width equal in all charts, however, did not solve. Thank you

Code of graphic 1:

ebov_data <- data.frame(
  BioProject_acession_number=c("PRJNA352396", "PRJNA352396"),
  status=c("control", "patient"), 
  samples=c(16, 158))

ggplot(ebov_data, aes(x=BioProject_acession_number, y = samples, fill = status)) + 
  geom_bar(stat = "identity", position = "dodge", width = 0.8) +
  scale_fill_manual(values=c("#00BFC4", "#F8766D")) +
  ggtitle("Ebola study") +
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(y = "number of samples", x = "BioProject's acession number") +
  geom_text(aes(label=samples), position=position_dodge(width=0.8), vjust=-0.5)

Code of graphic 2:

dengue_data <- data.frame(
GEO_acession_number=c("GSE51808","GSE51808", "GSE28991", "GSE28991", "GSE28988", "GSE28988", "GSE28405", "GSE28405", "GSE25001", "GSE25001", "GSE13052", "GSE13052", "GSE116672", "GSE116672"),
status=c("control", "patient", "control", "patient", "control", "patient", "control", "patient", "control", "patient", "control", "patient", "control", "patient"), 
samples=c(28, 28, 11, 22, 96, 210, 31, 62, 34, 175, 12, 18, 4, 6))  

  ggplot(dengue_data, aes(x=GEO_acession_number, y = samples, fill = status)) + 
       geom_bar(stat = "identity", position = "dodge", width = 0.8) +
       scale_fill_manual(values=c("#00BFC4", "#F8766D")) +
       ggtitle("Dengue studies") +
       theme(plot.title = element_text(hjust = 0.5)) +
       labs(y = "number of samples", x = "GEO's acession number") +
       geom_text(aes(label=samples), position=position_dodge(width=0.8), vjust=-0.5)

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • Hi, Vanessa. What have you tried?

  • 1

    I tried to put the argument width with equal value for all graphics, however, did not solve

  • 1

    I think it’s nice to edit your question and put this attempt you made there too, showing code as well (if applicable). The OS values many questions that the author has tried something and has not solved, so there is less chance of the question being closed... besides that it helps people understand what they tried to do and discard as a possible answer.

  • Where do the data from the second graph come from? We only have the data and code from the first.

1 answer

1

Note: The following solution may not be what is required.

To automatically ensure that the bars are of the same width, I will join the two datasets creating a new column, Data, which says which data set, "Dengue" or "Ebola", and plot the graphs in two facets.

For that, I’ll use the package dplyr.

First, see the final (temporary) dataset, which will be passed to ggplot.

library(dplyr)
library(ggplot2)

ebov_data %>%
  mutate(Data = "Ebola") %>%
  rename(Acession_number = ends_with("acession_number")) %>%
  bind_rows(dengue_data %>%
              mutate(Data = "Dengue") %>%
              rename(Acession_number = ends_with("acession_number")))
#   Acession_number  status samples   Data
#1      PRJNA352396 control      16  Ebola
#2      PRJNA352396 patient     158  Ebola
#3         GSE51808 control      28 Dengue
#4         GSE51808 patient      28 Dengue
#5         GSE28991 control      11 Dengue
#6         GSE28991 patient      22 Dengue
#7         GSE28988 control      96 Dengue
#8         GSE28988 patient     210 Dengue
#9         GSE28405 control      31 Dengue
#10        GSE28405 patient      62 Dengue
#11        GSE25001 control      34 Dengue
#12        GSE25001 patient     175 Dengue
#13        GSE13052 control      12 Dengue
#14        GSE13052 patient      18 Dengue
#15       GSE116672 control       4 Dengue
#16       GSE116672 patient       6 Dengue

Warning messages:
1: In bind_rows_(x, .id) Unequal factor levels: coercing to Character
2: In bind_rows_(x, . id) :
Binding Character and factor vector, coercing into Character vector
3: In bind_rows_(x, . id) :
Binding Character and factor vector, coercing into Character vector

Now just pass it on to one more pipe %>% at the ggplot, that has some changes. The trick is in the arguments scales and space of facet_grid. These arguments modify the widths of facets and therefore of members.

ebov_data %>%
  mutate(Data = "Ebola") %>%
  rename(Acession_number = ends_with("acession_number")) %>%
  bind_rows(dengue_data %>%
              mutate(Data = "Dengue") %>%
              rename(Acession_number = ends_with("acession_number"))) %>%
  ggplot(aes(x = Acession_number, y = samples, fill = status)) +
  geom_bar(stat = "identity",
           position = position_dodge(width = 0.9)) +
  geom_text(aes(label=samples),
            position = position_dodge(width = 0.9),
            vjust=-0.5) +
  scale_fill_manual(values=c("#00BFC4", "#F8766D")) +
  ggtitle("Dengue and Ebola Studies") +
  labs(y = "number of samples", x = "Acession Number") +
  theme(plot.title = element_text(hjust = 0.5),
        axis.ticks.x = element_blank(),
        strip.text.x = element_text(size = 10),
        strip.background = element_blank()) +
  facet_grid(~ Data, scales = "free_x", space = "free_x")

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.