0

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.

bdemarest
  • 14,397
  • 3
  • 53
  • 56
Agus camacho
  • 868
  • 2
  • 9
  • 24

1 Answers1

1

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)

enter image description here

bdemarest
  • 14,397
  • 3
  • 53
  • 56
  • That is great! However I now cannot change the name of the response variable. – Agus camacho Feb 14 '17 at 11:50
  • 1
    @Aguscamacho, I'm not sure I understand the problem. You can still add `+ ylab("Escape Strategy")` to change the y-axis label. Also, maybe you want to try `geom_bar(position="stack")` and `geom_bar(position="dodge")` to show counts on the y-axix (instead of fraction). – bdemarest Feb 14 '17 at 18:53
  • I cannot understand why I could not yesterday and can today. Thanks for the other advice too! – Agus camacho Feb 15 '17 at 13:24