-1

I need help with showing the legend for the data I plotted on both axis - two data frames with common x-axis as date, I used scaling to plot them together. However, the show.legend = true, none of them are showing on the plot.

It would be greatly appreciated if you could help me with this! Thanks!

Example Data Set test_lead: (data available once a week for 2 months)

Site   CollectDate   Param   ReportedResult
1      2019-06-17    Lead    0.6 
1      2019-06-23    Lead    0.3
1      2019-07-02    Lead    1.3
1      2019-07-10    Lead    0.4
1      2019-07-17    Lead    2.3

Example Data Set test_ortho: (data available once every 3 days for 2 months)

Site   CollectDate   Param   ReportedResult
1      2019-06-17    Ortho    0.2 
1      2019-06-20    Ortho    0.2
1      2019-06-23    Ortho    0.16
1      2019-06-26    Ortho    0.2
1      2019-06-29    Ortho    0.2
1      2019-07-02    Ortho    0.22
1      2019-07-20    Ortho    0.23
1      2019-07-29    Ortho    0.3

Here's my code:

test1 <- ggplot()+

geom_point(mapping = aes(x=test_lead$CollectDate, y=test_lead$ReportedResult), colour = "blue",  show.legend = TRUE) +

geom_line(mapping = aes(x=test_lead$CollectDate, y=test_lead$ReportedResult), colour = "blue", show.legend = TRUE) +

geom_point(mapping = aes(x=test_ph$CollectDate, y=test_ph$ReportedResult*27.2/1.920),colour = "hotpink", show.legend = TRUE) +

geom_line(mapping = aes(x=test_ph$CollectDate, y=test_ph$ReportedResult*27.2/1.920), colour = "hotpink", show.legend = TRUE) +
 scale_x_date(name = "", date_breaks = "2 week", date_labels = "%Y/%m/%d") +
theme(
    plot.title = element_text(size = 16, 
                              face = "bold",
                              family = "sans",
                              color = "black",
                              hjust = 0.5,
                              lineheight = 1.2 ), 
    axis.text.x = element_text(face="plain", size=10.5, angle=90), 
    axis.text.y = element_text(face="plain", size=10.5, angle=0), 
    legend.position="bottom", legend.box = "horizontal",
    panel.background = element_rect(fill='white', colour='black'), 
    panel.grid.major.y = element_line(colour = "grey")
  )
Eileenleen
  • 21
  • 2

1 Answers1

0

The easiest way is to bind both the data frame into one and plot calling Param for color. By this, you can avoid running the same code twice for two data frames.

If you don't want to bind, you can call Param for color in all the aes(), and adding the scale_color_manual.

test1 <- ggplot()+

  geom_point(test_lead, mapping = aes(x=CollectDate, y=ReportedResult, color = Param), show.legend = TRUE) +

  geom_line(test_lead, mapping = aes(x=CollectDate, y=ReportedResult, color = Param), show.legend = TRUE) +

  geom_point(test_ph, mapping = aes(x=CollectDate, y=ReportedResult*27.2/1.920, color = Param), show.legend = TRUE) +

  geom_line(test_ph, mapping = aes(x=CollectDate, y=ReportedResult*27.2/1.920, color = Param), show.legend = TRUE) +
  scale_color_manual(values=c("blue", "hotpink")) +
  scale_x_date(name = "", date_breaks = "2 week", date_labels = "%Y/%m/%d") +
  theme(
    plot.title = element_text(size = 16, 
                              face = "bold",
                              family = "sans",
                              color = "black",
                              hjust = 0.5,
                              lineheight = 1.2 ), 
    axis.text.x = element_text(face="plain", size=10.5, angle=90), 
    axis.text.y = element_text(face="plain", size=10.5, angle=0), 
    legend.position="bottom", legend.box = "horizontal",
    panel.background = element_rect(fill='white', colour='black'), 
    panel.grid.major.y = element_line(colour = "grey")
  )

enter image description here

Mohanasundaram
  • 2,889
  • 1
  • 8
  • 18
  • Thank you!!! I thought about binding them, since there are like 10 parameters and about 10 sites, my current code is very inefficient. But I also ran into issue when plotting them using the bind df so I decided to stick with this. – Eileenleen May 20 '20 at 14:22