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

How to use color hex values as a function argument in R?

我正在创建一个带有用户定义函数的绘图,并将颜色作为选项传递。通过使用 as_labelquo_name 按名称命名的颜色 可以正常工作,例如 greypink .

但是当我传递 十六进制代码时 然后它 失败 即使我只传递 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)

这适用于您提供的数据:

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