NewUsr_stat
NewUsr_stat

Reputation: 2583

Add a box on a plot

I would like to highlight the bar at the right tail of the distribution of my data. Since it is too small I suppose that the best way to do this is by adding a box with red margins around the bar (at 25.0 level on the x axis). How it is possible to add such box on an histogram plot?

Thanks in advance.

enter image description here

Upvotes: 2

Views: 220

Answers (1)

Vincent Guillemot
Vincent Guillemot

Reputation: 3429

It is possible to color both the bars and their borders independently. But for that you need to know how many of those you have !

Here is a proposition, when what you want to do is to single out the first bar on the right to a certain value (here 1.96):

set.seed(123)
x <- rnorm(100)
res.hist <- hist(x, plot=FALSE)

n_bars <- length(res.hist$mids)
left_lim <- res.hist$breaks[1:n_bars]
col_bars <- c("steelblue", "gold")[ diff(left_lim >= 1.96) + 1 ]
col_borders <- c(NA, "red")[ diff(left_lim >= 1.96) +1 ]

plot(res.hist, col=col_bars, border=col_borders)

Histogram with colored bars

Upvotes: 3

Related Questions