0

I am using below codes

p <- ggplot() +
       geom_bar(data=filter(df, variable=="LA"), aes(x=Gen, y=Mean, fill=Leaf), 
       stat="identity", position="dodge")+
       geom_point(data=filter(df, variable=="TT"),aes(x=Gen, y=Mean, colour=Leaf))+
       geom_line(data=filter(df, variable=="TT"), aes(x=Gen, y=Mean, group=Leaf))+
       ggtitle("G")+xlab("Genotypes")+ylab("Canopy temperature")+
       scale_fill_hue(name="", labels=c("Leaf-1", "Leaf-2", "Leaf-3"))+ 
       scale_y_continuous(sec.axis=sec_axis(~./20, name="2nd Y-axis"))+
       theme(axis.text.x=element_text(angle=90, hjust=1), legend.position="top")

graph produced from above code I want graph like that

data

https://docs.google.com/spreadsheets/d/1Fjmg-l0WTL7jhEqwwtC4RXY_9VQV9GOBliFq_3G1f8I/edit#gid=0

From data, I want variable LA to left side and TT from right side

Above part is resolved,

Now, I am trying to put errorbars on the bar graph with below code, it caused an error, can someone have a look for solution?

p + geom_errorbar(aes(ymin=Mean-se, ymax=Mean+se), width=0.5,
        position=position_dodge(0.9), colour="black", size=.7)
washfaq
  • 268
  • 2
  • 12
  • `dput(yourdataframename)` in R then paste output here. Read [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Daman deep Dec 17 '20 at 06:16
  • I have updated the df, plz have a look – washfaq Dec 17 '20 at 07:26
  • I am not able to read data it's not reproducible. Open new scrip store the result in a variable you will see an error fix that. – Daman deep Dec 17 '20 at 07:33
  • There are multiple things that need to be clarify: Which variable is the left Y-Axis & which variable is the right Axis? Please consider put in more details of what you want the graph to be in words. – Sinh Nguyen Dec 17 '20 at 07:48

1 Answers1

2

For this you need to understand that even you have the second Y-Axis, it is just a markup and everything draw on the graph is still base on the main Y-Axis(left one).

So you need to do two things:

  1. Convert anything that should reference to the second Y-Axis to same scale of the one on the left, in this case is the bar scale (LA variables) whose maximum is 15. So you need to divide the value of TT by 20.
  2. Second Axis needs to label correctly so it will be the main Y-Axis multiply by 20.
p <- ggplot() +
  geom_bar(data=filter(df, variable=="LA"), aes(x=Gen, y=Mean, fill=Leaf), 
    stat="identity", position="dodge") +
  # values are divided by 20 to be in the same value range of bar graph
  geom_point(data=filter(df, variable=="TT"),aes(x=Gen, y=Mean/20, colour=Leaf))+
  geom_line(data=filter(df, variable=="TT"), aes(x=Gen, y=Mean/20, group=Leaf))+
  ggtitle("G")+xlab("Genotypes")+ylab("Canopy temperature")+
  scale_fill_hue(name="", labels=c("Leaf-1", "Leaf-2", "Leaf-3"))+ 
  # second axis is multiply by 20 to reflect the actual value of lines & points
  scale_y_continuous(
    sec.axis=sec_axis(trans = ~ . * 20, name="2nd Y-axis",
      breaks = c(0, 100, 200, 300))) +
  theme(axis.text.x=element_text(angle=90, hjust=1), legend.position="top")

enter image description here

For the error par which is very basic here. You will need to adjust the theme and the graph to have a good looking one.

p + geom_errorbar(data = filter(df, variable=="TT"),
   aes(x = Gen, y=Mean/20, ymin=(Mean-se)/20,
   ymax=(Mean+se)/20), width=0.5,                  
   position=position_dodge(0.9), colour="black", size=.7)

One final note: Please consider reading the error message, understand what it say, reference to the help document of packages, functions in R so you can learn how to do all the code yourself.

Sinh Nguyen
  • 4,277
  • 3
  • 18
  • 26
  • Now, I am trying to put errorbars on the bar graph with below code, it caused an error, can someone have a look for solution? p + geom_errorbar(aes(ymin=Mean-se, ymax=Mean+se), width=0.5, position=position_dodge(0.9), colour="black", size=.7) – washfaq Dec 17 '20 at 15:17
  • Thank you Sinh for your help, very much appreciated. – washfaq Dec 18 '20 at 15:16