[R] Highlighting a few bars in a barplot
Michael Bibo
michael_bibo at health.qld.gov.au
Fri Sep 10 07:29:33 CEST 2010
Daniel Brewer <daniel.brewer <at> icr.ac.uk> writes:
>
> Hello,
>
> I have a bar plot where I am already using colour to distinguish one set
> of samples from another. I would also like to highlight a few of these
> bars as ones that should be looked at in detail. I was thinking of
> using hatching, but I can't work out how or if you can have a background
> colour and hatching which is different between bars. Any suggestions on
> how I should do this?
>
> Thanks
>
> Dan
Hi Dan,
The following code was part of a response to another person off-list recently.
She wanted to use different hatching angles to highlight different bars which
were already black-and-white coded stacked bars. This approach basically
superimposes hatched bars over the coloured bars, using base graphics. It uses
the "add=TRUE" argument to superimpose the plots. The same principle should
work for what you want.
X1 <- c(2300,2110)
X2 <- c(1300,2220)
X3 <- c(1300,1100)
X4 <- c(450,650)
data <- cbind (X1,X2,X3,X4)
colnames(data) <- c("sample 1","sample 2","sample 3","sample 4")
par(las=1)
par(mar=c(5,5,4,2))
barplot(
data,
beside=FALSE,
horiz=TRUE,
col=c("black","white"),
xlim = c(0,5000)
)
barplot(
cbind(X1,NA,X3), # NA is necessary to correctly space the bars
names.arg=rep("",3), # so that the labels are not overwritten
xlim = c(0,5000),
beside=FALSE,
horiz=TRUE,
density=8, # how dark the cross-hatching lines are
angle=0, # angle of hatch lines
col="black", # colour of hatch lines
add=TRUE # superimposes cross-hatched bars over original bars
)
barplot(
cbind(NA,X2,NA,X4),
names.arg=rep("",4), # a second superimposed plot
xlim = c(0,5000), # is necessary because of the
beside=FALSE, # limitations of the angle argument
horiz=TRUE,
density=8,
angle=90,
col="black",
add=TRUE
)
Hope it helps,
Michael Bibo
michael_bibo<at>health.qld.gov.au
More information about the R-help
mailing list