高charter箱线图的图例颜色在R中

huangapple go评论88阅读模式
英文:

Legend color of highcharter boxplot in R

问题

我尝试按照这里描述的方式构建箱线图 https://rpubs.com/techanswers88/highcharter-boxplot-in-r

安装包("highcharter")
安装包("dplyr")
安装包("gapminder")

载入库(highcharter)
载入库(dplyr)
载入库(gapminder)

数据集 <- gapminder %>%
   dplyr::filter(year == 2007) %>%
   dplyr::arrange(-pop)

我的箱线图数据 <- data_to_boxplot(
    数据集,
    lifeExp,
    continent,
    group_var     = continent,
    add_outliers  = FALSE,
    fillColor     = c("red", "green","yellow", "pink","blue"),
    color        = "black",                        
   )

highchart()%>%
hc_xAxis(type ="category") %>%
hc_add_series_list(我的箱线图数据) %>%
hc_xAxis(title = list(text = "大陆")) %>%
hc_yAxis(title = list(text = "预期寿命")) %>%
hc_title(text= "使用 highcharter 的箱线图") %>% 
hc_subtitle(text= "各大陆的预期寿命") %>% 
hc_caption(text= "基于 2007 年的人口数据") %>% 
hc_legend(enabled= FALSE)

它运行正常。

现在我想在底部添加图例,所以我将

hc_legend(enabled= FALSE)

改为

hc_legend(enabled= TRUE)

图例的名称出现了,但所有颜色都是黑色,而不是在 data_to_boxplot 函数中定义的 fillColor 中的颜色。

所以我尝试了:

color = c("red", "green","yellow", "pink","blue")

而不是

color        = "black"

这样做对于图例颜色有效,但箱线图的形状和中位数消失了(不再是黑色)。

有没有办法保持箱线图和中位数的形状为黑色,图例与箱线图的 fillColor 相同的颜色?谢谢

英文:

I tried to build boxplot as described here https://rpubs.com/techanswers88/highcharter-boxplot-in-r

install.packages("highcharter")
install.packages("dplyr")
install.packages("gapminder")

library(highcharter)
library(dplyr)
library(gapminder)

ds <- gapminder %>%
   dplyr::filter(year == 2007) %>%
   dplyr::arrange(-pop)

myboxplotData <- data_to_boxplot(
    ds,
    lifeExp,
    continent,
    group_var     = continent,
    add_outliers  = FALSE,
    fillColor     = c("red", "green","yellow", "pink","blue"),
    color        = "black",                        
   )

highchart()%>%
hc_xAxis(type ="category") %>%
hc_add_series_list(myboxplotData) %>%
hc_xAxis(title = list(text = "contient")) %>%
hc_yAxis(title = list(text = "Life expectancy")) %>%
hc_title(text= "Boxplot  using highcharter") %>% 
hc_subtitle(text= "Life expectancy of each continent") %>% 
hc_caption(text= "Based on year 2007 population") %>% 
hc_legend(enabled= FALSE)

and its working fine

Now I wanted to add a legend at the bottom so I changed

hc_legend(enabled= FALSE)

to

hc_legend(enabled= TRUE)

The name of the legends appears but all colors are black instead of those defined in fillColor in the data_to_boxplot function.

So I tried :

color = c("red", "green","yellow", "pink","blue")

instead of

color        = "black"

and it works for legend color BUT the boxplot shape and the median disappears (not black anymore)

Is there a way to keep the shape of boxplot and median black and the legens with the same color as the boxplot fillColor ? Thanx

答案1

得分: 1

在Highcharter中,color参数实际上改变了箱线图的轮廓和中位数的颜色。您在使用fillColor参数来改变箱线图本身的颜色时是正确的,但不幸的是,在R中的Highcharter库不会自动使用fillColor来设置图例符号的颜色。

解决这个问题的一种方法是直接在hc_chart函数内使用Highcharts.setOptions方法来改变图例符号的颜色。以下是如何实现的:

install.packages("highcharter")
install.packages("dplyr")
install.packages("gapminder")

library(highcharter)
library(dplyr)
library(gapminder)

ds <- gapminder %>%
  dplyr::filter(year == 2007) %>%
  dplyr::arrange(-pop)

myboxplotData <- data_to_boxplot(
  ds,
  lifeExp,
  continent,
  group_var     = continent,
  add_outliers  = FALSE,
  fillColor     = c("red", "green","yellow", "pink","blue"),
  color        = "black",                        
)

highchart()%>%
  hc_chart(
    events = list(
      load = JS("function () {
        Highcharts.each(this.series, function (series) {
          series.legendSymbol.attr({ fill: series.options.fillColor });
        });
      }")
    )
  ) %>%
  hc_xAxis(type ="category") %>%
  hc_add_series_list(myboxplotData) %>%
  hc_xAxis(title = list(text = "contient")) %>%
  hc_yAxis(title = list(text = "Life expectancy")) %>%
  hc_title(text= "Boxplot using highcharter") %>%
  hc_subtitle(text= "Life expectancy of each continent") %>%
  hc_caption(text= "Based on year 2007 population") %>%
  hc_legend(enabled= TRUE)

这段代码会在加载图表时使用JavaScript来改变图例符号的颜色,使其与箱线图的颜色一致。

英文:

In Highcharter, the color argument actually changes the color of the boxplot outline and median. You're correct in using the fillColor argument to change the color of the boxplot itself, but unfortunately, the Highcharter library in R does not automatically use fillColor for the legend symbols.

One solution to this issue is to change the color of the legend symbols using JavaScript directly within the hc_chart function using the Highcharts.setOptions method. Here is how you can do it:

install.packages(&quot;highcharter&quot;)
install.packages(&quot;dplyr&quot;)
install.packages(&quot;gapminder&quot;)

library(highcharter)
library(dplyr)
library(gapminder)

ds &lt;- gapminder %&gt;%
  dplyr::filter(year == 2007) %&gt;%
  dplyr::arrange(-pop)

myboxplotData &lt;- data_to_boxplot(
  ds,
  lifeExp,
  continent,
  group_var     = continent,
  add_outliers  = FALSE,
  fillColor     = c(&quot;red&quot;, &quot;green&quot;,&quot;yellow&quot;, &quot;pink&quot;,&quot;blue&quot;),
  color        = &quot;black&quot;,                        
)

highchart()%&gt;%
  hc_chart(
    events = list(
      load = JS(&quot;function () {
        Highcharts.each(this.series, function (series) {
          series.legendSymbol.attr({ fill: series.options.fillColor });
        });
      }&quot;)
    )
  ) %&gt;%
  hc_xAxis(type =&quot;category&quot;) %&gt;%
  hc_add_series_list(myboxplotData) %&gt;%
  hc_xAxis(title = list(text = &quot;contient&quot;)) %&gt;%
  hc_yAxis(title = list(text = &quot;Life expectancy&quot;)) %&gt;%
  hc_title(text= &quot;Boxplot  using highcharter&quot;) %&gt;% 
  hc_subtitle(text= &quot;Life expectancy of each continent&quot;) %&gt;% 
  hc_caption(text= &quot;Based on year 2007 population&quot;) %&gt;% 
  hc_legend(enabled= TRUE)

huangapple
  • 本文由 发表于 2023年7月3日 23:08:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76606013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定