簡體   English   中英

如何更改取決於變量的高字符列圖的調色板顏色?

[英]How to change the palette colors of a highcharter column plot which depends on a variable?

我開始在R中使用highcharter程序包(版本0.5.0),但我無法弄清楚如何更改列的顏色,這些列鏈接到系列美學中的變量上。 數據框的開頭如下所示:

> head(Top_G)
# A tibble: 6 × 29
     Rk              Name   Yrs  From    To   ASG     G    PA    AB     R     H   X2B   X3B    HR   RBI
  <int>             <chr> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1   352      Omar Vizquel    24  1989  2012     3  2968 12013 10586  1445  2877   456    77    80   951
2    14 Luis Aparicio HOF    18  1956  1973    13  2599 11230 10230  1335  2677   394    92    83   791
3    75   Dave Concepcion    19  1970  1988     9  2488  9641  8723   993  2326   389    48   101   950
4     1       Bobby Abreu    18  1996  2014     2  2425 10081  8480  1453  2470   574    59   288  1363
5   107  Andres Galarraga    19  1985  2004     5  2257  8916  8096  1195  2333   444    32   399  1425
6    43    Miguel Cabrera    14  2003  2016    11  2096  9001  7853  1321  2519   523    17   446  1553
# ... with 14 more variables: SB <int>, CS <int>, BB <int>, SO <int>, BA <dbl>, OBP <dbl>, SLG <dbl>,
#   OPS <dbl>, Birthdate <date>, Debut <date>, City <chr>, State <chr>, Pos <chr>, Active <chr>

Active變量有兩個可能的值,這是我用來關聯圖表中color美學的值。 我使用的代碼如下:

highchart() %>%
  hc_add_series(Top_G, "column", hcaes(x = Name, y = G, color = Active), name = "G", showInLegend = FALSE) %>%
  hc_colors(cols) %>%
  #hc_add_series(Top_Yrs, name = "Games") %>%
  hc_add_theme(hc_theme_smpl()) %>%
  hc_title(text = "Games", style = list(fontSize = "22px")) %>%
  hc_subtitle(text = "Chart", style = list(fontSize = "12px")) %>%
  hc_yAxis(title = list(text = "G")) %>%
  hc_xAxis(categories = Top_G$Name) %>%
  hc_xAxis(title = list(text = "Player")) %>%
  hc_credits(enabled = TRUE, # add credits
             text = "Test") %>%
  hc_tooltip(crosshairs = FALSE, backgroundColor = "#DDDEE6",
             shared = FALSE, borderWidth = 2)

但是該圖使用默認的調色板顏色,而不是cols變量中定義的顏色。

結果具有默認顏色

這是dput(head(Top_G))的輸出:

> dput(head(Top_G))
structure(list(Rk = c(352L, 14L, 75L, 1L, 107L, 43L), Name = c("Omar Vizquel", 
"Luis Aparicio HOF", "Dave Concepcion", "Bobby Abreu", "Andres Galarraga", 
"Miguel Cabrera"), Yrs = c(24L, 18L, 19L, 18L, 19L, 14L), From = c(1989L, 
1956L, 1970L, 1996L, 1985L, 2003L), To = c(2012L, 1973L, 1988L, 
2014L, 2004L, 2016L), ASG = c(3L, 13L, 9L, 2L, 5L, 11L), G = c(2968L, 
2599L, 2488L, 2425L, 2257L, 2096L), PA = c(12013L, 11230L, 9641L, 
10081L, 8916L, 9001L), AB = c(10586L, 10230L, 8723L, 8480L, 8096L, 
7853L), R = c(1445L, 1335L, 993L, 1453L, 1195L, 1321L), H = c(2877L, 
2677L, 2326L, 2470L, 2333L, 2519L), X2B = c(456L, 394L, 389L, 
574L, 444L, 523L), X3B = c(77L, 92L, 48L, 59L, 32L, 17L), HR = c(80L, 
83L, 101L, 288L, 399L, 446L), RBI = c(951L, 791L, 950L, 1363L, 
1425L, 1553L), SB = c(404L, 506L, 321L, 400L, 128L, 38L), CS = c(167L, 
136L, 109L, 128L, 81L, 20L), BB = c(1028L, 736L, 736L, 1476L, 
583L, 1011L), SO = c(1087L, 742L, 1186L, 1840L, 2003L, 1516L), 
    BA = c(0.272, 0.262, 0.267, 0.291, 0.288, 0.321), OBP = c(0.336, 
    0.311, 0.322, 0.395, 0.347, 0.399), SLG = c(0.352, 0.343, 
    0.357, 0.475, 0.499, 0.562), OPS = c(0.688, 0.653, 0.679, 
    0.87, 0.846, 0.961), Birthdate = structure(c(-983, -13031, 
    -7868, 1530, -3119, 4855), class = "Date"), Debut = structure(c(7032, 
    -5007, 95, 9740, 5713, 12223), class = "Date"), City = c("Caracas", 
    "Maracaibo", "Ocumare de la Costa", "Maracay", "Caracas", 
    "Maracay"), State = c("Distrito Federal", "Zulia", "Aragua", 
    "Aragua", "Distrito Federal", "Aragua"), Pos = c("POS", "POS", 
    "POS", "POS", "POS", "POS"), Active = c("Retirado", "Retirado", 
    "Retirado", "Retirado", "Retirado", "Activo")), .Names = c("Rk", 
"Name", "Yrs", "From", "To", "ASG", "G", "PA", "AB", "R", "H", 
"X2B", "X3B", "HR", "RBI", "SB", "CS", "BB", "SO", "BA", "OBP", 
"SLG", "OPS", "Birthdate", "Debut", "City", "State", "Pos", "Active"
), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

任何提示正確的命令?

提前致謝。

丹尼爾。

您可以創建一個字符串變量,其名稱為(css有效)或十六進制顏色:

Top_G <- mutate(Top_G, coloract = ifelse(Active == "Activo", "#FF0000", "#00FFFF"))

然后使用:

hc_add_series(Top_G, "column", hcaes(x = Name, y = G, color = coloract), name = "G", showInLegend = FALSE)

您可以使用太colorize功能:

Top_G <- mutate(Top_G, coloract = colorize(Active, c("#FF0000", "#00FFFF")))

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM