ggplot and geom_line trouble

Hello,
I am new to R, and by new I mean I've only started using it last Thursday..
I am trying to create a line graph showing data for separate housing units but I cant seem to figure it out. I used the following to try to plot my data

d.wastewaternew$sampledate = mdy(d.wastewaternew$ï..DateResult)
d.wastewaternew %>%
  ggplot(aes(x = ï..DateResult, y= SARS2_levels, color = Hall)) + geom_line()

and the result I got was the first image, which shows now data plots and has the dates all jumbled up

On the second try, I changed ï..DateResult in the sting to sampledate, which was the new mdy variable I had created for this data set and it gave me the following. Now instead of plotting by day its just plotting months, which is not what I need for the graph

any tips would be greatly appreciated

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

> datapasta::df_paste(head(d.wastewaternew))
named list()
> reprex::reprex({
+ library("ggplot2")
+ library(lubridate)
+ library(magrittr)
+ library(dplyr)
+ d.wastewaternew$sampledate = mdy(d.wastewaternew$ï..DateResult)
+ d.wastewaternew %>%
+   ggplot(aes(x = ï..DateResult, y= SARS2_levels, color = Hall)) + geom_line()
+ datapasta::df_paste(head(d.wastewaternew))
+ data.frame(
+   stringsAsFactors = FALSE,
+      ï..DateResult = c("9/1/2020","9/4/2020",
+                        "9/10/2020","9/15/2020","9/17/2020","9/22/2020"),
+               Hall = c("Booth Hall","Booth Hall",
+                        "Booth Hall","Booth Hall","Booth Hall","Booth Hall"),
+       SARS2_levels = c(0, 0, 0, 0, 0, 0),
+         sampledate = c("2020-09-01","2020-09-04",
+                        "2020-09-10","2020-09-15","2020-09-17","2020-09-22")
+ )})

Not quite a reprex but good enough I think.

To plot dates in the x-axis you need to use a proper date variable, not character strings, and to customize the "date breaks" of your plot you can use the scale_x_date() function. Check this example:

library(tidyverse)

d.wastewaternew <- data.frame(
    stringsAsFactors = FALSE,
    ï..DateResult = c("9/1/2020","9/4/2020",
                      "9/10/2020","9/15/2020","9/17/2020","9/22/2020"),
    Hall = c("Booth Hall","Booth Hall",
             "Booth Hall","Booth Hall","Booth Hall","Booth Hall"),
    SARS2_levels = c(0, 0, 0, 0, 0, 0),
    sampledate = as.Date(c("2020-09-01","2020-09-04",
                   "2020-09-10","2020-09-15","2020-09-17","2020-09-22"))
)

d.wastewaternew %>%
    ggplot(aes(x = sampledate, y = SARS2_levels, color = Hall)) + 
    geom_line() +
    scale_x_date(date_breaks = "1 day",
                 labels = scales::label_date_short())

Created on 2021-04-18 by the reprex package (v2.0.0)

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.