one approach:
- save default graphic parameters for later restore:
old_pars <- par()
- set character expansion for axis:
par(cex.axis = .5)
- map factor value to color (using {scales} for convenience):
library(scales)
my_colors <- scales::col_numeric('Blues', domain = as.integer(car$Brand),
reverse = TRUE)(as.integer(car$Brand))
plot(car$Brand, las=2, col= my_colors)
- reset graphical parameters:
par(old_pars)
Please note that, while technically possible, using several visual cues for the same property (here: bar height and color for Brand count) is likely to bias the viewer's impression: https://en.wikipedia.org/wiki/Chartjunk
Edit
To truly map colours to values (so that e.g. tied frequency ranks share the same colour):
df <- data.frame(Brand = rep(c('Volvo', 'Mercedes-Benz', 'Peugeot', 'Audi'), times = c(12, 12, 5, 2)))
- summarise brands by frequency:
brand_count <- table(car$Brand) |> sort(decreasing = TRUE)
my_colors <- scales::col_numeric('Blues', domain = brand_count)
barplot(brand_count, col = my_colors(as.integer(brand_count)))
(barplot
being implicity called by plot(some_factor)
when the y-value is numeric like e.g. frequency of the factor level)
output:
