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

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

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

  1. library(highcharter)
  2. data <- data.frame(
  3. X = rep(c(1:20),3),
  4. Y = c(rep(c("A"), 20), rep(c("B"), 20), rep(c("C"), 20)),
  5. Label = rep(c("V","W","X", "Y", "Z"), 6),
  6. Value = round(rnorm(60))
  7. )
  8. data %>%
  9. hchart(type = "heatmap", hcaes(x = X, y = Y, value = Label, color = Value)) %>%
  10. hc_plotOptions(
  11. series = list(
  12. dataLabels = list(enabled = TRUE
  13. ))) %>%
  14. hc_chart(
  15. zoomType = "x"
  16. )

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

  1. library(plotly)
  2. plot_ly(
  3. x = data$X,
  4. y = data$Y,
  5. z = data$Value,
  6. type = "heatmap",
  7. colors = color_scheme,
  8. # colorbar = list(len=20, limits = c(-100, 100)),
  9. showscale=FALSE
  10. ) %>%
  11. add_annotations(
  12. data = data,
  13. x = ~X,
  14. y = ~Y,
  15. text = ~Label,
  16. xref = 'x',
  17. yref = 'y',
  18. showarrow = FALSE,
  19. font=list(color='black'))
f0ofjuux

f0ofjuux1#

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

  1. data2 <- data |>
  2. mutate(
  3. x = X,
  4. y = case_when(
  5. Y == "A" ~ 0, # y needs to be numeric to index the position
  6. Y == "B" ~ 1,
  7. Y == "C" ~ 2
  8. ),
  9. text = Label
  10. ) |>
  11. select(x, y, text)

你可以画出你的热图。

  1. data |>
  2. hchart(type = "heatmap", hcaes(x = X, y = Y, value = Value)) |>
  3. hc_annotations(list(labels = df_to_annotations_labels(data2))) |>
  4. hc_chart(zoomType = "x")
展开查看全部

相关问题