1

I don't understand why this error is appearing. the normal cause is data being read as character strings. What am I missing?

I have two dataframes, individually I can plot them, but I want to plot both on the same graph. The first plots the depth against date_time, a 5 second interval dataset. The second plots a variable 'mixed layer depth' against date, a daily interval dataset.

gg <- ggplot()
gg <- gg + geom_line(data = Mn, aes(x = Date_time, y = 1-Depth, color = Temperature), size = 0)
gg # this works on it's own
gg <- gg + geom_line(data = MLDepth, aes(x = Date, y = 1-ML_Depth, color = "red"))
gg # this works on it's own. but when together with above, causes error.
gg <- gg + theme(legend.title = element_blank(), legend.position = 'none')
gg <- gg + xlab("Date") + ylab("Depth")
gg

When I try to plot them both on the same plot, I get the 'Error: Discrete value supplied to continuous scale'

Both Mn$Date_time and MLDepth$Date are posixct format e.g. "2013-10-14 12:30:00". Depth and ML_Depth are both numeric. There are no NA's.

> str(MLDepth)
'data.frame':   88 obs. of  2 variables:
 $ Date    : POSIXct, format: "2013-10-14 08:00:00" "2013-10-15 08:00:00" "2013-10-16 08:00:00" "2013-10-17 08:00:00" ...
 $ ML_Depth: num  14.1 15.2 16.3 20.9 16.6 ...
> str(Mn)
'data.frame':   1510819 obs. of  5 variables:
 $ Date_time  : POSIXct, format: "2013-10-14 12:30:00" "2013-10-14 12:30:05" "2013-10-14 12:30:10" "2013-10-14 12:30:15" ...
 $ Depth      : num  64.4 65.9 65.9 66.4 67.9 ...
 $ Temperature: num  27.5 27.5 27.4 27.4 27.2 ...
 $ Light_Level: int  148 148 148 148 147 147 146 146 146 146 ...
 $ Date       : Date, format: "2013-10-14" "2013-10-14" "2013-10-14" "2013-10-14" ...
R student
  • 109
  • 6
  • Can you please provide some sample data as an output of `dput()`? It helps us help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Christopher Belanger Sep 17 '21 at 13:27
  • 4
    The issue is probably that you map the numeric `Temperature` on `color` and in the second geom_line you map the character `"red"` on color. Maybe you want color="red" as an argument outside of aes()? – stefan Sep 17 '21 at 13:28
  • @stefan Thank you! that was precisely the issue. I moved the first ) before the ', color = "red")' and the plot works. – R student Sep 17 '21 at 13:53

1 Answers1

0

as per Stefan's comment: The issue is that I map the numeric Temperature on color and in the second geom_line you map the character "red" on color. Adding color="red" as an argument outside of aes() fixes the problem

R student
  • 109
  • 6