0

I am trying to make a scatterplot with salinity on the y-axis and Date/Time on x-axis. However, when I try to plot it my x-axis data is getting cut off before the end of my data. I've tried adding an xlim but I get an error. See below

p_OY1 <- ggplot(data = OY1, aes(x=date_time2, y=Salinity)) + geom_point() + ylim=c(0,12) + xlim=c("0019-10-04 00:00:00", "0020-06-10 00:00:00")+ theme_classic()+
  scale_x_discrete("Date", breaks=c("0019-10-04 10:30:00", "0019-11-01 00:00:00", "0019-12-01 00:00:00", "0020-01-01 00:00:00", "0020-02-01 00:00:00",
                                    "0020-03-01 00:00:00","0020-04-01 00:00:00", "0020-05-01 00:00:00", "0020-06-01 00:00:00"),
                   labels=c("10/4/2019", "11/1/2019", "12/1/2019", "1/1/2020", "2/1/2020", "3/1/2020", "4/1/2020", "5/1/2020", "6/1/2020"))+ylab("Salinity")+ggtitle("OY1")

Error:

Error in c("0019-10-04 00:00:00", "0020-06-10 00:00:00") + scale_x_discrete("Date", : non-numeric argument to binary operator

OY1 Data

> dput(head(OY1))
structure(list(Site = c("OY1", "OY1", "OY1", "OY1", "OY1", "OY1"
), Date = c("10/4/19", "10/4/19", "10/4/19", "10/4/19", "10/4/19", 
"10/4/19"), Time = structure(c(37800, 39600, 41400, 43200, 45000, 
46800), class = c("hms", "difftime"), units = "secs"), Salinity = c(0.891307674, 
0.960962348, 1.015788098, 1.096679068, 1.191564305, 1.272517514
), Date_time = c("10/4/19 10:30:00", "10/4/19 11:00:00", "10/4/19 11:30:00", 
"10/4/19 12:00:00", "10/4/19 12:30:00", "10/4/19 13:00:00")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"), problems = structure(list(
    row = 4550L, col = "Salinity", expected = "a double", actual = "#VALUE!", 
    file = "'~/Documents/TAMUG_Thesis/Rollover_Pass_Data/Logger/RP_LoggerData_OY1.csv'"), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame")))

Can anyone help? I've been messing with this for far too long. Thanks!

  • Try with `xlim("0019-10-04 00:00:00", "0020-06-10 00:00:00")` and `ylim(0,12)` without `=c` – stefan Jul 27 '21 at 22:03
  • It would help to see some or all of the data in `OY1` in plain text format. You can paste the output of `dput(OY1)`, if it's not too large, or `dput(head(OY1))` if it is. – neilfws Jul 27 '21 at 22:59
  • 1
    `scale_x_discrete()` with time data won't work. You need to (a) make sure your datetime data is formatted correctly (the reason for @neilfws' request), and then (b) use `scale_x_datetime()`. Check out [this page](https://ggplot2.tidyverse.org/reference/scale_date.html) for help with breaks, labels, etc. – phalteman Jul 27 '21 at 23:07
  • @neilfws and @phlateman I have added a view of some of my data, unfortunately it is a very large dataset so I can't post all of it. I just tried `scale_x_datetime` but am still running into errors. – Ashley McDonald Jul 28 '21 at 14:43

1 Answers1

0

Your example data is helpful because it shows that the Date_time column has class character. That is why scale_x_datetime isn't working as expected.

You can convert it to datetime format in several ways. One is to use the lubridate package. Your dates look as though they are day/month/year, so you can convert date + time using the function dmy_hms.

See if this gets you started. You'll need to experiment with date_breaks to get what you want.

library(lubridate)
library(dplyr)
library(ggplot2)

OY1 %>% 
  mutate(dt = lubridate::dmy_hms(Date_time)) %>% 
  ggplot(aes(dt, Salinity)) + 
  geom_point() + 
  scale_x_datetime(date_labels = "%d/%m/%Y")

Result using your example data:

enter image description here

neilfws
  • 32,751
  • 5
  • 50
  • 63