I have a dataset with two facors (f1 and f2) and two responses (r1 and r2). For the f1, I although in the dataset it is coded with A, B, C, etc, I want to use specific names. However, when I tray to add a scale_x_discrete() RStudio is not capable of compiling the code.
Here is a MWE that actually works:
library(ggplot2)
library(ggpubr)
library(tidyverse)
library(car)
library(multcomp)
library(palmerpenguins)
library(ggpattern)
library(multcompView)
library(egg)
dput(example)
structure(list(f1 = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L,
5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 1L,
1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L,
9L, 10L, 10L, 11L, 11L), levels = c("A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K"), class = "factor"), f2 = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("1",
"2"), class = "factor"), r1 = c(10, 11, 12, 14, 11, 13, 18, 19.5,
20, 19, 23, 25, 5, 7, 9, 12, 21, 24, 9, 8, 15, 13.5, 8, 9, 10,
12, 9, 11, 16, 17.5, 18, 17, 21, 23, 3, 5, 7, 10, 19, 22, 7,
6, 13, 11.5), r2 = c(7, 8, 9, 11, 8, 10, 19.5, 21, 21.5, 20.5,
18, 20, 0, 2, 4, 7, 16, 19, 4, 3, 10, 8.5, 8, 9, 10, 12, 9, 11,
20.5, 22, 22.5, 21.5, 19, 21, 1, 3, 5, 8, 17, 20, 5, 4, 11, 9.5
)), row.names = c(NA, -44L), class = c("tbl_df", "tbl", "data.frame"
))
example
example$f1 <- as.factor(example$f1)
example$f2 <- as.factor(example$f2)
example_r1_anova<- aov(r1 ~ f1*f2, data = example)
summary(example_r1_anova)
example_r1_summary<-group_by(example, f1, f2) %>%
summarise(mean=mean(r1), sd=sd(r1))%>%
arrange(desc(mean))
print(example_r1_summary)
tukey_example_r1 <- TukeyHSD(example_r1_anova)
tukey_example_r1
example_r1_tukey.cld <- multcompLetters4(example_r1_anova, tukey_example_r1)
example_r1_tukey.cld
example_r1_cld <- as.data.frame.list(example_r1_tukey.cld$`f1:f2`)
example_r1_cld
example_r1_summary$Tukey <- example_r1_cld$Letters
print(example_r1_summary)
example_r1_summary$r <- rep(c("r1"),each=22)
example_r1_summary
example_r2_anova<- aov(r2 ~ f1*f2, data = example)
summary(example_r2_anova)
example_r2_summary<-group_by(example, f1, f2) %>%
summarise(mean=mean(r2), sd=sd(r2))%>%
arrange(desc(mean))
print(example_r2_summary)
tukey_example_r2 <- TukeyHSD(example_r2_anova)
tukey_example_r2
example_r2_tukey.cld <- multcompLetters4(example_r2_anova, tukey_example_r2)
example_r2_tukey.cld
example_r2_cld <- as.data.frame.list(example_r2_tukey.cld$`f1:f2`)
example_r2_cld
example_r2_summary$Tukey <- example_r2_cld$Letters
print(example_r2_summary)
example_r2_summary$r <- rep(c("r2"),each=22)
example_r2_summary
example_r <- rbind(example_r1_summary, example_r2_summary)
example_r$r <- as.factor(example_r$r)
ggplot(example_r, aes(x = f1, y = mean, fill = r, pattern = f2)) +
geom_bar(stat = "identity", color="black",position = "dodge", alpha = 1, width=0.7) +
geom_col_pattern(alpha=0.5, color="grey20",width=0.7, position="dodge", pattern_fill="black", pattern_density=0.1, pattern_angle=45, pattern_spacing=0.015, pattern_key_scale_factor=0.6)+
scale_pattern_manual(name="f2", values = c("1" = "none", "2" = "circle"),labels = c("f21", "f22")) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), color="black", position = position_dodge(0.7), width = 0.25, show.legend = FALSE) +
geom_text(aes(label=Tukey, y=mean+sd), vjust=-0.5, position = position_dodge(0.7), size = 6.5, colour = "gray25") +
scale_fill_manual(name="r",values = c("#E7B800", "#CC79A7"), guide = guide_legend(label.hjust = 0, override.aes = list(pattern = "none")))+
theme_bw(base_size = 20) +
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(),axis.text=element_text(size=20), axis.title=element_text(size=22,face="bold"), legend.title=element_text(size=20, face="bold"),legend.text=element_text(size=20), axis.text.x=element_text(angle = 45, hjust = 1),legend.position = c(0.5, 0.9),legend.box = "horizontal")+
scale_y_continuous(expand = c(0, 0),limits=c(0, 35))
The result is the following figure:
However, when I add the discrete scale in the x-axis, I get nothing but a Error in seq.default(from, to, by) : invalid '(to - from)/by'
ggplot(example_r, aes(x = f1, y = mean, fill = r, pattern = f2)) +
geom_bar(stat = "identity", color="black",position = "dodge", alpha = 1, width=0.7) +
geom_col_pattern(alpha=0.5, color="grey20",width=0.7, position="dodge", pattern_fill="black", pattern_density=0.1, pattern_angle=45, pattern_spacing=0.015, pattern_key_scale_factor=0.6)+
scale_pattern_manual(name="f2", values = c("1" = "none", "2" = "circle"),labels = c("f21", "f22")) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), color="black", position = position_dodge(0.7), width = 0.25, show.legend = FALSE) +
geom_text(aes(label=Tukey, y=mean+sd), vjust=-0.5, position = position_dodge(0.7), size = 6.5, colour = "gray25") +
scale_fill_manual(name="r",values = c("#E7B800", "#CC79A7"), guide = guide_legend(label.hjust = 0, override.aes = list(pattern = "none")))+
theme_bw(base_size = 20) +
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(),axis.text=element_text(size=20), axis.title=element_text(size=22,face="bold"), legend.title=element_text(size=20, face="bold"),legend.text=element_text(size=20), axis.text.x=element_text(angle = 45, hjust = 1),legend.position = c(0.5, 0.9),legend.box = "horizontal")+
scale_x_discrete(name="", labels = c(
`A`= expression(ChCl:Lactic~acid),
`B` = expression(ChCl:2~Lactic~acid),
`C` = expression(ChCl:3~Lactic~acid),
`D` = expression(ChCl:Oxalic~acid),
`E` = expression(ChCl:"1,4"-butanediol:Water),
`F` = expression(ChCl:2~Levulinic~acid),
`G` = expression(ChCl:2~Urea),
`H` = expression(ChCl:2~Glycerol),
`I` = expression(Betaine:2~Levulinic~acid),
`J` = expression(Betaine:2~Urea:Water),
`K` = expression(Betaine:2~Lactic~acid)))+
scale_y_continuous(expand = c(0, 0),limits=c(0, 35))
I guess the problem is in the expression() of the x-axis labels, but I don´t know any other way to do it.

Following from stefan's identification of the underlying issue, you can keep
geom_col_patternand rotated axis text by initially setting the angle to 0, and then manually changing it to 45 in the gtable.