Home > Mobile >  I cannot create the plot with given variables
I cannot create the plot with given variables

Time:02-28

I am basic R user.However I cannot create the plot which shows the daily minimum temperature (in F) of the weather dataset, which is given in nycflights13. I need tocreate a new column called date with str_c, such that date should be given as follows "YYYY-MM-DD".I use the lubridate package but it gives error.

Someone has explanation for that?

CodePudding user response:

lubridate::make_date can solve the problem.

Below is the documentation.

make_date(year = 1970L, month = 1L, day = 1L)
library(tidyverse)
library(nycflights13)
library(lubridate)
df <- weather

df <- df %>%
  mutate(date = make_date(year, month, day)) %>%
  group_by(date) %>%
  summarize(min_temp = min(temp)*1.8   32)

> df
# A tibble: 364 x 2
   date       min_temp
   <date>        <dbl>
 1 2013-01-01     80.5
 2 2013-01-02     73.4
 3 2013-01-03     78.9
 4 2013-01-04     84.1
 5 2013-01-05     89.6
 6 2013-01-06     91.5
 7 2013-01-07     89.6
 8 2013-01-08     84.1
 9 2013-01-09     93.2
10 2013-01-10    102. 
# ... with 354 more rows


ggplot(df)   geom_point(aes(x = date, y = min_temp))

enter image description here

Also this might solve your code.

as.Date(str_c(weather$year, "-", weather$month, "-", weather$day), format = "%Y-%m-%d")
  • Related