Xiangwu
Xiangwu

Reputation: 745

R, added line plot to barplot, the line points do not align to the x-axis evenly.

enter image description here

For the plot on RIGHT, becuase the bar and line have same scale and limit, so I used the method as follow

(method 1):

mybar<-barplot(x,names.arg=1:10)      // create variable for plot
lines(mybar,y,type="o",pch=19,lwd=2)  // add line which points to x-axis

but for LEFT plot, the barplot data has limit (0,20000) and the lineplot data has limit (0,100000), I tried the above method, but it didn't work (it didn't give error, but the line didn't show). Then I tried the mothod as below

(method 2):

barplot(rbind(colum1,column2), names.arg=nameColumn,
        las=2, ylim=c(0,20000))
par(new = T)
plot(column3, axes = F, type="b",pch=20)
axis(4, at= ,labels= )

How do I centered the point to each bar for the LEFT plot? would method 1 solve the problem?

Thanks.

Upvotes: 0

Views: 7019

Answers (1)

Xiangwu
Xiangwu

Reputation: 745

I found the solution myself.

Firstly in the left plot, the values of line plot were incorrect. Method 1 would work (align the point to the bar), just need to scale the values of line plot.

plot(column3/5, axes = F, type="b",pch=20)

The reason of " it didn't give error, but the line didn't show" was because the left plot limit was set to (0, 20000), the values of line plot exceeded 20000 before scaling.

The plot after correction: enter image description here

====== Update ======

This website gives me a lot helps for this solution: www.cs.odu.edu

Upvotes: 1

Related Questions