I am trying to create a faceted plot, where each group has a different number of data points and different x-axis labels. For each plot, faceted by SID_2, I want the x-axis to be the FULL.STATUS in its current order (i.e. Prior to SBV/MET, During MET, After MET), with the x-axis labeled for every data point. To overcome this and preserve the current order, I set x = (paste0(SERIAL, FULL.STATUS)). However, I either can't get the labels to work correctly after the first plot (first code example) or get the labels to change at all (second code example below)
I have tried too many things to list them all, but a few things are:
# Create a vector of ordered labels of Status IDs
labs <- df$FULL.STATUS
labs2 <- paste0(df$SERIAL, df$FULL.STATUS)
final <- setNames(labs2, labs)
final.factors <- factor(final, levels = unique(final))
# Plot total abundance profiles with updated qPCR data
ggplot(df, aes(x = (paste0(SERIAL, FULL.STATUS)), y = qPCR, group = SID_2)) +
geom_line() +
geom_point(size = 1) +
facet_wrap(~SID_2, scales = "free", nrow = 2) +
theme_bw() +
theme(legend.position = "none", text = element_text(size = 6), axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_x_discrete(labels = labs) +
ylab("Total Abundance") +
xlab("") +
ggtitle("Met Study - Complete qPCR Data")
# Create a vector of ordered labels of Status IDs
labs <- df$FULL.STATUS
labs2 <- paste0(df$SERIAL, df$FULL.STATUS)
final <- setNames(labs2, labs)
final.factors <- factor(final, levels = unique(final))
# Plot total abundance profiles with updated qPCR data
ggplot(df, aes(x = (paste0(SERIAL, FULL.STATUS)), y = qPCR, group = SID_2)) +
geom_line() +
geom_point(size = 1) +
facet_wrap(~SID_2, scales = "free", nrow = 2) +
theme_bw() +
theme(legend.position = "none", text = element_text(size = 6), axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_x_discrete(labels = final.factors) +
ylab("Total Abundance") +
xlab("") +
ggtitle("Met Study - Complete qPCR Data")
If someone could please help me, I would truly appreciate it! Thank you!
I figured it out using a combination of tidytext and vectors, although it probably isn't the most graceful code. I created the most unique x-axis variable names and vector with the same names. I used that vector to set the factor levels of the dataframe and then scale_x_reordered() to remove text from axis labels.
# Create a vector of ordered labels of Status IDs
labs <- df$FULL.STATUS
lvls <- paste0(df$FULL.STATUS,"___",df$SERIAL,df$ID)
unique.lvls <- lvls[!duplicated(lvls)]
# Reformat FULL.STATUS column so x-axis labels are correct in faceted
df <- df %>%
mutate(FULL.STATUS = reorder_within(FULL.STATUS, ID, SERIAL),
FULL.STATUS = paste0(FULL.STATUS, ID))
df$FULL.STATUS <- factor(df$FULL.STATUS, levels = unique.lvls)
# Plot total abundance profiles with updated qPCR data
ggplot(df, aes(x = FULL.STATUS, y = qPCR, group = SID_2)) +
geom_line() +
geom_point(size = 1) +
facet_wrap(~SID_2, scales = "free", nrow = 2) +
theme_bw() +
theme(legend.position = "none", text = element_text(size = 6), axis.text.x = element_text(angle = 90, hjust = 1)) +
ylab("Total Abundance") +
xlab("") +
scale_x_reordered() +
ggtitle("Met Study - Complete qPCR Data")
I hope this helps someone!