Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change colors on barplot?

Tags:

r

I have bars that are all pink and want to know how to change them from light to dark of a color like from red to blue, white to red, etc.

    barplot(d1[1:25,]$freq, las = 2, names.arg = 
    stri_trans_totitle(d1[1:25,]$word),
    col = "pink", main ="Most Frequent Words \n in The Three Musketeers",
    ylab = "Word frequencies", ylim=c(0,2000)) 
like image 418
Math Expert Avatar asked Feb 11 '18 04:02

Math Expert


People also ask

How do I change the color of a Barchart in R?

Change fill colors It is also possible to change manually barplot fill colors using the functions : scale_fill_manual() : to use custom colors. scale_fill_brewer() : to use color palettes from RColorBrewer package. scale_fill_grey() : to use grey color palettes.

Can we Colour in bar graph?

Color can be used in bar chart on two levels: 1. Coloring of bars grouped by series. This option is relevant for simple bar charts or stacked bar charts.

How do I change the color of my barplot in Seaborn?

Again we can manually set colors for each bar using the palette parameter of the barplot(). We have to pass the color names within the list as palette values. Apart from manually providing the color values, we can also use predefined color palettes defined in the Seaborn color palettes.


1 Answers

Overview

For each height value supplied in barplot(), create a corresponding color. In this case, I create a color palette that progresses from a gray to a dark blue color.

Reproducible Example

Color Picker helps me translate general colors into hexadecimal color values.

Barplot

# create data frame
df <- data.frame(
  id = 1:5
  , Coolness_Level = 1:5
  , Coolness_Color = NA
  , stringsAsFactors = FALSE
)

# view data
df
# id Coolness_Level Coolness_Color
# 1  1              1             NA
# 2  2              2             NA
# 3  3              3             NA
# 4  4              4             NA
# 5  5              5             NA


# I want colors to progress
# from gray to dark blue
color.function <- colorRampPalette( c( "#CCCCCC" , "#104E8B" ) )

# decide how many groups I want, in this case 5
# so the end product will have 5 bars
color.ramp <- color.function( n = nrow( x = df ) )

# view colors
color.ramp
# [1] "#CCCCCC" "#9DACBB" "#6E8DAB" "#3F6D9B" "#104E8B"

# assign every row in df
# a color
# based on their $Coolness_Level
df$Coolness_Color <-
  as.character(
    x = cut(
      x = rank( x = df$Coolness_Level )  # used to assign order in the event of ties
      , breaks = nrow( x = df )  # same as the 'n' supplied in color.function()
      , labels = color.ramp  # label the groups with the color in color.ramp
    )
  )

# view the data
df
# id Coolness_Level Coolness_Color
# 1  1              1        #CCCCCC
# 2  2              2        #9DACBB
# 3  3              3        #6E8DAB
# 4  4              4        #3F6D9B
# 5  5              5        #104E8B

# make barplot
# and save as PNG
png( filename = "my_cool_barplot.png"
     , units = "px"
     , height = 1600
     , width = 1600
     , res = 300
     )
barplot( height = df$Coolness_Level
         , names.arg = df$id
         , las = 1
         , col = df$Coolness_Color
         , border = NA  # eliminates borders around the bars
         , main = "Is Coolness Correlated with Higher ID #s?"
         , ylab = "Coolness Level"
         , xlab = "ID #"
         )
# shut down plotting device
dev.off()

# end of script #
like image 176
Cristian E. Nuno Avatar answered Sep 21 '22 14:09

Cristian E. Nuno