做柱状图分面,运行以下代码的时候报错
library(lme4)
library(multcomp)##pairwise comparison
library(dplyr)##data partitioning
library(lmerTest)
library(afex)#????????p??
library(RColorBrewer)
library(ggplot2)
library(Rmisc)
library(gridExtra)
library(facetscales)
library(wesanderson)
setwd("C:/Users/GXYtutu/Desktop/GXY")
Data.AWCD<-read.csv("144h.三个重复awcd.csv")
NPF.AWCD<-Data.AWCD[c(1:5,8)]
NPF.AWCD$treatment<-factor(NPF.AWCD$treatment,levels = c("1","2","3","4"),ordered = TRUE)
NPF.AWCD$Forest<-factor(NPF.AWCD$Forest,levels = c("PF","NF"),ordered = TRUE)
NPF.AWCD$classification<-factor(NPF.AWCD$classification,
levels = c(
"糖类","氨基酸类","胺类","酚酸类","聚合物类","羧酸类"),
ordered = TRUE)
library(reshape2)
levels(NPF.AWCD$treatment)[levels(NPF.AWCD$treatment)=="1"]<-"CK"
levels(NPF.AWCD$treatment)[levels(NPF.AWCD$treatment)=="2"]<-"W"
levels(NPF.AWCD$treatment)[levels(NPF.AWCD$treatment)=="3"]<-"R"
levels(NPF.AWCD$treatment)[levels(NPF.AWCD$treatment)=="4"]<-"WR"
library(dplyr)
NPF.AWCD.se<-summarySE(NPF.AWCD,measurevar = "AWCD",groupvars = c("treatment", "Forest","classification"))
scales_y.NF <- list(
'NF' = scale_y_continuous(limits = c(0,3),expand = c(0,0),breaks = seq(0,.5,3)),
'PF' = scale_y_continuous(limits = c(0,3),expand = c(0,0), breaks = seq(0,.5,3)))
legend.labs <- c(expression(C),
expression(W),
expression(R),
expression(WR))
ggplot(NPF.AWCD.se,aes(x=classification,y=AWCD,fill=treatment))+
geom_bar(stat="identity",color="black",position=position_dodge(0.5),width=0.5)+
geom_errorbar(aes(ymin=AWCD-se,ymax=AWCD+se),width=0.3,position=position_dodge(.5))+
scale_y_continuous(expand = c(0,0))+
theme_bw()+
facet_grid(~Forest)
theme(panel.grid = element_blank(),
axis.text.x = element_text(angle = 0,hjust = 0.8))+
theme(axis.ticks.length=unit(-0.2,"cm"))+
theme(axis.text.y =element_text(margin=unit(c(0.5,0.5,0.5,0.5),"cm")))+
theme(axis.text.x =element_text(margin=unit(c(0.5,0.5,0.5,0.5),"cm")))+
theme(axis.ticks.x = element_blank())+
theme(strip.background = element_blank(),
panel.grid = element_blank())+
labs(x="Classifications", y= expression(paste("Absorbrance")))+
#theme(strip.background = element_blank(), strip.text = element_blank())+# remove labels in all facet figures
theme(axis.title.y = element_text(margin = margin(t = 0, r = 0.5, b = 0, l = 0)))+
theme(axis.title.x = element_text(margin = margin(t = 0, r = 0.1, b = 0, l = 0)))+
theme(plot.margin=unit(c(1,0.5,1,0.5), "lines"))+
facet_grid_sc(rows = vars(Forest),
scales = list(y = scales_y.NF))+
scale_x_discrete(labels=c( 碳水化合物="carbohydrate",
氨基酸类="amino acid",
胺类="amine",
酚酸类="phenolic acids",
聚合物类="polymer",
羧酸类="carboxylic acids"))+
scale_fill_manual(values=c("white","darkolivegreen1","Burlywood2","Brown"))+
theme(legend.text.align = 0)+
theme(legend.title = element_blank())
ggsave("Figure 1.pdf",width = 16,height = 9,dpi = 600)
system('open "Figure 1.pdf"')
但是他报错:
Error: Can't add `facet_grid_sc(rows = vars(Forest), scales = list(y = scales_y.NPF))` to a theme object
我试过把facet_grid(~Forest)删除,但是做出来的图纵坐标没有单位刻度,就是纵坐标没有数字