我想在年和月绘制散点图

时间:2018-05-10 13:02:02

标签: r time-series scatter-plot

我有seoul的nonacc(不包括事故的总死亡人数)数据及其日期数据。这是数据样本。 data1具有nonacc数据和日期数据。

head(data1$date)
[1] "2000-01-01" "2000-01-02" "2000-01-03" "2000-01-04" "2000-01-05"
[6] "2000-01-06"

tail(data1$date)
[1] "2007-12-26" "2007-12-27" "2007-12-28" "2007-12-29" "2007-12-30"
[6] "2007-12-31"

head(seoul$nonacc)
[1] 139 144 130 149 143 136

我想将date和nonacc之间的关联绘制为散点图。但是我想绘制一个按年和月划分的散点图。

所以我试过了,但他们的结果是一样的......

我试过的月份散点图。

 plot(seoul$date,seoul$nonacc, 
      xlab="Date", ylab="Nonaccidental Mortality", 
      xaxt="n")
 seq(as.Date("2000-01-01"), as.Date("2002-12-31"),"day")
 x.at <- seq(as.Date("2000-01-01"), as.Date("2007-12-31"),"month")
 xname = seq(2000, 2007, 1)
 axis(side=1, at=x.at, labels=x.at, las=1)

我试过的年份散点图。

 plot(seoul$date,seoul$nonacc, 
      xlab="Date", ylab="Nonaccidental Mortality", 
      xaxt="n")
 seq(as.Date("2000-01-01"), as.Date("2002-12-31"),"day")
 x.at <- seq(as.Date("2000-01-01"), as.Date("2007-12-31"),"year")
 xname = seq(2000, 2007, 1)
 axis(side=1, at=x.at, labels=x.at, las=1)

请帮帮我。

1 个答案:

答案 0 :(得分:1)

如果你可以使用ggplot2,你可以使用scale_x_date格式化x轴上的断点。

library(ggplot2)

load("~/Downloads/mort.rda")
seoul <- subset(mort, cname=="sl")

# plot with months as major breaks
# limit data to year 2000
seoul_2001 <- subset(seoul, date >= "2000-01-01" & date < "2001-01-01")
ggplot(seoul_2001, aes(x=date, y=nonacc)) +
  geom_point() +
  scale_x_date(date_breaks="1 month", date_labels="%b")

# plot with year as major breaks and month as minor
ggplot(seoul, aes(x=date, y=nonacc)) +
  geom_point() +
  scale_x_date(date_breaks = "1 year", date_minor_breaks = "1 month", date_labels="%Y")

第一个情节:

enter image description here

第二个情节:

enter image description here