2017-08-28 60 views
0

我试着做一个阴谋如下。如何在保存级别设置2个不同的y轴?

但是我的2个y轴不在不同的水平。此外,我无法在“红色”中设置左侧的Y轴“WD”

有没有人能帮我弄清楚这两个问题?

非常感谢!

DLPM10$hours <- as.integer(gsub('.* |:.*', '', DLPM10$date)) 

dt <- group_by(DLPM10, hours) %>% 

summarise(WD = mean(WD_DOUGLAS, na.rm = TRUE), WS = mean(WS_DOUGLAS, na.rm = TRUE)) 

par(mar=c(5,4,4,5)+.1) 

barplot(height = dt$WD, axes=FALSE, border =NA,space = 0.4,ylim=c(0,370), 
    col="red",xlab = 'Hour', ylab = 'WD', main = "Dougals Red Cross") 

axis(2, ylim=c(0,370),col="red",col.axis="red",col.lab="red",las=1) 

par(new = TRUE) 

plot(dt$hours, dt$WS,axes = FALSE, bty = 'n',pch= 19,xlim = c(-.35, 23.35), 
ylim=c(0,5), col="blue", xlab = '', ylab = '') 

axis(4, 0:5, col="blue", col.axis="blue",las=1) 

mtext('WS', dt$WS,col="blue", side = 4, line = 2) 

axis(1, 0:23, pos = -0.2) 

legend("topleft",legend=c("WS","WD"),bty = "n", 
    bg = par("bg"), 
    text.col=c("blue","red"),pch=c(19,15), 
    col=c("blue","red"),horiz= T,text.width= 0.01) 

enter image description here

回答

0

喜欢这个?

par(mar=c(5,4,4,4)) 
plot(1, axes=FALSE, ylab='', ylim=c(0,370), xlim = c(-.35, 23.35), col=0) 
mtext("WD", side=2, line=3, col="red", cex=1) 
mtext("WS", side=4, line=3, col="blue", cex=1) 
axis(1, 0:23, pos = -0.2) 
axis(2, ylim=c(0,370),col="red",col.axis="red",col.lab="red") 
axis(4, at=c(0:5)*370/6,labels=0:5, col="blue", col.axis="blue",las=1) 
+0

谢谢你的帮助。对不起,它不适合我。我发现如果我改变barplot(height = dt $ WD,axes = FALSE,border = NA,space = 0.4,ylim = c(-10,370), col =“red”,xlab ='Hour',ylab ='WD ',main =“道格拉斯红十字会”)两轴现在都很近,但是。如果在同一级别,我不是100%。 –