Home > Enterprise >  Can I use ggplot when I have created a time series data using the ts function
Can I use ggplot when I have created a time series data using the ts function

Time:11-24

I have data that shows daily Covid cases and I used the ts function to convert the data into a time series. The issue is when I plot the data, the x-axis gives me this:

2021.1  2021.2  2021.3 and so on....

Since I want to look at 2021 respectively, I want the x-axis to show me the month names on the x-axis. How do I go about this? I am sharing the code below:

options(scipen=5)
myts = ts(brazil$new_cases,frequency = 365, start = c(2020,02,26))
myts = na_mean(myts, option = "mean")
plot(myts)
myts2 = window(myts, start = c(2021,1)) 
plot(myts2)

The data is as follows:

structure(c(66588, 65998, 65169, 61602, 34027, 35742, 59925, 
71704, 75102, 75495, 69609, 80508, 32321, 70764, 79876, 75412, 
85663, 76178, 43812, 36239, 83926, 90303, 86982, 90570, 79069, 
47774, 49293, 82493, 89992, 100158, 84245, 85948, 44326, 38927, 
84494, 90638, 91097, 70238, 43515, 31359, 28645, 86979, 92625, 
86652, 93317, 71832, 37017, 35785, 82186, 73513, 73174, 85774, 
67636, 42980, 30624, 69381, 79719, 45178, 69105, 71137, 32572, 
28636, 72140, 79726, 69389, 68333, 66964, 28935, 24619, 77359, 
73295, 73380, 78886, 63430, 38911, 25200, 72715, 76692, 74592, 
85536, 67009, 40709, 30148, 75445, 79219, 82039, 76855, 76490, 
35819, 37498, 73453, 80486, 67467, 49768, 79670, 43520, 30434, 
78926, 95601, 83391, 37936, 66017, 39637, 37156, 52911, 85748, 
88092, 85149, 78700, 37948, 39846, 80609, 95367, 74042, 98832, 
82288, 44178, 38903, 87822, 115228, 73602, 79277, 64134, 33704, 
27804, 64903, 43836, 65163, 65165, 54556, 27783, 22703, 62504, 
54022, 53725, 57737, 48504, 20937, 17031, 45022, 57736, 52789, 
45591, 34339, 34126, 15271, 27592, 54517, 49757, 108732, 38091, 
18129, 18999, 41411, 48013, 42283, 40904, 37582, 20503, 15143, 
32316, 40716, 40054, 42159, 43033, 13893, 12085, 34885, 32443, 
39982, 33933, 31142, 13957, 14471, 37613, 41714, 36315, 33887, 
28388, 14404, 13103, 30872, 30671, 31024, 27345, 24699, 13210, 
10466, 24589, 27345, 26280, 25565, 21804, 12915, 9154, 14304, 
13771, 30891, 15951, 14314, 10615, 6645, 13406, 14780, 34407, 
11202, 34962.8922829582, 9458, 7884, -573, 36473, 24611, 19438, 
15688, 8668, 14423, 15395, 17756, 27527, 18578, 13466, 9004, 
10425, 20528, 17893, 15591, 18172, 16451, 8639, 6918, 7359, 7852, 
14288, 15239, 11250, 5738, 7446, 12969, 15609, 16853, 14502, 
11716, 6204, 5797, 13424, 17184, 15268, 11965, 10693, 6761, 3838, 
6431, 14661, 13352, 13321, 11866, 6115, 5638, 10948), .Tsp = c(2021, 
2021.70684931507, 365), class = "ts")

CodePudding user response:

It seems you are using package "forecast" which has issues with dates. I suggest you switch to package "fable" which is more advanced and easy to use. Here are some codes to do what you want. I don't think ggplot supports tsibbles, you can try to convert the data to tibble with as_tibble().

brazil 2020-2021

  library(tidyverse)
    library(tsibble)
    library(lubridate)
    library(fable)
    library(tidyquant)
    library(knitr)
    
    corona_bra <-readr::read_csv("00_data/brazil.csv") %>% 
     as_tsibble()%>%
     
     corona_bra
    mean <- corona_bra %>%
     model(
      
      mean= MEAN(new_cases) 
     )
    
    
    fcats<- mean %>%
     
     forecast(h = 14)
    
    hilo(fcats, level = 80, round (hilo, digits = 0))
    
    fcats %>%
     
     autoplot(corona_bra, level = 80)  
     ggtitle("Brazil (Mean), Nov-23, 2021")  
     xlab("date")   ylab("new_cases") 
     theme_tq()
    
    
    corona_bra_21 <- filter(corona_bra,date >="2021-01-01" ) 
    
    mean_21 <- corona_bra_21 %>%
     model(
      
      mean= MEAN(new_cases) 
     )
    
    
    fcats<- mean_21 %>%
     
     forecast(h = 14)
    
    hilo(fcats, level = 80, round (hilo, digits = 0))
    
    fcats %>%
     
   [brazil 2021][2]
  autoplot(corona_bra_21, level = 80)  
     ggtitle("Brazil 2021 (Mean), Nov-23, 2021")  
     xlab("date")   ylab("new_cases") 
     theme_tq()

CodePudding user response:

Here is the code to convert the tsibble into a tibble then use ggplot2 to produce your graphic

enter code here 

library (ggplot2)
bra_21_tbl<- corona_bra_21 %>% as_tibble
bra_21_tbl

ggplot(data=bra_21_tbl, aes(x=date, y=new_cases))  
 geom_line() 
 geom_point() 
 geom_hline(yintercept=43675,linetype='dotted', col = 'red') 
 labs( title = "Covid19 New Cases in Brazil 2021 vs Mean ")

CodePudding user response:

2021 plot with a horizontal line for the Mean Here is the ggplot2 graphic. You can improve on it by adding more tick marks for the missing months and use:

theme(     axis.text.x = element_text(angle = 45, hjust = 0)    )

CodePudding user response:

ggplot hline textHere is some suggested improvements to the ggplot:

    p<- ggplot(data=bra_21_tbl, aes(x=date, y=new_cases))  
 geom_line() 
 geom_point() 
 geom_hline(yintercept=43675,linetype='dotted', col = 'red',size = 2) 
 labs( title = "Covid19 New Cases in Brazil 2021 vs Mean ") 
p
 
 library(grid)
 # Add text as a grob
 grob <- grobTree(textGrob("Mean = 43675", x=0.75,  y=0.75, hjust=0,
                           gp=gpar(col="red", fontsize=13, fontface="italic")))
 # Add to ggplot
 p   annotation_custom(grob)
 
 
  • Related