[英]Plotting time-series with Date labels on x-axis
我知道這個問題可能是陳詞濫調,但我很難做到。
我有以下格式的數據集:
Date Visits 11/1/2010 696537 11/2/2010 718748 11/3/2010 799355 11/4/2010 805800 11/5/2010 701262 11/6/2010 531579 11/7/2010 690068 11/8/2010 756947 11/9/2010 718757 11/10/2010 701768 11/11/2010 820113 11/12/2010 645259
我想創建一個時間序列圖,x 軸代表時間和 y 軸視圖。 另外,我想用日期標記 x 軸。 我使用的代碼如下:
dm$newday = as.POSIXct(strptime(dm$Day, format="%Y-%m-%d"))
plot(as.Date(dm$day),dm$visits)
axis.Date(1,Day,at=seq(as.Date("2010/10/30"), as.Date("2011/01/29"),by="days"))
1)由於時間是日期,請務必使用"Date"
類,而不是"POSIXct"
或"POSIXlt"
。 請參閱 R 新聞 4/1 以獲取建議,並在末尾的注釋中定義Lines
地方嘗試此操作。 這里沒有使用任何包。
dm <- read.table(text = Lines, header = TRUE)
dm$Date <- as.Date(dm$Date, "%m/%d/%Y")
plot(Visits ~ Date, dm, xaxt = "n", type = "l")
axis(1, dm$Date, format(dm$Date, "%b %d"), cex.axis = .7)
使用text = Lines
只是為了保持示例自包含,實際上它會被替換為類似"myfile.dat"
。 (在圖像后繼續)
2)由於這是一個時間序列,您可能希望使用時間序列表示,給出稍微簡單的代碼:
library(zoo)
z <- read.zoo(text = Lines, header = TRUE, format = "%m/%d/%Y")
plot(z, xaxt = "n")
axis(1, dm$Date, format(dm$Date, "%b %d"), cex.axis = .7)
根據您希望繪圖的外觀plot(Visits ~ Date, dm)
在第一種情況下使用plot(Visits ~ Date, dm)
或在第二種情況下使用plot(z)
完全抑制axis
命令可能就足夠了。 也可以使用 xyplot.zoo
library(lattice)
xyplot(z)
或 autoplot.zoo:
library(ggplot2)
autoplot(z)
注意:
Lines <- "Date Visits
11/1/2010 696537
11/2/2010 718748
11/3/2010 799355
11/4/2010 805800
11/5/2010 701262
11/6/2010 531579
11/7/2010 690068
11/8/2010 756947
11/9/2010 718757
11/10/2010 701768
11/11/2010 820113
11/12/2010 645259"
我喜歡將ggplot2
用於此類事情:
df$Date <- as.Date( df$Date, '%m/%d/%Y')
require(ggplot2)
ggplot( data = df, aes( Date, Visits )) + geom_line()
你的代碼有很多錯誤。
dm$Day
和dm$day
。 可能不是一回事Date
和Visits
。 所以你會訪問它們(我猜)作為dm$Date
和dm$Visits
%Y-%m-%d
這應該是%m/%d/%Y
以下代碼應該繪制您想要的內容:
dm$newday = as.Date(dm$Date, "%m/%d/%Y")
plot(dm$newday, dm$Visits)
您可以通過使用 text() 修改軸符號來旋轉日期
Lines <- "Date Visits
11/1/2010 696537
11/2/2010 718748
11/3/2010 799355
11/4/2010 805800
11/5/2010 701262
11/6/2010 531579
11/7/2010 690068
11/8/2010 756947
11/9/2010 718757
11/10/2010 701768
11/11/2010 820113
11/12/2010 645259"
dm <- read.table(textConnection(Lines), header = TRUE)
dm$Date <- as.Date(dm$Date, "%m/%d/%Y")
plot(Visits ~ Date, dm, xaxt = "n", type = "l")
axis(1,at=NULL, labels=F)
text(x = dm$Date, par("usr")[3]*.97, labels = paste(dm$Date,' '), srt = 45, pos = 1, xpd = TRUE,cex=.7)
在 ggplot 中是可能的,你可以使用 scale_date 來完成這個任務
library(ggplot2)
Lines <- "Date Visits
11/1/2010 696537
11/2/2010 718748
11/3/2010 799355
11/4/2010 805800
11/5/2010 701262
11/6/2010 531579
11/7/2010 690068
11/8/2010 756947
11/9/2010 718757
11/10/2010 701768
11/11/2010 820113
11/12/2010 645259"
dm <- read.table(textConnection(Lines), header = TRUE)
dm <- mutate(dm, Date = as.Date(dm$Date, "%m/%d/%Y"))
ggplot(data = dm, aes(Date, Visits)) +
geom_line() +
scale_x_date(format = "%b %d", major = "1 day")
我也喜歡ggplot
。
下面是一個例子:
df1 = data.frame(
date_id = c('2017-08-01', '2017-08-02', '2017-08-03', '2017-08-04'),
nation = c('China', 'USA', 'China', 'USA'),
value = c(4.0, 5.0, 6.0, 5.5))
ggplot(df1, aes(date_id, value, group=nation, colour=nation))+geom_line()+xlab(label='dates')+ylab(label='value')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.