Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: barplot with colors as a function of y-axis value

I have this simple code (data + barplot) :

dat <- c('Jan','Feb','Mar', 'Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
val <- c(-2.5, 3, 2.5, -3.3, -1, 0.2, 6, 4.3, 5.5, 2, -1.9, -2.3)
df <- data.frame(dat, val)
bar <- ggplot(data = df, aes(x = factor(dat, levels = month.abb), y = val)) +
    geom_bar(stat = 'identity')
print(bar)

Elsewhere I created a heatmap using the following palette:

# Palette
LtoM <-colorRampPalette(c('red', 'yellow' ))
Mid <- "snow3"
MtoH <-colorRampPalette(c('lightgreen', 'darkgreen'))

that is called by:

scale_fill_gradient2(low = LtoM(100), mid = Mid, high = MtoH(100))

Now I would like to use a similar color palette for my barplot, that is I would like the color in each bar to be a function of the height (some grade from red for mimimum y to green for maximum y).

How can I do that, please?

like image 931
tagoma Avatar asked Nov 21 '13 11:11

tagoma


People also ask

How do I add color to a barplot in R?

To set colors for bars in Bar Plot drawn using barplot() function, pass the required color value(s) for col parameter in the function call. col parameter can accept a single value for color, or a vector of color values to set color(s) for bars in the bar plot.

How do I change the color of a barplot?

You can change the color of bars in a barplot using color argument. RGB is a way of making colors. You have to to provide an amount of red, green, blue, and the transparency value to the color argument and it returns a color.

How do I change the color of a stacked barplot in R?

You can change the colors of the stacked bars with a predefined color palette, such as the ones provided by scale_fill_brewer . If you prefer choossing each of the colors you can use scale_fill_manual and pass the vector of colors to the values argument.

What Ggplot Geom function would you use to create a bar graph?

There are two types of bar charts: geom_bar() and geom_col() . geom_bar() makes the height of the bar proportional to the number of cases in each group (or if the weight aesthetic is supplied, the sum of the weights). If you want the heights of the bars to represent values in the data, use geom_col() instead.


1 Answers

You're basically there with your current code. Just add the scale_fill_gradient function.

bar <- ggplot(data = df, aes(x = factor(dat, levels = month.abb), y = val, 
              fill=val)) +
       geom_bar(stat = 'identity') + 
       scale_fill_gradient2(low=LtoM(100), mid='snow3', 
       high=MtoH(100), space='Lab')

custom colors

However, allowing scale_fill_gradient2 to take care of the gradient-ing works pretty well

bar <- ggplot(data = df, aes(x = factor(dat, levels = month.abb), y = val, 
              fill=val)) + 
       geom_bar(stat = 'identity') + 
       scale_fill_gradient2(low='red', mid='snow3', high='darkgreen', space='Lab')

generated colors

like image 136
lilster Avatar answered Oct 16 '22 01:10

lilster