2014-07-04 62 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