rtemperature

How do I spread out temperatures in the same year for ggplot2?


I've got monthly temp data that spans over 16 years. I've formatted it like this in excel:

date temperature
2002-1-1 4.38
2002-1-2 2.88
2002-1-3 3.06
2002-1-4 5.53
2002-1-5 7.47
2002-1-6 10.62
2002-1-7 14.11
2002-1-8 15.82
2002-1-9 14.9
2002-1-10 12.81
2002-1-11 9.02
2002-1-12 5.51

This is the code in R I have so far:

dput(all_temp_data)

all_temp <- ggplot(data = all_temp_data, x = date, y = temperature)

all_temp + geom_point(aes(x = date, y = temperature)) + geom_line(aes(x = date, y = temperature))

However the dates don't descend in terms of the month and year, just by the year to give a graph that looks like:

enter image description here

How do I change it to descend in month and years?


Solution

  • Based on the sample data provided: You should (probably) convert your dates to a real date format, using as.Date().

    based on your sample data, try:

    library(tidyverse)
    all_temp_data %>%
      mutate(date = as.Date(date, format = "%Y-%d-%m")) %>%
      ggplot(aes(x = date, y = temperature)) +
      geom_point() +
      geom_line()
    

    enter image description here

    sample data used

    all_temp_data <- read.table(text = "date    temperature
    2002-1-1    4.38
    2002-1-2    2.88
    2002-1-3    3.06
    2002-1-4    5.53
    2002-1-5    7.47
    2002-1-6    10.62
    2002-1-7    14.11
    2002-1-8    15.82
    2002-1-9    14.9
    2002-1-10   12.81
    2002-1-11   9.02
    2002-1-12   5.51", header = TRUE)