-1

Could you please have a look on this code and let me know how to reorder the levels to start with sucrose, p:c, p:c+u, p:c:l+u? and also for the legend to start with the control group?

Here is my code:

    Nutrient <- as.factor(c("s", "p:c", "p:c+u", "p:c:l+u"))
    Nutrientlevels<-c("s", "p:c", "p:c+u", "p:c:l+u")
    levels(Nutrient) <- Nutrientlevels
    ggplot(df,aes(x = Nutrient, y = Consumption),levels)+
    geom_bar(aes(fill = Diet), stat = "identity",position = "dodge")+
    scale_fill_brewer(palette="Set3")+
    levels(Nutrient)+
    xlab("Nutrient")+
    ylab("Average consumption (mg)")+
    ggtitle("Average daily consumption")+
    theme(panel.grid.major = element_blank(), panel.grid.minor = 
    element_blank(),panel.background = element_blank(), axis.line = 
    element_line(colour = "black"))+
    guides(color=guide_legend(override.aes=list(fill=NA)))+
    theme_bw() +
    theme(panel.border = element_blank(), panel.grid.major = 
    element_blank(),panel.grid.minor = element_blank(), 
    axis.line = element_line(colour="black"))+
    theme(plot.title = element_text(hjust = 0.5))

Thanks so much

Sally
  • 17
  • 3
  • Thank you for your reply.. It still doesn't order the groups – Sally Jul 08 '18 at 23:02
  • How to do it then? – Sally Jul 08 '18 at 23:03
  • 2
    There are several unrelated mistakes in this code that should be dealt with separately and before this question. For one thing you can't just stick `levels(Nutrient)+` randomly in the `gpplot2` code. – Hack-R Jul 08 '18 at 23:06
  • 1
    Please provide some data (some or all of data frame `df`) to [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – neilfws Jul 08 '18 at 23:07
  • Your code is creating a vector of factors, `Nutrient`, with the correct levels. But then you use a data frame `df`, with a column named `Nutrient`. They are not the same thing unless you assign the vector to the column. – neilfws Jul 08 '18 at 23:17
  • I can't share the data unfortunately. Is there any way to help without sharing the data? – Sally Jul 08 '18 at 23:27
  • They are the same neilfws – Sally Jul 08 '18 at 23:36
  • 2
    @Sweet Most people have confidential data. However that doesn't mean that you don't have to make reproducible data anyway. This is discussed in the links I shared. It's your job to create fake data that has the same relevant properties as the original data. – Hack-R Jul 08 '18 at 23:38
  • @Hack-R Here is my data frame: df – Sally Jul 09 '18 at 00:02

1 Answers1

0

Maybe this would work?

Nutrientlevels<- ordered("s", "p:c", "p:c+u", "p:c:l+u")

Anxofs
  • 11
  • 5