2

*** Updated code so it is reproducible. ***

This question relates to R using RStudio.

I need to format the date on the x-axis as three-letter month and two-digit day, as shown below (Mar 01).

The date is a dataframe itself as date.local, it's of type "chr", and in this format: 2021-04-10T20:00:00+02:00

This is the code I'm using on R:

measurements_page <- fromJSON("https://api.openaq.org/v2/measurements?coordinates=41.385063,2.173404&radius=1200&limit=2000#/")
measurements_df <- as_tibble(measurements_page$results)
# Tried as.Date() but I couldn't get it to work 
#as.Date(measurements_df$date$local, format = "%m/%d/%Y" )
measurements_df %>%
  ggplot() +
  geom_line(mapping = aes(
     x = date$local, 
     y = value,
     group = 1
  )) +
  facet_wrap(~ parameter, scales = "free_y")   

It's supposed to look like this:

enter image description here

But it looks like this:

enter image description here

It appears to me like the dates are all superimposed on top of each other in the long format.

How can I fix this?

I tried this:

as.Date(measurements_df$date$local, format = "%m/%d/%Y" )

But I'm getting all NA's

Help is appreciated. Please and Thank you!

Fher
  • 35
  • 4
  • 1
    1) `date$local` and `value` are separate data sets? That's not a good idea; 2) Can you post sample data? Like this the question's problem is not reproducible; 3) see `scale_x_datetime`, argument `date_labels = "%b %d"`. – Rui Barradas Apr 11 '21 at 05:35
  • Updated code. date is a dataframe within the main dataframe. I hope I am explaining this correctly. – Fher Apr 11 '21 at 06:51
  • @Rui Barradas. Thank you Rui Barradas. I tried to use `ymd()` from lubridate on the data set but had no success. No change in `date.utc`. How do you encouter that date$lcoal and value are separate datasets? Thanks in advance. – TarJae Apr 11 '21 at 08:51
  • 1
    To se the structure of the data, use `str()`. You will see that `date` is a data.frame of variables, so `date$local` is the vector to be in plotted the x axis. (Or `date$utc`). – Rui Barradas Apr 11 '21 at 10:00

2 Answers2

2

Though there already is an accepted answer, here is another one.
I have some doubts on a line graph of these data, see below a scatter plot.

library(dplyr)
library(ggplot2)

measurements_page <- jsonlite::fromJSON("https://api.openaq.org/v2/measurements?coordinates=41.385063,2.173404&radius=1200&limit=2000#/")

measurements_page %>% 
  magrittr::extract2("results") %>%
  mutate(date_local = as.Date(date$local)) %>%
  ggplot(aes(date_local, value)) +
  #geom_line() +
  geom_point() +
  xlab("Date local") +
  facet_wrap(~ parameter, scales = "free_y")

enter image description here

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
1

I just wrapped x variable is aes around as.Date and it worked in my window

measurements_df %>%
  ggplot() +
  geom_line(mapping = aes(
    x = as.Date(date$local), 
    y = value,
    group = 1
  )) +
  facet_wrap(~ parameter, scales = "free_y")

enter image description here

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
  • 1
    Thank you! I knew it was something simple I just didn't know where to fit the as.Date(). This worked for me. – Fher Apr 11 '21 at 08:17