0

I am very new to R and thanks to the provided help here so far, I managed to create the following Barplot by using the following code:

res <- data.frame( aggregate( rentals ~ weatherCond + season, bikes, sum ))

res$weatherCond <- ordered(res$weatherCond,
levels = c(1, 2, 3, 4),
labels = c("Clear", "Fog", "Snow", "Storm"))

res$season <- factor(res$season,
levels = c(1, 2, 3, 4),
labels = c("Winter", "Spring", "Summer", "Autumn"))

par(mar=c(10,4,1,1)+.5) 
barplot( res[,"rentals"], names=apply( res[,1:2], 1, function(x) paste0(x[1],"_",x[2]) ), las=3 )

The plot visualizes the connection between the number of RENTED bikes and the SEASONs depending on the WEATHER conditions (3 variables). The bars in the plot show the amount of bikes RENTED, separated into the corresponding SEASON/WEATHER combinations.

enter image description here

  1. How should I modify the code in order to display the number of rented bikes above every single bar in the barplot?
  2. How should I modify the code in order to adjust the colors: blue for every bar, which is "Winter", Green for "Spring", Red for "Summer" and Yellow for "Autumn". (I am also to open to ideas for distributing the colors in a better way).
Embargo96
  • 63
  • 2
  • 7
  • 2
    try this https://stackoverflow.com/questions/53134623/text-in-barplot-in-r for a more helpful answer, you should add a small example of `bikes` using `dput(...)` – rawr Nov 19 '21 at 20:12
  • 1
    since the vector defining your colors is a `factor` (either season or weather condition), you could simply do `barplot(res$rentals, col = res$season)` or use a custom color vector `barplot(res$rentals, col = c('blue', 'red', 'green', 'grey')[res$season])` which will map each color to each factor level since they are stored as integers, so this expands to `c('blue', 'red', 'green', 'grey')[c(1, 1, 1, ..., 4, 4, 4)]` – rawr Nov 19 '21 at 20:49
  • @rawr thank you very much! It seems to work. There is just one small issue. Maybe I'm adding the barplot(res$rentals, col = res$season) code in the wrong place, but when I run it, the colors appear but everything else (values, labels, title) disappears. What could be the solution? Any additional help would be very appreciated! – Embargo96 Nov 20 '21 at 00:35
  • just add col in addition to what you already have – rawr Nov 20 '21 at 01:58

0 Answers0