Reputation: 3
I'm having trouble dealing with dates in my R code.
I have a data frame consisting of local temperatures and humidity measurements with the date (dd/mm/YY format) and a timestamp (hh:mm:ss, in 24h format).
The data frame looks like this:
day hour hum temp
1 19/3/2020 13:39:34 53.0 31.8
2 19/3/2020 13:54:34 53.1 31.7
3 19/3/2020 14:9:33 53.5 30.9
4 19/3/2020 14:24:33 54.1 31.2
5 19/3/2020 14:39:33 53.8 31.3
6 19/3/2020 14:54:33 53.5 31.4
I want to plot daily temperature and humidity fluctuations, however when I use the following code:
df %>
filter(date == '19/03/2020' &
!is.na(temp)) %>% # I have to filter NAs to remove failed measurements (empty cells)
ggplot(aes(day, temp, color = temp)) +
geom_line()
It generates an empty plot with just the axis names (y = temp and x = day)
The only way I'm able to get a plot is when I use
df %>%
ggplot(aes(day, temp, color = temp) +
geom_poin(alpha = 0.2) +
geom_jitter() +
theme(axis.text.x = element_text(angle = 90)
Example plot: note the dates on the x-axis
However, the dates in the x-axis are sorted by the first digit of the date (see picture). This data frame consists of over 8000 measurements spanning from March to June (beginning in 19/03/2020) so the dates are plotted out of order, like this:
1 - 01/04/2020
2 - 01/05/2020
3 - 01/06/2020
4 - 02/04/2020
5 - ...
My final goal is to plot temp and hum with timestamps and facet by day so I can get different plots for different days. Something like this:
df %>%
filter(!is.na(temp)) %>%
ggplot(aes(hour, temp, color = temp) +
geom_line() +
facet_grid(.~day)
So, my questions are:
str()
it returns that df$day
and df$hour
are 'character' classes. Shouldn't they be considered something else? Maybe that is why my code doesn't work...Upvotes: 0
Views: 51
Reputation: 12461
It looks as if your dates are character, so '19/3/2020'
is not equal to '19/03/2020'
so your filter returns an empty data frame. Convert them to dates with as.Date()
. For more sophisticated parsing, look at the lubridate
package.
Upvotes: 1