难以让线型传奇出现在ggplot2中

时间:2017-11-27 21:44:51

标签: r ggplot2 line legend

 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

我正在尝试为此图表添加线型图例。提供的代码从我的数据集生成图形。但是,我需要一个图例来描述每一行的含义,每当我尝试scale_linetype或scale_colour函数时,我什么也得不到。我甚至无法得到一个传奇出现。我尝试在aes函数中添加线型和颜色,但这也没有帮助。我需要用实线读取"氧气",虚线读取"坦克水"和虚线读取"硫化物"。请帮忙!

0 个答案:

没有答案