如何在 R 中使用颜色十六进制值作为函数参数?

Posted

技术标签:

【中文标题】如何在 R 中使用颜色十六进制值作为函数参数?【英文标题】:How to use color hex values as a function argument in R? 【发布时间】:2020-10-14 12:27:34 【问题描述】:

我正在使用用户定义的函数创建一个绘图并将颜色作为选项传递。将as_labelquo_namecolors by name(例如greypink)一起使用可以正常工作。

但是当我传递 hex 代码 时,它 失败,即使我只传递没有 # 的 alpha 数值,它仍然不会将其作为字符串。

小例子:

udf_tax_rev_plot <- function(background_line_color = grey)
  
  background_line_color = enquo(background_line_color)
  
  print(as_label(background_line_color) )


udf_tax_rev_plot(33ffff) 

##### output ##### 
Error: unexpected symbol in "udf_tax_rev_plot(33ffff"

实际剧情代码

udf_tax_rev_plot <- function(background_line_color = grey)
  
  background_line_color = enquo(background_line_color)

  tax_rev %>% 
    mutate(highlight_type = case_when(country %in%
                                   c("India","Singapore","Malaysia","Norway",
                                     "Denmark","United States","United Kingdom","China") ~ "Yes",
                                 TRUE ~ "No")) %>%
  ggplot() +
    geom_line(aes(x = year, y = round(tax_revnue_perc_of_gdp,2), col = country), size = 1.1) +
    scale_x_continuous(breaks = seq(from = 1980, to = 2020, by = 5)) +
    scale_y_continuous(labels = label_dollar(prefix = "", suffix = " %"),
                       breaks = seq(from = 0, to = 60, by = 5)) +
    gghighlight(highlight_type == "Yes",
                unhighlighted_params = list(size = 1, colour = alpha(as_label(background_line_color), 0.4))) +
    facet_wrap(~continent) +
    theme_viny_bright() +
    theme(axis.text.x = element_text(angle = 90)
            ) +
    labs(title = "Total Tax Revenue earned % of GDP for world countries across time",
         subtitle = "created by ViSa",
         caption = "Data Source: Gapminder",
         y = "Total Tax Revenue % of GDP" 
         )


udf_tax_rev_plot(#33ffff)

我该如何解决这个问题?

更新

这是带有要重现的gapminder数据的代码

library(tidyverse)
library(scales)
library(gghighlight)
library(gapminder)

udf_tax_rev_plot <- function(background_line_color = grey)
  
  background_line_color = enquo(background_line_color)

  gapminder %>% 
    mutate(highlight_type = case_when(country %in%
                                   c("India","Singapore","Malaysia","Norway",
                                     "Denmark","United States","United Kingdom","China") ~ "Yes",
                                 TRUE ~ "No")) %>%
  ggplot() +
    geom_line(aes(x = year, y = round(gdpPercap,2), col = country), size = 1.1) +
    #scale_x_continuous(breaks = seq(from = 1980, to = 2020, by = 5)) +
    #scale_y_continuous(labels = label_dollar(prefix = "", suffix = " %"),
                       #breaks = seq(from = 0, to = 60, by = 5)) +
    gghighlight(highlight_type == "Yes",
                unhighlighted_params = list(size = 1, colour = alpha(as_label(background_line_color), 0.4))) +
    facet_wrap(~continent) +
    theme_minimal() +
    theme(axis.text.x = element_text(angle = 90)
            ) +
    labs(title = "Total Tax Revenue earned % of GDP for world countries across time",
         subtitle = "created by ViSa",
         caption = "Data Source: Gapminder",
         y = "Total Tax Revenue % of GDP" 
         )


udf_tax_rev_plot(pink)

【问题讨论】:

如果不带引号的变量以 # 或数字开头,则不能传递它们。用户必须使用双引号或单引号 @AllanCameron 哦 ...我尝试使用相同的代码在引号 udf_tax_rev_plot("#33ffff") 中传递十六进制,但它给出了错误。我想我必须修改它来处理"hex color"color_name。但这很奇怪。 是的,长话短说,这里没有理由使用enquo()。只需将颜色作为字符传递并在函数中使用它。 是的@Adam,我同意,但我正在考虑传递颜色名称,因为它不带引号和带或不带引号的十六进制,但我想这不是一个简单的选择,所以会保持简单你提到的方式! 【参考方案1】:

这适用于您提供的数据:

library(tidyverse)
library(scales)
library(gghighlight)
library(gapminder)

udf_tax_rev_plot <- function(background_line_color = "grey")
  
  
  gapminder %>% 
    mutate(highlight_type = case_when(country %in%
                                        c("India","Singapore","Malaysia","Norway",
                                          "Denmark","United States","United Kingdom","China") ~ "Yes",
                                      TRUE ~ "No")) %>%
    ggplot() +
    geom_line(aes(x = year, y = round(gdpPercap,2), col = country), size = 1.1) +
    scale_x_continuous(breaks = seq(from = 1980, to = 2020, by = 5)) +
    scale_y_continuous(labels = label_dollar(prefix = "", suffix = " %"),
                       breaks = seq(from = 0, to = 60, by = 5)) +
    gghighlight(highlight_type == "Yes",
                unhighlighted_params = list(size = 1, colour = alpha(background_line_color, 0.4))) +
    facet_wrap(~continent) +
    theme_minimal() +
    theme(axis.text.x = element_text(angle = 90)
    ) +
    labs(title = "Total Tax Revenue earned % of GDP for world countries across time",
         subtitle = "created by ViSa",
         caption = "Data Source: Gapminder",
         y = "Total Tax Revenue % of GDP" 
    )


udf_tax_rev_plot("pink")

您必须以字符串的形式提供颜色。

所以要么像这样:udf_tax_rev_plot("#33ffff") 或像这样:udf_tax_rev_plot('#33ffff')

【讨论】:

udf_tax_rev_plot("#33ffff") 给出错误:Error: Unknown colour name: "#33ffff"。我将尝试将上面的代码与 gapminder 数据一起放置,以便可以复制它 你能分享一个示例数据框吗?这样会更容易找到问题 我更新了我的答案。它似乎工作。确保不要使用enquo,也不要使用as_label 我已经用 gapminder 数据更新了原始帖子,但我会尝试用你更新的代码来实现。非常感谢!!!

以上是关于如何在 R 中使用颜色十六进制值作为函数参数?的主要内容,如果未能解决你的问题,请参考以下文章

如何将表格十六进制颜色代码#RRGGBB 转换为 R、G、B 值

详细请教:关于十进制RGB颜色值转换十六进制颜色值的算法

如何将字符串颜色转换为其十六进制代码或 RGB 值?

如何在 Excel/VBA 中获取 RGB 颜色的相应十六进制值?

R语言实战——1.3图形初阶

微信小程序使用wxs在页面中调用js函数,颜色值格式转换 rgb和十六进制的转换