我有一个包含两个因素(
f1
和f2
)和两个响应(r1
和r2
)的数据集。对于f1
,虽然在数据集中它是用A、B、C等编码的,但我想使用特定的名称。然而,当我添加一个 scale_x_discrete()
时,RStudio无法编译代码。
这是一个实际有效的 MWE:
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))
结果如下图:
但是,当我在 x 轴上添加离散刻度时,除了
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))
我猜问题出在 x 轴标签的
expression()
上,但我不知道还有其他方法可以做到这一点。
根据 stefan 对根本问题的识别,您可以通过最初将角度设置为 0,然后在 gtable 中手动将其更改为 45,来保留
geom_col_pattern
和旋转轴文本。
p1 <- 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 = 0, hjust = 1),
legend.position = c(0.5, 0.9),
legend.box = "horizontal") +
scale_x_manual(name="",
values = 1:11,
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))
p2 <- ggplot_gtable(ggplot_build(p1))
p2$grobs[[7]]$children[[2]]$grobs[[2]]$children[[1]]$rot <- 45
grid::grid.newpage()
grid::grid.draw(p2)