0

I used ggplot to make a like graph with two variables, but I need to add a secondary y-axis and assign it to one of the variables ("volt").

I also would like to specify the range of the secondary y-axis (upper and lower limit), as well as the breaks - as I did for the y-main-axis.

My two variables are "Sr" and "volt".

I don't want to use different dataframes and then merge the graphs.

Do any of you know how to do it? Oh, I must add that I am an absolute beginner!

Thanks, Pedro

ggplot(data = k, aes(x = Dist)) +
geom_line(aes(y = Sr), colour="blue") +
geom_line(aes(y = volt), colour = "grey") +
xlab(bquote('Distance-um')) +
ylab(bquote('Sr87Sr86')) +
geom_point(aes(y = Sr), colour="black", size=2) +
geom_point(aes(y = volt), colour="grey", size=2) +
theme(axis.title.x = element_text(colour="black",size=10,face="bold"),
    axis.title.y = element_text(colour="black",size=10,face="bold"),
    axis.text.x = element_text(colour="black",size=8, face="plain"),
    axis.text.y = element_text(colour="black",size=8, face="plain")) +
theme(panel.background = element_rect(fill = "white")) +
theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    strip.background = element_blank(),
    panel.border = element_rect(colour = "black", fill="transparent")) +
theme(plot.title = element_text(lineheight=.8, size=10, face="bold")) +
geom_hline(aes(yintercept=0.7061), colour="black", linetype="dotted") +
geom_hline(aes(yintercept=0.7078), colour="black", linetype="dotted") +
geom_hline(aes(yintercept=0.70467), colour="black", linetype="dotted") +
scale_x_continuous(limits=c(-0.01, 1000), breaks=c(0, 250, 500, 750, 1000))+
scale_y_continuous(limits=c(0.7039, 0.7101), breaks=c(0.7040, 0.7050, 
0.7060, 0.7070, 0.7080, 0.7090)) +
theme(plot.margin = unit(c(.25,.25,.0,.0), "cm"))
Pedro Miguel
  • 39
  • 1
  • 5
  • 1
    Possible duplicate of [ggplot2: Create second y-axis on the right side for one variable](https://stackoverflow.com/questions/44911768/ggplot2-create-second-y-axis-on-the-right-side-for-one-variable) – S Rivero Jul 12 '17 at 23:20
  • Sorry, but I couldn't figure out how this previous question and answer could be used to answer my question. – Pedro Miguel Jul 13 '17 at 15:16
  • Any data to reproduce your plot? Please use `dput()` to copy your data. [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – S Rivero Jul 13 '17 at 15:28
  • Any random numbers will serve, as long as dependent variables are different enough. Dist (X) - 1 2 3 4 5 6 7 8 9 10 Sr (Y1) - 0.704 0.705 0.706 0.707 0.708 0.704 0.705 0.706 0.707 0.708 volt (Y2) - 3 5 10 8 12 4 11 3 14 22 – Pedro Miguel Jul 13 '17 at 20:29

1 Answers1

0

First, I would like to mention that two axis is not the best idea. Having said that, if you still want two axis, you have to scale one of your variables (volt in this case).

Dist<-seq(1,10)
Sr<-c(0.704, 0.705, 0.706, 0.707, 0.708, 0.704, 0.705, 0.706, 0.707, 0.708)
volt<-c(3,5,10,8,12,4,11,3,14,22)
k<-data.frame(Dist,Sr,volt)
k$volt<-k$volt/10

Now, fixing the data makes things easier for plotting, just melt your variables

library(reshape)

k_melt<-melt(k,id="Dist") 

And plotting. With sec_axis you can create the second axis and rescale again the values

ggplot(k_melt, aes(x=Dist, y=value, fill=variable, colour=variable))+ 
geom_line(stat='identity', size=0.5)+ 
geom_point(stat='identity', size=2)+scale_color_manual(values=c("blue", "grey")) +
scale_y_continuous("SR", sec.axis = sec_axis(~ . *10, name = "Volt"))`

NOTE: You can add your theme and geom_hline to this code. they don't work for the simulated data I created

S Rivero
  • 708
  • 5
  • 14