简体   繁体   English

如何改变条形图上的颜色?

[英]How to change colors on barplot?

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)) 

Overview 概观

For each height value supplied in barplot() , create a corresponding color. 对于barplot()提供的每个height值,创建相应的颜色。 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 #

As per ?barplot : 根据?barplot

col   a vector of colors for the bars or bar components. By default, grey is used if height is a vector, and a gamma-corrected grey palette if height is a matrix.

You need to add the colours you want as a vector to the col parameter. 您需要将所需的颜色作为向量添加到col参数。 If you specify fewer colours than bars, the colours will be recycled from the start. 如果指定的颜色少于条形,则颜色将从一开始就被回收。

First generate some data. 首先生成一些数据。

# Load packages
library(dplyr, warn.conflicts = FALSE, quietly = TRUE, )

# Generate some miles per gallon per number of cylinders data using the mtcars
foo <- mtcars %>% 
       group_by(cyl) %>% 
       summarise(mpg = mean(mpg))

Plot with rainbow colours 情节与彩虹的颜色

with(foo, barplot(mpg, 
                  names.arg = cyl, 
                  xlab = "Number of cylinders", 
                  ylab = "Mean miles per gallon", 
                  col = rainbow(3)))

Plot with greyscale 用灰度绘图

with(foo, barplot(mpg, 
                  names.arg = cyl, 
                  xlab = "Number of cylinders", 
                  ylab = "Mean miles per gallon", 
                  col = grey.colors(3)))

Make your own colour ramp and then plot 制作自己的颜色渐变,然后绘制

pal <- colorRampPalette(colors = c("lightblue", "blue"))(3)

with(foo, barplot(mpg, 
                  names.arg = cyl, 
                  xlab = "Number of cylinders", 
                  ylab = "Mean miles per gallon", 
                  col = pal))

Plot with a user-specified palette 使用用户指定的调色板绘图

with(foo, barplot(mpg, 
          names.arg = cyl,
          xlab = "Number of cylinders", 
          ylab = "Mean miles per gallon", 
          col = c("#E69F00", "#56B4E9", "#009E73")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM