Multi-line ggplot for long data sets with legend

I struggled to find links on doing multi-line plots with long time-series data. From various threads created something. Maybe this helps you...

LP2 <- ggplot() +
        xlab("month, day") +
        ylab("Y (H [m], correllation)") +
        ggtitle("Example data showing tidal removal") +
        geom_line(data = WL[at:bt,], aes(dmy, WL, color = "WL"), size = 0.7) + 
        geom_line(data = WL[at:bt,], aes(dmy, Cwl, color = "WL.de-tide"), size = 0.9) + 
        geom_line(data = WL[at:bt,], aes(dmy, Tspk, color = "pulses"), size = 0.7) + 
        geom_line(data = WL[at:bt,], aes(dmy, LMr, color = "Rsquare"), size = 0.7) + 
        geom_line(data = WL[at:bt,], aes(dmy, TLs, color = "Tide(m)"), size = 0.7) + 
        scale_color_manual(name = "Key:-", 
                           values = c("WL" = "blue", "WL.de-tide" = "red", "pulses" = "darkorchid",
                                      "Rsquare" = "dodgerblue", "Tide(m)" = "orange")) +
        theme(legend.position="bottom",
              legend.text = element_text(size=9),
              axis.text = element_text(size=9),
              axis.title = element_text(size=9),
              plot.title=element_text(size = 9),
              legend.title=element_text(size=9))
        
        print(LP2)

It is nice of you trying to share what you have learned and it is not my intention to criticize you but the way you have achived the plot is not considered the best approach. It would be better to reshape your data into a long format using tidyr before plotting. If you are interest in this approach, please post a proper REPRoducible EXample (reprex) including sample data and we can go from there.

Hi Andres, thanks, yes, I knew that reshaping the df was an option. I was curious to find a work-around to avoid it. My datasets are 500,000 or more records, and I´m plotting subsets, to demonstrate/check/validate certain operations. I could subset the data for each plot block and then reshape long prior to the plotting stage. I´m very much learning, as I go, so feedback very welcome. I´ll check-out how to post a sample of the data from the link you´ve provided... :grin:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.