Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a border around a barplot in R the same way a border is drawn for a boxplot

Tags:

plot

r

I'm trying to get the same effect of isolating the plot of a barplot in the same way R isolates a boxplot with a border by default. In other words, I want the border that appears in the first plot below to appear in the second plot:

par(mfrow=c(2,1))

## boxplot on a formula:
boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
# *add* notches (somewhat funny here):
boxplot(count ~ spray, data = InsectSprays,
        notch = TRUE, add = TRUE, col = "blue")


require(grDevices) # for colours
tN <- table(Ni <- stats::rpois(100, lambda=5))
r <- barplot(tN, col=rainbow(20))
#- type = "h" plotting *is* 'bar'plot
lines(r, tN, type='h', col='red', lwd=2)
like image 905
Atticus29 Avatar asked Mar 05 '13 23:03

Atticus29


1 Answers

You just add box() at the end of your barplot code.

par(mfrow=c(2,1))
boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
boxplot(count ~ spray, data = InsectSprays,
        notch = TRUE, add = TRUE, col = "blue")
require(grDevices) # for colours
tN <- table(Ni <- stats::rpois(100, lambda=5))
r <- barplot(tN, col=rainbow(20))
box()
lines(r, tN, type='h', col='red', lwd=2)
like image 151
alexwhan Avatar answered Nov 07 '22 03:11

alexwhan