-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 <- read.table(text = " Diet consumption control s 0.01 1:7 s 0.01 1:5 s 0.0101 control p:c 0.01 1:7 p:c 0.01333 1:5 p:c 0.1 control p:c+u 0.009 1:7 p:c+u 0.012 1:5 p:c+u 0.001 control p:c:l+u 0.007 1:7 p:c:l+u 0.005 1:5 p:c:l+u 0.003",header = TRUE,sep = "") – 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
  • I stll get this Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object? – Sally Jul 08 '18 at 23:06
  • Try removing these three lines instead of the above oneNutrient <- 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 – Anxofs Jul 08 '18 at 23:10
  • then how to write the code with ggplot2? Hack-R is correct but don't know how to write it? – Sally Jul 08 '18 at 23:34
  • df$Nutrient<- ordered("s", "p:c", "p:c+u", "p:c:l+u") then ggplot(df,aes(x = Nutrient, y = Consumption))+etc – Anxofs Jul 08 '18 at 23:36
  • I got this error non-numeric argument to binary operator In addition: Warning message: Incompatible methods ("Ops.ordered", "+.gg") for "+" – Sally Jul 08 '18 at 23:44
  • It is hard to help with no reproducible example, but try this df$Nutrient <- as.factor(c("s", "p:c", "p:c+u", "p:c:l+u")) ### ggplot(data=df, aes(x=Nutrient, y=Consumption, fill=Diet)) + geom_bar(stat="identity", position=position_dodge()) – Anxofs Jul 08 '18 at 23:48
  • I did, it cancelled the groups and created only one group! – Sally Jul 09 '18 at 00:07
  • I added the data frame above – Sally Jul 09 '18 at 00:08
  • try adding next to df$Nutrient <- as.factor(c("s", "p:c", "p:c+u", "p:c:l+u")), the line with df$Diet <- as.factor(c("control",etc...),. It is not clear to me from the df example you typed above what are the levels within diet are – Anxofs Jul 09 '18 at 00:23
  • I have diets: control, 1:7 and 1:5. Each diet includes nutrients: s, p:c, p:c+u, p:c:l+u – Sally Jul 09 '18 at 07:44