Quantcast

Horizontal grid in background of barplot

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Horizontal grid in background of barplot

David Arnold
All,

I have:

x <- matrix(c(22,3,6,69,9,4,7,81,23,4,22,50),nrow=3,byrow=TRUE)
rownames(x) <- c("Cold or flu","Headache","Backache");
colnames(x) <- c("Went to doctor","No response","Did nothing","Self-medicated")
x <- t(x)
print(x)
barplot(x,beside=TRUE,
        ylim=c(0,90),
        xlab="Ailment",
        ylab="Percent",
        legend.text=TRUE,
        args.legend=list("topright",title="Treatment"))
abline(h=c(seq(10,90,10)))
box()

 I'd like to get the horizontal lines in the background.

Any suggestions?

D.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Horizontal grid in background of barplot

Peter Ehlers
On 2012-09-03 12:12, David Arnold wrote:

> All,
>
> I have:
>
> x <- matrix(c(22,3,6,69,9,4,7,81,23,4,22,50),nrow=3,byrow=TRUE)
> rownames(x) <- c("Cold or flu","Headache","Backache");
> colnames(x) <- c("Went to doctor","No response","Did
> nothing","Self-medicated")
> x <- t(x)
> print(x)
> barplot(x,beside=TRUE,
>          ylim=c(0,90),
>          xlab="Ailment",
>          ylab="Percent",
>          legend.text=TRUE,
>          args.legend=list("topright",title="Treatment"))
> abline(h=c(seq(10,90,10)))
> box()
>
>   I'd like to get the horizontal lines in the background.
>
> Any suggestions?

Just plot the bars twice and add the background colour of
the legend region.

  barplot(x,beside=TRUE,
          ylim=c(0,90))

  abline(h=c(seq(10,90,10)))
  box()
  barplot(x,beside=TRUE,
          xlab="Ailment",
          ylab="Percent",
          legend.text=TRUE,
          args.legend=list("topright",title="Treatment",bg="white"),
          add=TRUE)

Peter Ehlers

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Horizontal grid in background of barplot

Jim Lemon
In reply to this post by David Arnold
On 09/04/2012 05:12 AM, David Arnold wrote:

> All,
>
> I have:
>
> x<- matrix(c(22,3,6,69,9,4,7,81,23,4,22,50),nrow=3,byrow=TRUE)
> rownames(x)<- c("Cold or flu","Headache","Backache");
> colnames(x)<- c("Went to doctor","No response","Did
> nothing","Self-medicated")
> x<- t(x)
> print(x)
> barplot(x,beside=TRUE,
>          ylim=c(0,90),
>          xlab="Ailment",
>          ylab="Percent",
>          legend.text=TRUE,
>          args.legend=list("topright",title="Treatment"))
> abline(h=c(seq(10,90,10)))
> box()
>
>   I'd like to get the horizontal lines in the background.
>
> Any suggestions?

Hi David,
Try this:

library(plotrix)
barp(x,col=gray(c(0.2,0.4,0.6,0.8)),names.arg=colnames(x),
  xlab="Ailment",ylab="Percent",ylim=c(0,90),
  do.first=expression(abline(h=seq(10,80,10))))
legend(2.5,85,rownames(x),fill=gray(c(0.2,0.4,0.6,0.8)),
  bg="white")

Jim

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Horizontal grid in background of barplot

Cable, Sam B Civ USAF AFMC AFRL/RVBXI
In reply to this post by David Arnold
A horribly pedestrian alternative to plotrix:

 

barplot(x,beside=TRUE,  ylim=c(0,90),  etc.)

par('usr')    # just getting plot limits

[1]  0.44  15.56  0.00  90.00

par(new=T)

lines(c(0.44,15.56),c(20,20))

lines(c(0.44,15.56),c(40,40))

etc.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Loading...