R语言 包机:热图显示非数字数据表

icomxhvb  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(124)

我正在试图弄清楚如何在热图上显示非数字标签。样本代码:

library(highcharter)    
data <- data.frame(
      X = rep(c(1:20),3),
      Y = c(rep(c("A"), 20), rep(c("B"), 20), rep(c("C"), 20)),
      Label = rep(c("V","W","X", "Y", "Z"), 6),
      Value = round(rnorm(60))
    )
    

data %>%
  hchart(type = "heatmap", hcaes(x = X, y = Y, value = Label, color = Value)) %>%
  hc_plotOptions(
    series = list(
      dataLabels = list(enabled = TRUE
      ))) %>%
  hc_chart(
    zoomType = "x"
  )

我可以用Plotly创建我想要的东西,但是对于更大的图来说它变得非常慢,我想手动为值/标签分配颜色。

library(plotly)
plot_ly(
  x = data$X,
  y = data$Y,
  z = data$Value,
  type = "heatmap",
  colors = color_scheme,
  # colorbar = list(len=20, limits = c(-100, 100)),
  showscale=FALSE
) %>%
  add_annotations(
    data = data,
    x = ~X, 
    y = ~Y, 
    text = ~Label, 
    xref = 'x', 
    yref = 'y', 
    showarrow = FALSE, 
    font=list(color='black'))
f0ofjuux

f0ofjuux1#

选项dataLabels用于标记值,您可以在hc_annotations中使用labels。首先,创建一个用于注解的数据框:

data2 <- data |>
  mutate(
    x = X,
    y = case_when(
      Y == "A" ~ 0, # y needs to be numeric to index the position
      Y == "B" ~ 1,
      Y == "C" ~ 2
    ),
    text = Label
  ) |>
  select(x, y, text)

你可以画出你的热图。

data |>
  hchart(type = "heatmap", hcaes(x = X, y = Y, value = Value)) |>
  hc_annotations(list(labels = df_to_annotations_labels(data2))) |>
  hc_chart(zoomType = "x")

相关问题