首页 文章

饼图高图R

提问于
浏览
5

我正在使用highcharter绘制一个饼图 . 当鼠标悬停在每个切片上时<它只显示一个值,但我想添加另一个值,以便它显示两个!这是一个示例代码:在我的代码中,您看到它仅使用A列和B列绘制数据,但我希望它将C列显示为鼠标悬停在切片上的附加信息 .

library(highcharter)
A <- c("a", "b", "c", "d")
B <- c(4, 6, 9, 2)
C <- c(23, 26, 13, 15)
df <- data.frame(A, B, C)

highchart() %>% 
  hc_chart(type = "pie") %>% 
  hc_add_series_labels_values(labels = df$A, values = df$B)%>%    

  hc_tooltip(crosshairs = TRUE, borderWidth = 5, sort = TRUE, shared = TRUE, table = TRUE,
             pointFormat = paste('<b>{point.percentage:.1f}%</b>')
  ) %>%

  hc_title(text = "ABC",
           margin = 20,
           style = list(color = "#144746", useHTML = TRUE))

2 回答

  • 1

    您可以在高级图表中创建具有自定义名称的图表,如下所示:http://jsfiddle.net/zh65suhm/ . 也就是说,将工具提示更改为以下内容:

    '<b>{point.percentage:.1f}%</b><br>Custom point: <b>{point.customData}</b>'
    

    每个点都有自己的 customData 值 .

    我没有使用过highcharter,但是看看API可能有一些工作:

    hc_add_series(favorite_bars, "pie", hcaes(name = bar, y = percent), name = "Bars") %>%
    

    这可能有用:

    hc_add_series(favorite_bars, "pie", hcaes(name = bar, y = percent, customData = variable_with_customdata), name = "My pie") %>%
    

    希望这会帮助你 .

  • 2

    据我所知,@ ewolden提出的解决方案在 highcharter 中不起作用 .
    一个简单而灵活的解决方案,用于在 highcharter 饼的工具提示中设置其他信息,如下所示:

    library(highcharter)
    A <- c("a", "b", "c", "d")
    B <- c(4, 6, 9, 2)
    C <- c(23, 26, 13, 15)
    df <- data.frame(A, B, C)
    
    # A modified version of the "hc_add_series_labels_values" function
    # The "text" input is now available
    myhc_add_series_labels_values <- function (hc, labels, values, text, colors = NULL, ...) 
    {
        assertthat::assert_that(is.highchart(hc), is.numeric(values), 
            length(labels) == length(values))
        df <- dplyr::data_frame(name = labels, y = values, text=text)
        if (!is.null(colors)) {
            assert_that(length(labels) == length(colors))
            df <- mutate(df, color = colors)
        }
        ds <- list_parse(df)
        hc <- hc %>% hc_add_series(data = ds, ...)
        hc
    }
    
    # Set the "text" input in myhc_add_series_labels_values
    # point.text is now available inside pointFormat of hc_tooltip
    highchart() %>% 
      hc_chart(type = "pie", data=df) %>% 
      myhc_add_series_labels_values(labels=A, values=B, text=C) %>% 
      hc_tooltip(crosshairs=TRUE, borderWidth=5, sort=TRUE, shared=TRUE, table=TRUE,
         pointFormat=paste('<br><b>A: {point.percentage:.1f}%</b><br>C: {point.text}')) %>%
      hc_title(text="ABC", margin=20, style=list(color="#144746", useHTML=TRUE))
    

    enter image description here

    使用此解决方案,我们可以在工具提示中打印两个或更多变量的内容 .
    下面我们在 df 数据集中添加一个变量 D ,并在工具提示中将其可视化:

    D <- c("Mars", "Jupiter", "Venus", "Saturn")
    df <- data.frame(A, B, C, D)
    txt <- paste("C:",C," <br>D:", D)
    
    highchart() %>% 
      hc_chart(type="pie", data=df) %>% 
      myhc_add_series_labels_values(labels=A, values=B, text=txt) %>% 
      hc_tooltip(crosshairs=TRUE, borderWidth=5, sort=TRUE, shared=TRUE, table=TRUE,
              pointFormat=paste('<br><b>A: {point.percentage:.1f}%</b><br>{point.text}')) %>%
      hc_title(text = "ABC", margin = 20, style = list(color = "#144746", useHTML = TRUE))
    

    enter image description here

相关问题