绘制两个分类因子对 R 中无序多项式变量的影响

plot the effect of two categorical factors over an unordered multinomial variable in R

使用这些 data,我想同时绘制两个分类因子对多项变量的影响。这是 ai 所能达到的极限:

require(ggplot2)
 df=read.csv("example.csv")
 ggplot(df,aes(x=treatment , y=option, group=morph)) +
 geom_bar(stat = "identity")+
 scale_colour_brewer(palette="Set1")+
 ylab("Escape strategy")+
 xlab("Treatment")

我在看一张图表,其中每个处理都有两个条,每个变形一个,在每个条内,每个选项的频率显示为占据实心条比例长度的颜色。

提前致谢。

这是显示三个分类变量的条形图的一种方法。我添加了一个最小的示例数据集以实现可重复性(基于您发布的数据)。

# Small example data set.
dat = read.table(header=TRUE,
text="option    treatment   morph
burrow  a   c
hide    a   c
hide    a   c
hide    a   c
run a   c
run a   c
burrow  a   d
burrow  a   d
burrow  a   d
hide    a   d
run a   d
run b   c
run b   c
run b   c
burrow  b   c
hide    b   c
burrow  b   d
burrow  b   d
hide    b   d
run b   d")


library(ggplot2)

p1 = ggplot(dat, aes(x=morph, fill=option)) +
     geom_bar(position="fill") +
     scale_fill_brewer(palette="Set1") +
     facet_grid(. ~ treatment, labeller=label_both)

ggsave("bar_plot.png", p1, height=6, width=6, dpi=150)