Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

r - Why is my plot on the right hand axis overwriting my existing plot?

I am running code which I ran successfully on my work laptop. But on my home laptop when I get to the second 'plot' command it seems to start a new plot and overwrites what was there before. ???? What has changed and how can I prevent this?

cases<-read.csv("exporttablenewhist.csv")
str(cases)
date<-cases[,1]
modhistcase<-cases[,2]
realhistcase<-cases[,3]
lockdownhist<-cases[,4]
modhistdeaths<-cases[,5]
realhistdeaths<-cases[,6]


datereal<-as.Date(date,"%d/%m/%Y")

# Create new output window
dev.new(width=12, height=7.5) 
# We suggest a 6" by 3.75" window to facilitate importing into word document (default values ~golden ratio)
#
# Adjust the margin of plot (bottom,left,top,right)
par(mar=c(5, 5, 3, 7))
#
plot(datereal, realhistcase, type = "l", lwd=2, col = "green", xaxt = "n", xlab="Month",ylab="Daily new cases")
which(substr(datereal, 9, 10) == "01")
labs <- datereal[substr(datereal, 9, 10) == "01"]
ids <- seq_along(labs)[(seq_along(labs) + 0) %% 2 == 0]
axis.Date(1, at=labs[ids], format="%b %y", cex.axis=.9)


lines(datereal,modhistcase, lwd=2,col = "blue")
legend(datereal[1],50000, legend=c("reported cases",
                                   "modelled known cases",
                                   "lockdown %"),
       col=c("green","blue","red"),lty=1)

plot(lockdownhist,type="l",lwd=2,lty=1,col = "red",xaxt = "n", yaxt = "n", xlab= "", ylab ` 
="",ylim=c(0,100))
axis(side=4)
mtext("lockdown percentage", side=4, line=3)
axis(4,seq(0,100,by=10))

IMAGES BEFORE AND AFTER SECOND PLOT COMMAND

before after

question from:https://stackoverflow.com/questions/65650876/why-is-my-plot-on-the-right-hand-axis-overwriting-my-existing-plot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...