I need to change the color and fill of a barplot

Viewed 27

I have the following code that I used to create a barplot:

barplot(table(data$r_difficulty), ylab = "count", xlab = "r_difficulty") 

I was wondering how I can change the fill and color with this code.

1 Answers

Using the col= argument for the fill color and border= for the border, among others it accepts numeric vectors of predefined colors from 1 to 8 (see below).

barplot(table(dat$value), ylab = "count", xlab = "r_diff", col=1:8, border=8:1)

You may also:

  • use a vector of color names such as c("red", "blue"), check colors() for all available color names

  • define any RGB color such as #FF0000, or #FF000000 where the additional two digits are for the alpha (transparency)

  • use color palettes such as the builtin rainbow, hcl.colors, heat.colors etc., or those of a whole bunch of additional packages

  • mix some of the listed possibilities

enter image description here


Data:

set.seed(42)
dat <- data.frame(value=sample(8, 100, replace=TRUE))
Related