and-bri
and-bri

Reputation: 1664

How to plot a empty barplot?

I like to use it for prepare the plot, add horizontal lines and finally plot the bars. When I do it like below, R plots every thing twice and this looks not so nice. For a normal plot the equivalent I search is: plot(NULL, xlim=c(1,2), ylim=c(3,5), axes=F)

data = c(1,4,2) 
barplot(data)
abline(h=seq(0,5,1), col="red")
barplot(data, add=T)

I prefer to have a base solution.

Upvotes: 9

Views: 2513

Answers (2)

camel_case
camel_case

Reputation: 63

I am not entirely sure if this is what you are looking for. You can "plot" with white bar and white boarder line. For example,

data = c(1,4,2) 
# "white" / rgb(1,1,1) / rgb(255,255,255, maxColorValue=255) all identical 
barplot(data, col=rgb(255, 255, 255, maxColorValue=255), border="white")
abline(h=seq(0,5,1), col="red")
barplot(data, add=T, xaxt="n", yaxt="n")
axis(2, at=c(0:4), label=c(0:4))
axis(1)    

but this will produce same plot that is produced by your sample code.

Upvotes: 2

d.b
d.b

Reputation: 32558

Plotting with col and border set to NA and axes = FALSE will effectively create an empty plot. Then you can add abline and actual barplot

data = c(1,4,2) 
barplot(data, col = NA, border = NA, axes = FALSE)
abline(h=0:5, col="red")
barplot(data, add = TRUE)

Upvotes: 7

Related Questions