R 如何转换为时间序列并以每天为变量或列绘制数据帧?

R 如何转换为时间序列并以每天为变量或列绘制数据帧?,r,plot,time-series,R,Plot,Time Series,我试图得到意大利Cov-19的数量随时间变化的曲线图,我发现了这一点,并试图将意大利的数据子集如下: require(RCurl) require(foreign) x = getURL("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv") corona =

我试图得到意大利Cov-19的数量随时间变化的曲线图,我发现了这一点,并试图将意大利的数据子集如下:

require(RCurl)
require(foreign)
x = getURL("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv")
corona = read.csv(text = x, sep =",",header = T)
str(corona)
Italy <- corona[,corona$Country.Region=='Italy']
Italy <- corona[corona$Country.Region=='Italy',][1,5:ncol(corona)]
head(Italy)[,45:52]
xts
将其转换为时间序列让我读了几篇文章,询问如何将数据库转换为时间序列,其中每天都是可变日期中的一行,但在这个数据框中,似乎每个日期都是一个变量

我不一定需要将其格式化为时间序列,但我希望随着时间的推移绘制案例的数量


以下是一种绕过时间序列的方法:

require(RCurl)
require(foreign)
x = getURL("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv")
corona = read.csv(text = x, sep =",",header = T)
str(corona)
Italy <- corona[,corona$Country.Region=='Italy']
Italy <- corona[corona$Country.Region=='Italy',][1,5:ncol(corona)]
Italy <- as.matrix(sapply(Italy, as.numeric))
plot(Italy[,1],typ='l',xlab='', ylab='', col='red', lwd=3,
     main="Italy Cov-19 cum cases")
require(RCurl)
要求(外国)
x=getURL(“https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv")
corona=read.csv(text=x,sep=“,”,header=T)
str(科罗纳)

意大利这里有一个使用
tidyverse
的解决方案

首先,我使用
read_csv
直接读取csv文件(警告告诉您列的类,您可以将其复制到命令,因为所有数据类都猜对了):

现在我们可以将意大利的数据子集并绘制:

data_long_ital <- data_long %>% 
  filter(`Country/Region` == "Italy")

ggplot(data_long_ital, aes(x = date, y = cases, group = `Country/Region`))+
  geom_line() +
  scale_x_date(date_breaks = "1 weeks")
data\u long\u ital%
过滤器(`Country/Region`==“意大利”)
ggplot(数据长度,aes(x=日期,y=病例,组=`国家/地区')+
geom_线()+
缩放日期(日期间隔=“1周”)

我们可以转换为
xts
并绘图

library(xts)
plot(xts(unlist(Italy), order.by = as.Date(sub("X", "", names(Italy)),
        "%m.%d.%y")), , main = "xts plot")


有些值为0,因此将这些值转换为
NA
,因为当
log2
转换完成时,可能会产生
Inf

library(dplyr)
plot(xts(log(na_if(unlist(Italy), 0), 2), order.by = as.Date(sub("X", "", names(Italy)),
     "%m.%d.%y")), main = 'xts log2 plot')

推信封;-)你能做log base 2吗?这看起来非常直接和简单。有没有办法执行log base 2,并排除运行代码时生成的绘图的丑陋标题?@Mathstunned您可以指定
main
参数。updatedWhere指示log2()?@Mathstunned某些值为0,这将在获取日志时作为Inf。最后一个问题是,您希望值是多少?能否将y轴上的值更改为实际案例数?
data_long_ital <- data_long %>% 
  filter(`Country/Region` == "Italy")

ggplot(data_long_ital, aes(x = date, y = cases, group = `Country/Region`))+
  geom_line() +
  scale_x_date(date_breaks = "1 weeks")
library(xts)
plot(xts(unlist(Italy), order.by = as.Date(sub("X", "", names(Italy)),
        "%m.%d.%y")), , main = "xts plot")
library(dplyr)
plot(xts(log(na_if(unlist(Italy), 0), 2), order.by = as.Date(sub("X", "", names(Italy)),
     "%m.%d.%y")), main = 'xts log2 plot')