2014-07-04 65 views
0

我在下面寫了下面的代碼。我想用線圖覆蓋條形圖。我有這個代碼,但只有一個問題。我希望線圖上的點位於條形圖的中心,即它們應該稍微向左移動。我在哪裏錯過了?如果這可以在ggplot中完成,我也會很高興。但即使基礎R會做線條圖基礎r中的條形圖重疊

par(mai = c (1 , 2, 1, 1), omi = c(0, 0, 0, 0)) 
yy <- c(31,31,31,50,50,61,69,75,80,88,94,101,108,115,121,124,125,125,125,126,127) 
name1 <- c ("15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35") 
xx <- barplot(yy, ylab = "", names.arg = name1, ylim = c(0, 140),col="steelblue") 
text(xx, yy + 3, labels = as.character(yy),srt=45) 
mtext(2,text="",line=2) 
par(new = T) 
xx2 <- c(15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35) 
yy2 <- c(379,474,579,725,922,1181,1473,1846,2316,2962,3688,4786,6069,7605,9504,10680,11074,11074,11074,11483,11484) 
plot(xx2, yy2, xlim = c(14, 36), ylim = c(0, 14000),type ="n" , axes = F, xlab ="", ylab ="",col="blue",main="") 
lines(xx2, yy2, lwd = 2,col="red",lty=1) 
points(xx2, yy2, pch = 18, cex = 1,col="red") 
text(xx2, yy2 + 4 , labels = as.character(yy2),srt=90) 
par(new = T) 
par(mai = c (1 , 1, 1, 1)) 
axis(2) 
mtext(2,text="",line=2.5) 
mtext("",side=1,col="black",line=2) 
grid() 

回答

0

的問題是,你有,由於兩個地塊的不同幅度不同的x規模。

除非你想用手找到xx2 ......另一種解決方案是考慮使用正確的y軸。

yy <- c(31,31,31,50,50,61,69,75,80,88,94,101,108,115,121,124,125,125,125,126,127) 
name1 <- c ("15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35") 
xx <- barplot(yy, ylab = "", names.arg = name1, ylim = c(0, 140),col="steelblue") 
text(xx, yy + 3, labels = as.character(yy),srt=45) 
mtext(2,text="",line=2) 
par(new = T) 
yy2 <- c(379,474,579,725,922,1181,1473,1846,2316,2962,3688,4786,6069,7605,9504,10680,11074,11074,11074,11483,11484) 
plot(xx+0.5, yy2, "l", lwd = 2,col="red",lty=1, 
    axes=F, ylim=c(0, 14000), xlim=c(min(xx), max(xx)+1)) 
points(xx+0.5, yy2, pch = 18, cex = 1,col="red") 
axis(4) 
text(xx+0.5, yy2 + 4 , labels = as.character(yy2),srt=90) 

double plot

1

它可以報價棘手的事情,如果你使用barplot和標準plot()排隊。我建議只撥打plot一次。爲了做到這一點,您需要將yy2的值重新調整爲與yy相同的比例。這裏是你會如何做,

par(mai = c (1 , 2, 1, 1), omi = c(0, 0, 0, 0)) 
yy <- c(31,31,31,50,50,61,69,75,80,88,94,101,108,115,121,124,125,125,125,126,127) 
name1 <- c ("15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35") 

#draw bar plot 
xx <- barplot(yy, ylab = "", names.arg = name1, ylim = c(0, 140),col="steelblue") 
text(xx, yy + 3, labels = as.character(yy),srt=45) 
mtext(2,text="",line=2) 

xx2 <- xx #c(15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35) 
yy2 <- c(379,474,579,725,922,1181,1473,1846,2316,2962,3688,4786,6069,7605,9504,10680,11074,11074,11074,11483,11484) 

#transform data 
yy2tx <- yy2/14000 * max(pretty(yy)) 

#draw line data 
lines(xx2, yy2tx, lwd = 2,col="red",lty=1) 
points(xx2, yy2tx, pch = 18, cex = 1,col="red") 
text(xx2, yy2tx, labels = as.character(yy2),srt=90) 

#draw axis for transformed data 
par(mai = c (1 , 1, 1, 1)) 
axis(2, at=pretty(c(0,14000))/14000*max(pretty(yy)), labels=pretty(c(0,14000))) 
grid() 

這將產生以下情節

enter image description here

+0

很好的解決了我的問題 – user2724453