R语言 包机:第二个系列和偏移散点图上工具提示

col17t5w  于 2023-07-31  发布在  其他
关注(0)|答案(2)|浏览(93)

我有两个系列要打印在我的图表上:箱形图和该箱形图的点作为每个箱形图周围的散点图。
这段代码有两个问题:

  • 首先,我用hc_tooltip(formatter=...)部分为箱线图添加了一个个人工具提示。很好用。我使用hc_add_series添加散点数据,其中我仅为该散点添加工具提示。运行此代码显示的散点工具提示格式与箱线图工具提示格式相同。如何有一个特定的工具提示的散射系列???
  • 第二,我绘制带有抖动的散点。它工作得很好,但点是正确的第一个箱线图,就像我想在中间的中间箱线图,并留到最后一个箱线图。为什么x轴上会有这种偏移?

谢谢

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 = "continent")) %>%
  hc_yAxis(title = list(text = "Life expectancy")) %>%
  hc_title(text= "Boxplot using highcharter") %>% 
  hc_legend(enabled= TRUE) %>%
  hc_tooltip(formatter = JS("function(){
    return ('<strong>' +
          'Continent : ' + this.series.name +
          '<br>Maximum : ' + this.series.data[0].high +
          '<br>Minimum : ' + this.series.data[0].low +
          '</strong>'
  ) }")) %>%
  hc_add_series(
      data = ds,
      type = "scatter",
      hcaes(x = "continent", y = "lifeExp", group = "continent"),
      tooltip = list(pointFormat = "X: {point.lifeExp}")
  ) %>%
  hc_plotOptions(scatter = list(
    color = "grey",
    showInLegend = FALSE,
    marker = list(
      radius = 6,
      symbol = "circle",
      lineWidth = 2
    )
  )) %>%
  hc_plotOptions(scatter = list(jitter = list(x = 0.05, y = 0)))

字符串

ttcibm8c

ttcibm8c1#

出现第一个问题是因为您在图表级别定义的工具提示格式化程序正在覆盖散点系列的工具提示格式。相反,您可以分别为每个系列定义工具提示以自定义它们。
对于您的第二个问题,抖动似乎是Highcharts中与箱线图一起使用时的一个已知问题,不幸的是,这个问题没有一个直接的解决方案。
您可以尝试以下解决方法,我们在系列列表本身中添加工具提示,并删除图表级别的工具提示:

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",                        
)

# Add tooltip to each series
myboxplotData <- lapply(myboxplotData, function(s) {
  s$tooltip <- list(
    headerFormat = paste0('<span style="font-size: 10px">', s$name, '</span><br/>'),
    pointFormat = '<strong>Continent : {series.name}<br/>Maximum : {point.high}<br/>Minimum : {point.low}</strong>'
  )
  s
})

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 = "continent")) %>%
  hc_yAxis(title = list(text = "Life expectancy")) %>%
  hc_title(text= "Boxplot using highcharter") %>% 
  hc_legend(enabled= TRUE) %>%
  hc_add_series(
      data = ds,
      type = "scatter",
      hcaes(x = "continent", y = "lifeExp", group = "continent"),
      tooltip = list(pointFormat = "Life Expectancy: {point.y}")
  ) %>%
  hc_plotOptions(scatter = list(
    color = "grey",
    showInLegend = FALSE,
    marker = list(
      radius = 6,
      symbol = "circle",
      lineWidth = 2
    )
  )) %>%
  hc_plotOptions(scatter = list(jitter = list(x = 0.05, y = 0)))

字符串

enyaitl3

enyaitl32#

这里是我的问题的解决方案,谢谢@madepiet的线索。要点是设置一个空的headerFormat,以便删除自动设置的序列的名称。
我使用了一个“for循环”来设置箱形图的工具提示,因为lapply(第一个madepiet的答案)会给我一个错误。

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

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

continent_colors <- c("red", "green","yellow", "pink","blue")    

myboxplotData <- data_to_boxplot(
  ds,
  lifeExp,
  continent,
  group_var     = continent,
  add_outliers  = FALSE,
  fillColor     = continent_colors,
  color         = "black"                        
)

mylist <- list()
for (tmp in myboxplotData$data) {
  name <- tmp[[1]]$name
  high <- tmp[[1]]$high
  low <- tmp[[1]]$low
  lst <- list(
    headerFormat = '',
    pointFormat = paste0('<strong>Continent : ', name,'<br/>Maximum : ',     high,'<br/>Minimum : ', low, '</strong>')
  )    
  mylist <- append(mylist, list(lst))
}
myboxplotData$tooltip <- mylist

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 = "continent")) %>%
  hc_yAxis(title = list(text = "Life expectancy")) %>%
  hc_title(text= "Boxplot using highcharter") %>% 
  hc_legend(enabled= TRUE) %>%
  hc_add_series(
    data = ds,
    type = "scatter",
    hcaes(x = "continent", y = "lifeExp", group = "continent"),
    color=continent_colors,
    tooltip = list(headerFormat="", pointFormat = "Life Expectancy: {point.y}")
  ) %>%
  hc_plotOptions(scatter = list(
    showInLegend = FALSE,
      marker = list(
      radius = 6,
      symbol = "circle",
      lineWidth = 2
    )
  )) %>%
  hc_plotOptions(scatter = list(jitter = list(x = 0.05, y = 0)))

字符串

相关问题