0
 head(run2)
        Time5     Time15   Oxygen     Tank Sulfide
1 0.000000000 0.00000000 7.553702 19.27838    2.79
2 0.001388889 0.00416667 7.573448 19.25815    2.75
3 0.002777778 0.00833334 7.540538 19.31884    2.83
4 0.004166667 0.01250001 7.553702 19.29861    2.81
5 0.005555556 0.01666668 7.543829 19.27838    2.80
6 0.006944444 0.02083335 7.563575 19.33907    2.83

  p2 = ggplot() +
      geom_line(data = run2, linetype="solid", aes(x = run2$Time5, y = run2$Oxygen), size = 2, color = "black") + scale_size_discrete(range = c(1, 2))+
      geom_line(data = run2, linetype="longdash", aes(x = run2$Time15, y = run2$Tank), size = 2, color = "Black") + scale_size_discrete(range = c(1, 2))+
      geom_line(data = run2, linetype="dotted", aes(x = run2$Time5, y = (run2$Sulfide)/49), size = 2, color = "black") + scale_size_discrete(range = c(1, 2))+
      scale_x_continuous(expand = c(0, 0), breaks = c(0,1,2,3,4,5,6), limits = c(0,6)) + 
      scale_y_continuous(expand = c(0, 0), breaks = c(0,4,8,12,16,20), limits = c(0,20), 
                         sec.axis=sec_axis(~.*50, breaks = c(200,400,600,800,1000), 
                                           name = expression(paste("Hydrogen Sulfide Concentration  (  ",  mu, "M H" [" 2"], "S)")))) +
      labs(x = "",
           y = "Oxygen Partial Pressure (kPa)",
           subtitle = "(b)") +
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
            panel.background = element_blank(), axis.line = element_line(colour = "black"),
            text=element_text(size=16,  family="TT Arial"),
            axis.text = element_text(family="TT Arial", size=12, colour="black")
           )

Resulting Graph

I am trying to add a line type legend to this graph. The supplied code produces the graph from my data set. However, I need a legend to describe what each line means and everytime I try the scale_linetype or scale_colour functions I get nothing. I cant even get a legend to show up. I tried adding linetypes and colors to the aes function but that did not help either. I need the solid line to read "Oxygen", the dashed line to read "Tank water" and the dotted line to read "Sulfide". Please help!

  • 1
    Legends are created automatically for variables mapped in `aes()`. You need to put your data in long format so you have a single column for `y` column and a single column `linetype`, then a single `geom_line` call, and your legend will be there! (Also, **never** use `data$` inside `aes()`. Just use the unquoted column name.) – Gregor Thomas Nov 27 '17 at 21:56
  • Here is a quick reproducible example that might help you see what @Gregor is talking about: `library(ggplot2); ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, linetype=Species)) + geom_smooth(se=FALSE)`. If you need help reshaping your data, you will need to post the data (or a small representative subset of the data) in a reproducible form. – bdemarest Nov 27 '17 at 22:02
  • Right now I have vertical columns "time", "oxygen", and "sulfide" with the first row being the names. How do I transform to use the aes function? – Chris Johnson Nov 27 '17 at 22:13
  • Please edit data into your question. Probably just a few rows are needed - `dput(head(your_data))` is the best way to share because it is copy/pastable. – Gregor Thomas Nov 27 '17 at 22:17
  • @bdemarest I have added the data to the code in the question – Chris Johnson Nov 27 '17 at 22:21

0 Answers0