我有顯示每日 Covid 病例的資料,我使用 ts 函式將資料轉換為時間序列。問題是當我繪制資料時,x 軸給了我這個:
2021.1 2021.2 2021.3 and so on....
由于我想分別看2021年,所以我希望x軸顯示x軸上的月份名稱。我該怎么做?我正在分享以下代碼:
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)
資料如下:
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")
uj5u.com熱心網友回復:
看來您正在使用有日期問題的包“forecast”。我建議您改用更高級且易于使用的包“fable”。這里有一些代碼可以做你想做的事。我認為 ggplot 不支持 tsibbles,您可以嘗試使用 as_tibble() 將資料轉換為 tibble。
巴西 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()
uj5u.com熱心網友回復:
這是將 tsibble 轉換為 tibble 然后使用 ggplot2 生成圖形的代碼
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 ")
uj5u.com熱心網友回復:
2021 年的均值圖,帶有一條水平線這是 ggplot2 圖形。您可以通過為缺少的月份添加更多刻度線來改進它并使用:
theme( axis.text.x = element_text(angle = 45, hjust = 0) )
uj5u.com熱心網友回復:
ggplot hline text以下是對 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364219.html
