0
votes

I have a ggplot with data from three data frame within a list. Each data frame has a "date" and a "measurement" vector. These are hourly data from instrumentation. I have a separate list with three data frames that have manual data points. These data are collected once a month. The naming of the data frames is consistent within each list, but .

What I want to do, is to plot the manual data as points overtop of the hourly data.

I've managed to get pretty close to what I want with this code:

p1  <- ggplot(data_list[["Inst1"]],aes(date,elevation))+geom_line(aes(color="Instrument 1"))+
              geom_line(data=data_list[["Inst2"]],aes(color="Instrument 2"))+
              geom_line(data=data_list[["Inst3"]],aes(color="Instrument 3"))+

              geom_point(data=man_list[["Inst1"]],aes(date,water_el,color="Instrument 1",size = "3"))+
              geom_point(data=man_list[["Inst2"]],aes(date,water_el,color="Instrument 2",size = "3"))+
              geom_point(data=man_list[["Inst3"]],aes(date,water_el,color="Instrument 3",size = "3"))+

              labs(color="")

Here is the plot.

There are two main issues I don't know how to resolve.

1) I'd prefer to have the Legend show the manual points labelled separately from the hourly data (lines). I realize that using colour to generate the legend is the issue here, but I'm not sure what other approach I can take. I want the colours of the points and lines to be the same, however.

2) I'm not sure why size is showing up in the legend.

Help?

UPDATE:

 str(data_list)
 List of 5
 $ Inst1 :'data.frame':  7735 obs. of  4 variables:
 $ date: POSIXct[1:7735], format: "2016-01-22 01:56:00" "2016-01-22 02:56:00" ...
 $ measurement: num [1:7735] 1.73 1.73 1.73 1.73 1.74 ...
 $ trans_el   : num [1:7735] 341 341 341 341 341 ...
 $ elevation  : num [1:7735] 343 343 343 343 343 ...

 str(man_list)
 List of 5
 $ Inst1 :'data.frame':  14 obs. of  6 variables:
 $date          : POSIXct[1:14], format: "2015-12-07 15:16:00" "2015-12-23 15:51:00" ...
$ man           : num [1:14] 3.69 4.33 4.28 4.07 3.71 ...
$ trans_h2o     : num [1:14] NA NA NA 1.72 2.08 ...
$ trans_el      : num [1:14] NA NA NA 341 341 ...
$ water_el      : num [1:14] 343 342 342 343 343 ...
$ trans_el_round: num [1:14] NA NA NA 341 341 ...

Each list has 5 data frames of Inst1 to Inst 5. Within a given list, each data frame is set up the same (has same variables and names). BUT the date vectors are different for each data frame, and are different between each list.

So,

data_list[["Inst1"]]$date =/ data_list[["Inst2"]]$date

and

data_list[["Inst1"]]$date =/ man_list[["Inst1"]]$date

1
1 ) maybe use shapes 21:24 and use fill instead of color for geom_point() 2) move size = "3" outside of aes - Nate
This plot would be much easier to produce if you combine your data frames. Then you can map color to a column in your data frame and you would also need only one call each to geom_point and geom_line. If possible, please paste into your question the output of dput(lapply(data_list, head)) and the output of dput(lapply(man_list, head)). - eipi10
I do not understand what you mean with the legends to show manual points... But as @NathanDay said, you should move size=3 outside the aes. - TKN
Would be helpful to give a reproducible example. - timat
taking size=3 outside of the aes worked. - codenewb

1 Answers

0
votes

This is an example of how you could implement my comment, using data(mtcars)

library(ggplot2)
mtcars$cyl <- as.factor(mtcars$cyl)

ggplot(data = mtcars, aes(disp, hp)) +
    geom_point(aes(fill = cyl, shape = cyl), size = 4, stroke = 0) + # note size not in aes(); using stroke = 0 to eliminate edges, which would be part of the color scale
    stat_smooth(aes(group = cyl, color = cyl), method = "lm", se = F) +
    scale_color_brewer(palette = "Set1") +
    scale_fill_brewer(palette = "Dark2") +
    scale_shape_manual(values = 21:24) + # critical because only these shapes have a fill in addition to color, all the others are only just color
    guides(color = guide_legend(order = 1),
       fill = guide_legend(order = 2),
       shape = guide_legend(order = 2))

enter image description here

Happy to help work through a specific example with you data, if you can't get this example to transfer. Cheers.