如何在R中使用geom_bin_2d()使ggplot与ggplotly生成一致的绘图?
问题:使用geom_bin_2d()让ggplot与ggplotly生成一致的二维热图
我想要用ggplotly生成二维热图,实现鼠标悬停查看数值的功能。在RStudio中用ggplot结合geom_bin_2d(),通过以下示例数据框和代码生成了图1:
library(ggplot2) library(plotly) a<-c(1, 2, 2.1, 2.1, 2.2, 7) b<-c(1, 2, 2.1, 2.1, 2.1, 5) dt=data.frame(a,b) plotA<-ggplot(dt, aes(a,b))+geom_bin_2d() plotA
随后在macOS上使用ggplotly(plotA)生成了图2,但图2中的方块过大且相互连接。我尝试了geom_bin_2d(bins = 100)、geom_bin_2d(bins = 100, width =0.1,drop=TRUE)、stat_bin_2d(bins = 10, binwidth = c(0.1,0.1))等参数调整,在Linux(RHEL8.8)上也做了相同尝试,结果几乎一致。而使用ggplot(dt, aes(a,b)) + geom_hex(bins=10)时,ggplot与ggplotly生成的绘图效果相近。
我的环境信息如下:
R version 4.4.3 (2025-02-28) Platform: x86_64-apple-darwin20 Running under: macOS Sequoia 15.4 plotly_4.10.4 ggplot2_3.5.1
请问如何使用geom_bin_2d()使ggplot与ggplotly生成一致的绘图?
内容的提问来源于stack exchange,提问作者user250525
相关产品推荐
相关产品推荐

