英文:
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("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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论