[R] Barplot with Secondary axis
Rui Barradas
ruipbarradas at sapo.pt
Wed Aug 22 20:41:26 CEST 2012
Hello,
You were almost there.
x = c("a","b","c","d")
y= cbind(c(50,40,30,20),c(40,30,20,10))
y2 = c(0.80,0.65,0.75,0.50)
#
bp <- barplot(t(y), beside = TRUE)
xlim <- c(floor(min(bp)), ceiling(max(bp)))
#
par(new=T)
plot(colMeans(bp), y2, type="o", col="black", lwd=3,
lty=1, xaxt="n", yaxt="n", xlab="", ylab="", xlim = xlim)
points(colMeans(bp), y2, pch=20)
The trick was to get rid of xaxt = "n" for a while, to see what was
going on with the x axis: it wasn't only the y axis that was changing.
An act accordingly.
Hope this helps,
Rui Barradas
Em 22-08-2012 14:43, vikrant escreveu:
> Hi all,
>
> I am trying to plot a bar chart and trying to plot a line as a secondary
> axis as my scale is different for two y axis.
>
> I am plotting a clustered bar chart by using besides = True option in
> barplot function and my y coordinates are not plotted exactly at the center
> on each two bars. Please help me.
>
> I am pasting the code as follows.
>
> x = c("a","b","c","d")
> y= cbind(c(50,40,30,20),c(40,30,20,10))
> y2 = c(0.80,0.65,0.75,0.50)
> barplot(t(y),beside = TRUE)
> par(new=T)
> plot(y2,,,type="o",col="black",lwd=
> 3,lty=1,xaxt="n",yaxt="n",xlab="",ylab="")
> points(y2,pch=20)
>
> I also tried following code for plotting the line point exactly at the
> center of two bars:
>
> x = c("a","b","c","d")
> y= cbind(c(50,40,30,20),c(40,30,20,10))
> y2 = c(0.80,0.65,0.75,0.50)
> barplot(t(y),beside = TRUE)
> a = barplot(t(y),beside = TRUE)
> par(new=T)
> plot(colMeans(a),y2,type="l",col="black",lwd=
> 1,lty=1,xaxt="n",yaxt="n",xlab="",ylab="")
> points(colMeans(a),y2,pch=20)
>
> Please help me in plotting this graph
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Barplot-with-Secondary-axis-tp4640980.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list