Reputation: 63
I have trouble in changing the month from numeric to name in the x-axis (refer to picture). Below are my code. I want to change the month (1,2,3,4,..) to (Jan,Feb,Mar,...) in the x-axis.
dd <- data.frame(beginning=c(6,6,6,7,7,8),
end=c(7,7,7,7,7,12),
solution=c("death", "death","death","death","death","recovered")
)
dd$id <- c(1:length(dd[,1]))
par(mfcol=c(1,1), mar=c(5,5,1.6,1),cex.lab=1)
plot(id~beginning, data=dd,
xlim=c(1,12), ylim=c(0, max(dd$id)+2),# axis limits
pch=NA, # dont plot points yet
yaxt="n", xaxt="n",
xlab="Month", ylab="Case No.")
axis(side=2, at=dd$id, labels=dd$id, las=1)
axis(side=1,
at=seq(1,12, by=1),
labels=seq(1,12, by=1))
with(dd, arrows(y0=id, x0=beginning, y1=id, x1=end, length = 0))
with(dd, points(beginning, id, pch=25, bg="red", cex=1.5))
dd$my.pch <- ifelse(dd$solution=="recovered",24,4)
with(dd, points(end, id, pch= my.pch, bg="green", cex=1.5))
legend("topleft", legend=c("ill", "recovered", "death"),
pch=c(25,24,4),
pt.bg= c("red", "green", "black"),
bty="n"
)
Upvotes: 0
Views: 157
Reputation: 6483
You need only a super small change to your code.
Replace labels=seq(1,12, by=1)
with labels=month.name
for full names or labels=month.abb
for the abbreviated names:
dd <- data.frame(beginning=c(6,6,6,7,7,8),
end=c(7,7,7,7,7,12),
solution=c("death", "death","death","death","death","recovered")
)
dd$id <- c(1:length(dd[,1]))
par(mfcol=c(1,1), mar=c(5,5,1.6,1),cex.lab=1)
plot(id~beginning, data=dd,
xlim=c(1,12), ylim=c(0, max(dd$id)+2),# axis limits
pch=NA, # dont plot points yet
yaxt="n", xaxt="n",
xlab="Month", ylab="Case No.")
axis(side=2, at=dd$id, labels=dd$id, las=1)
axis(side=1,
at=seq(1,12, by=1),
labels=month.abb)
with(dd, arrows(y0=id, x0=beginning, y1=id, x1=end, length = 0))
with(dd, points(beginning, id, pch=25, bg="red", cex=1.5))
dd$my.pch <- ifelse(dd$solution=="recovered",24,4)
with(dd, points(end, id, pch= my.pch, bg="green", cex=1.5))
legend("topleft", legend=c("ill", "recovered", "death"),
pch=c(25,24,4),
pt.bg= c("red", "green", "black"),
bty="n"
)
Gives:
Upvotes: 2