Reputation: 11
I have seoul's nonacc(number of total mortality excluding accident) data and their date data. This is the sample of data. data1 have nonacc data and date data.
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
I want to draw the association between date and nonacc as a scatterplot. But I want to draw a scatter plot divided by year and month.
So I tried this, but theirs results are same...
Its the month scatter plot that I tried.
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)
Its the year scatter plot that I tried.
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)
Help me please.
Upvotes: 1
Views: 865
Reputation: 742
If you are okay with using ggplot2, it you can format the breaks on the x-axis using scale_x_date
.
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")
First plot:
Second plot:
Upvotes: 1