Box Plot Help for Undergraduate

Hi yall,
I have a real quick question about my boxplot. I wanted to know if there was any code that I could use in ggplots/tidyverse to make sure that my X-axis values were aligned properly with there respective ticks. I have attached a figure of my boxplot so I could show how they are not aligned. Also, does anyone know how I can fill in the boxes with their respective colors? I will also post my code down below just in case. Also, my Asterisk are not aligned properly.

Code -
df <- Normalized_Ranged_Data_of_TF_for_summer_project
new_df <- df %>%
gather(key = "mutant", value = "amount") %>% filter(complete.cases(.))

new_df$mutant <- as.character(new_df$mutant)

new_df$mutant <- factor(new_df$mutant, levels=unique(new_df$mutant))

p <- new_df %>%
ggplot(aes(x=mutant, y=amount)) + geom_boxplot(col=ifelse(colnames(NF)=='TF33'|colnames(NF)=='TF59'|colnames(NF)=='TF117'|colnames(NF)=='TF122'|colnames(NF)=='TF133'|colnames(NF)=='TF137'|colnames(NF)=='TF156'|colnames(NF)=='TF164'|colnames(NF)=='TF169'|colnames(NF)=='TF172'|colnames(NF)=='TF175',"blue","Orange"))

p <- p + theme(axis.text.x = element_text(angle = 90, hjust = 1), text=element_text(size=8,family="Serif")) +
ggtitle("Flocculation Level of Mutants ") +
xlab("Mutant Transcription Factors") + ylab("Degree of Flocculation")

f <- p + geom_text(aes(x = "SN250", y = 1.5, label = "")) + geom_text(aes(x = "TF33", y = 2, label = "")) + geom_text(aes(x = "TF59", y = 2, label = "")) + geom_text(aes(x = "TF117", y = 2, label = "")) + geom_text(aes(x = "TF122", y = 2, label = "")) + geom_text(aes(x = "TF132", y = 2, label = "")) + geom_text(aes(x = "TF137", y = 2, label = "")) + geom_text(aes(x = "TF156", y = 2, label = "")) + geom_text(aes(x = "TF164", y = 2, label = "")) + geom_text(aes(x = "TF169", y = 2, label = "")) + geom_text(aes(x = "TF172", y = 2, label = "")) + geom_text(aes(x = "TF175", y = 2, label = ""))

f <- f + geom_hline(yintercept=1, linetype="dashed", color = "red")

Thank you all for your help on my previous questions,
Gabriel Viramontes

Here is a small example of how to align the x axis labels and fill the box plots. Notice that I add a column to the data frame to define a Group for each row and I use that to control the fill color in combination with scale_fill_manual. I put in a comment to show how to control the color of the lines defining the box plots with scale_color_manual.
The xaxis labels are adjusted by passing a value to vjust. That is surprising but is a result of rotating the text.
I do not understand what you mean by the asterisks being misaligned. Can you explain that.

DF <- data.frame(mutant = rep(c("TF59", "TF117", "TF122"), each = 25),
                 amount = runif(75, min = 0, max = 2))
library(ggplot2)
library(dplyr)
colors = c(Grp1 = "orange", Grp2 = "blue")
DF <- DF %>% mutate(Group = ifelse(mutant %in% c("TF59", "TF122"), "Grp1", "Grp2"))
ggplot(DF, aes(x= mutant, y = amount, fill = Group)) + 
  geom_boxplot() +
  scale_fill_manual(values = colors) +
  #scale_color_manual(values = colors) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

Created on 2020-06-11 by the reprex package (v0.3.0)

The issue I am having is that after adding in the asterisk, my shaded tfs in this case in blue change, they do not stay the same ones. So instead of tf59 being shaded in blue it changes, while the asterisk stays on top of tf59

I would add asterisks as shown in the code below.

DF <- data.frame(mutant = rep(c("TF59", "TF117", "TF122"), each = 25),
                 amount = runif(75, min = 0, max = 2))
library(ggplot2)
library(dplyr)

colors = c(Grp1 = "orange", Grp2 = "blue")
DF <- DF %>% mutate(Group = ifelse(mutant %in% c("TF59", "TF122"), "Grp1", "Grp2"))
DFmark <- data.frame(mutant = unique(DF$mutant), amount = 2)
DFmark <- DFmark %>% mutate(Marker = ifelse(mutant %in% c("TF59", "TF122"), "", "*"))
ggplot() + 
  geom_boxplot(mapping = aes(x= mutant, y = amount, fill = Group), data = DF) +
  geom_text(aes(x = mutant, y = amount, label = Marker), data = DFmark) +
  scale_fill_manual(values = colors) +
  #scale_color_manual(values = colors) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

Created on 2020-06-12 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.