You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术需求:用Plotly原生复现ggplot2生成的指定热力图

使用Plotly原生代码复现指定样式的热力图

我已用ggplot2基于给定数据集绘制出目标样式的热力图,ggplot2代码如下:

library(ggplot2)
TestData1<-structure(list(Countries = c("AL", "CA", "DE", "ES", "MA", "EL", 
                                        "AL", "CA", "DE", "ES", "MA", "EL", "AL", "CA", "DE", "ES", "MA", 
                                        "EL"), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
                                                                      2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Total", 
                                                                                                                      "Female", "Male"), class = "factor"), value = c(-3.06, -4.41, 
                                                                                                                                                                      6.97, -2.74, 5.54, -1.31, -2.47, -3.45, 4.11, -3.06, 4.82, -1.73, 
                                                                                                                                                                      -3.95, -5.76, 8.74, -1.87, 6.37, -0.79)), row.names = c(NA, -18L
                                                                                                                                                                      ), class = "data.frame")


ggplot(TestData1, aes(x = Countries, y = factor(variable, levels = rev(levels(factor(variable)))))) +
  
  # 绘制热力图瓦片
  geom_tile(aes(fill = value)) +
  
  # 添加瓦片内数值文本
  geom_text(aes(label = value), color = "white") + 
  # 格式调整
  scale_x_discrete(position = "top") +
  scale_fill_gradient(high = "#132B43", low = "#56B1F7") +
  theme(legend.position = "none", 
        panel.grid = element_blank(), 
        panel.background = element_rect(fill = "white"),
        axis.ticks = element_blank()) +
  labs(y = " ")

目标热力图样式特点:

  • X轴国家标签位于顶部
  • Y轴为反转顺序的分类(Male、Female、Total)
  • 瓦片填充色从浅蓝(#56B1F7)到深蓝(#132B43)渐变
  • 每个瓦片内显示白色数值文本
  • 无图例、网格线、坐标轴刻度,背景为白色

尝试用ggplotly()转换后出现鼠标悬浮显示异常、样式不符等问题,现需Plotly原生代码复现该热力图。


解决方案:Plotly原生代码实现

以下是完全匹配目标样式的Plotly原生代码,同时修复了悬浮提示异常问题:

library(plotly)

# 预处理数据:反转variable的因子水平,保证Y轴顺序与原图表一致
TestData1$variable <- factor(TestData1$variable, levels = rev(c("Total", "Female", "Male")))

# 创建热力图核心层
fig <- plot_ly(
  data = TestData1,
  x = ~Countries,
  y = ~variable,
  z = ~value,
  type = "heatmap",
  colors = colorRamp(c("#56B1F7", "#132B43")), # 匹配原渐变配色
  showscale = FALSE, # 隐藏图例
  hovertemplate = paste(
    "国家: %{x}<br>",
    "分类: %{y}<br>",
    "数值: %{z}<extra></extra>" # 自定义悬浮提示内容
  )
)

# 添加瓦片内的白色数值文本
fig <- fig %>% add_text(
  x = ~Countries,
  y = ~variable,
  text = ~value,
  textfont = list(color = "white"),
  showlegend = FALSE
)

# 布局调整,复刻原图表样式
fig <- fig %>% layout(
  xaxis = list(
    position = "top", # X轴标签移至顶部
    showgrid = FALSE, # 隐藏网格线
    ticks = "" # 隐藏坐标轴刻度
  ),
  yaxis = list(
    title = "", # Y轴标题为空
    showgrid = FALSE, # 隐藏网格线
    ticks = "" # 隐藏坐标轴刻度
  ),
  plot_bgcolor = "white", # 设置白色背景
  margin = list(l = 40, r = 40, t = 40, b = 40) # 调整边距保证标签完整
)

# 显示图表
fig

代码说明

  1. 数据预处理:反转variable的因子水平,确保Y轴显示顺序和原ggplot2图表一致
  2. 热力图配置:通过colors参数匹配原渐变配色,showscale = FALSE隐藏图例,hovertemplate自定义悬浮提示解决显示异常问题
  3. 文本标签:用add_text添加瓦片内的白色数值文本,对应原图表的geom_text
  4. 布局复刻:调整X轴位置、隐藏网格线与刻度、设置白色背景,完全匹配原图表的视觉样式

内容的提问来源于stack exchange,提问作者silent_hunter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 17:17:55