0

Im trying to create a three way interaction plot but my x axis labels not according to my independent variable of P treatment which is "0,3,6,12,36".

I have tried to put in the code, levels = c("0","3","6","12","36") but it throws out this....

enter image description here

ggplot(tg2, aes(
         x = factor(interaction(cultivar, P),levels = c("0","3","6","12","36")),
         y = shoot, fill = waterlogg)) + 
  geom_bar(stat="identity", position = "dodge",colour="black",
           size=.1)+
  geom_errorbar(aes(ymin=shoot-se, ymax=shoot+se),
                size=0.3,
                width=.2,            
                position=position_dodge(.9))+
  scale_x_discrete(drop = FALSE)+
  facet_wrap(~cultivar, ncol=2)
TobiO
  • 1,335
  • 1
  • 9
  • 24
Eliott Reed
  • 351
  • 1
  • 4
  • 12

1 Answers1

0

Without a small example of reproducible data, it is hard to propose a good answer but based on the code you wrote, I have the feeling that trying to do the interaction of cultivar with P generates NA, so instead, maybe you should try:

library(ggplot2)
ggplot(tg2, aes( x = as.factor(P), y = shoot, fill = waterlogg))+
  geom_bar(stat = "identity", position = "dodge")+
  geom_errorbar((aes(ymin = shoot-se, ymax = shoot+se)), size = 0.3, 
                width = 0.2,
                position = position_dodge(.9))+
  scale_x_discrete(breaks = c("0","3","6","12","36"))+
  facet_wrap(.~cultivar)

Does it look better ?

If not, please consider providing a reproducible example of your data (see: How to make a great R reproducible example)

dc37
  • 15,840
  • 4
  • 15
  • 32
  • That worked! Than you very much! It worked without needing to add this line...+ scale_x_discrete(breaks = c("0","3","6","12","36")) – Eliott Reed Dec 27 '19 at 06:28
  • Glad it help you to solve your issue. Please next time consider to provide a reproducible example of your dataset, it will be much easier to help you ;) – dc37 Dec 27 '19 at 07:17