Reputation: 898
using these data, i want to simultaneously plot the effects of two categorical factors over a multinomial variable. This is as far as ai could go:
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")
I am after a graph in which for each treatment there are two bars, one for each morph, and within each bar, the frequency of each option is shown as color ocuppying a proportional length of the solid bar.
Thanks in advance.
Upvotes: 0
Views: 344
Reputation: 14667
Here is one approach for displaying bar plots for three categorical variables. I have added a minimal example data set for reproducibility (based on your posted data).
# 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)
Upvotes: 1