[R] Customize Legend in Juxtaposed Barplot

Paul Jones raptorman18 at gmail.com
Wed Apr 8 21:54:12 CEST 2009


Hello, I'm making barplots out of binary data,  typically 0=no 1=yes, 
but in some instances I have 2=Don't Know.

Anyway, when I create a juxtaposed barplot, what appears in the legend 
is 0,1,and 2.

I want it to read No, Yes, Don't Know.

I'm not sure how to do that.

In addition to this, the other variable that this juxtaposed barplot is 
grouped by is grade level, which is a string that takes the values of 
Elementary, Middle, and High Schools. When I create the Barplot it 
orders the groups in alphabetical order. Is there anyway I can change 
the order of the juxtaposed groupings to Elementary, Middle, and High 
School?

Also, is it possible to print in smaller text No,Yes, and Don't Know 
under each bar. If this is too hard, I'll just ignore it.


Here is an example of the code that I use.

## This is an R Script to analyze the data from the LVAC Survey

## set working directory and load data
setwd("/home/paul/LVAC/General/")
#load("/home/paul/LVAC/LVAC.RData")
General <- read.csv("/home/paul/LVAC/General.csv")

## This Data is split into three data frames for Elementary,Middle and 
High Schools

png(file="PTA.png")
    x <- table(General$PTA.Support, General$Grade.Level)
    #x <- table(x, exclude=NULL)
    #names(x) <- r ("Yes","No")
    yrange <- range(x,na.rm=TRUE)*1.2
        if (yrange[1]>0) yrange[1] <- 0
        if (yrange[2]<0) yrange[2] <- 0
    bplot <- barplot(x,col=rainbow(if(is.matrix(x)) dim(x) else 
length(x)),beside=TRUE,legend.text=TRUE,ylim=yrange,
        main ="Barplot of PTA Support in Grade Levels")
    text(bplot,x,labels=x,pos=3,offset=.5)




More information about the R-help mailing list