我试图将我的数据表示为方框图,我的数据框目前看起来如下:
V1 V2 V3 V4 V5
1 1 12.18 FEMALE A_ambiguus Host
2 2 11.81 FEMALE A_ambiguus Host
3 3 10.70 MALE A_ambiguus Host
4 4 11.07 MALE A_ambiguus Host
5 5 7.95 FEMALE A_ameliae Parasite
6 6 7.42 FEMALE A_ameliae Parasite
我运行以下脚本并生成一个图形,其中物种(V4)为x轴,总长度(V2)为y轴,按V2排序,并用V5着色。
box <- ggplot(TL_sub, aes(x = V4, y = V2, group = V4)) +
scale_y_continuous(name = "TL (mm)") +
theme(axis.text.x=element_text(angle = 45, hjust = 1)) +
geom_boxplot(aes(fill=Condition)) +
aes(x=reorder(V4,V2),y=V2,label=TL)
box
问题在于,当我运行
时box + facet_grid(. ~ V5)
目标是创建两个按性别划分的图(V3),但它不起作用。我收到以下错误:
Error in combine_vars(data, params$plot_env, cols, drop = params$drop) :
At least one layer must contain all variables used for facetting
如果需要,我可以提供完整的数据集。
任何帮助都会很棒! 谢谢, 史蒂文M。
答案 0 :(得分:3)
使用完整的数据集,这对我来说很好。
TL_subset %>%
ggplot(aes(reorder(Species, TL), TL)) +
geom_boxplot(aes(fill = Condition)) +
labs(x = "Species", y = "TL (mm)") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
facet_grid(. ~ Sex)
答案 1 :(得分:0)
以下是您的示例数据中的另一个示例
dataset<-data.frame(V2=c(12.18,11.81,10.70,11.07,7.95,7.42),
V3=c("FEMALE","FEMALE","Male","Male","FEMALE","FEMALE"),
V4=c("A_ambiguus","A_ambiguus","A_ambiguus","A_ambiguus","A_ameliae","A_ameliae"),
V5=c("Host","Host","Host",'Host',"Parasite","Parasite"))
library(ggplot2)
ggplot(data=dataset,aes(x=V4,y=V2)) + geom_boxplot(aes(fill=V5))+facet_grid(.~V3) +xlab("Species") +
ylab("TL (mm)") + scale_fill_discrete(name="Condition")