ggplot2转换为plotly时分面图底部x轴日期显示不全如何解决?
问题原因
你遇到的标签截断问题由两个原因导致:一是仅为第一个分面的x轴设置了自动边距,其余分面(包括最底部的分面)的x轴未开启自动边距;二是画布底部默认留白不足,无法容纳底部的x轴标签。
修复方案
在生成ggplotly对象后,统一配置所有x轴的自动边距,同时增加画布底部留白即可解决,完整可运行代码如下:
library(ggplot2) library(plotly) library(dplyr) # 清理后的可复现测试数据 test_data <- structure(list(Toys = c("Slinky", "Slinky", "Slinky", "Slinky", "Slinky", "Slinky", "Tin Solider", "Tin Solider", "Tin Solider", "Tin Solider", "Tin Solider", "Tin Solider", "Hungry Hungry Hippo", "Hungry Hungry Hippo", "Hungry Hungry Hippo", "Hungry Hungry Hippo", "Hungry Hungry Hippo", "Hungry Hungry Hippo", "Bead Maze", "Bead Maze", "Bead Maze", "Bead Maze", "Bead Maze", "Bead Maze", "Hula Hoop", "Hula Hoop", "Hula Hoop", "Hula Hoop", "Hula Hoop", "Hula Hoop", "Kaleidoscope", "Kaleidoscope", "Kaleidoscope", "Kaleidoscope", "Kaleidoscope", "Kaleidoscope", "Pogo Stick", "Pogo Stick", "Pogo Stick", "Pogo Stick", "Pogo Stick", "Pogo Stick", "Jump N' Dunk Trampoline", "Jump N' Dunk Trampoline", "Jump N' Dunk Trampoline", "Jump N' Dunk Trampoline", "Jump N' Dunk Trampoline", "Jump N' Dunk Trampoline", "Play-Doh", "Play-Doh", "Play-Doh", "Play-Doh", "Play-Doh", "Play-Doh", "Mr. Potato Head", "Mr. Potato Head", "Mr. Potato Head", "Mr. Potato Head", "Mr. Potato Head", "Mr. Potato Head", "Corn Popper", "Corn Popper", "Corn Popper", "Corn Popper", "Corn Popper", "Corn Popper", "Let's Go Fishing", "Let's Go Fishing", "Let's Go Fishing", "Let's Go Fishing", "Let's Go Fishing", "Let's Go Fishing", "Operation", "Operation", "Operation", "Operation", "Operation", "Operation", "Ker Plunk", "Ker Plunk", "Ker Plunk", "Ker Plunk", "Ker Plunk", "Ker Plunk"), Price = c(5.99, 6.99, 7.99, 9, 6, 5.54, 7, 9.99, 6.99, 6.75, 8, 7.99, 9.99, 7.99, 5.99, 8.99, 10.99, 9.75, 9.99, 10.15, 8.99, 6.99, 5.99, 9.99, 9.99, 7.75, 8.75, 9.95, 4.5, 5.54, 3.99, 4.5, 7.5, 8.95, 8.9, 6.99, 150.99, 175.99, 170.99, 180.99, 190.99, 175, 310.64, 335.64, 360.64, 385.64, 410.64, 435.64, 7.99, 8.99, 9.05, 9.1, 9.99, 10.15, 6.75, 8.75, 7.75, 9.75, 6.75, 8.75, 10.35, 10.55, 11, 17, 17.75, 18, 6.97, 7.05, 8.97, 9, 8.99, 6.99, 19.99, 21.15, 16.99, 17.99, 18.99, 14.99, 14.96, 14.97, 15.15, 18.17, 19, 50), Dates = c("1/1/2021", "3/1/2021", "5/1/2021", "7/1/2021", "9/1/2021", "10/1/2021", "1/1/2021", "3/1/2021", "5/1/2021", "7/1/2021", "9/1/2021", "10/1/2021", "1/1/2021", "3/1/2021", "5/1/2021", "7/1/2021", "9/1/2021", "10/1/2021", "1/1/2021", "3/1/2021", "5/1/2021", "7/1/2021", "9/1/2021", "10/1/2021", "1/1/2021", "3/1/2021", "5/1/2021", "7/1/2021", "9/1/2021", "10/1/2021", "1/1/2021", "3/1/2021", "5/1/2021", "7/1/2021", "9/1/2021", "10/1/2021", "1/1/2021", "3/1/2021", "5/1/2021", "7/1/2021", "9/1/2021", "10/1/2021", "1/1/2021", "3/1/2021", "5/1/2021", "7/1/2021", "9/1/2021", "10/1/2021", "1/1/2021", "3/1/2021", "5/1/2021", "7/1/2021", "9/1/2021", "10/1/2021", "1/1/2021", "3/1/2021", "5/1/2021", "7/1/2021", "9/1/2021", "10/1/2021", "1/1/2021", "3/1/2021", "5/1/2021", "7/1/2021", "9/1/2021", "10/1/2021", "1/1/2021", "3/1/2021", "5/1/2021", "7/1/2021", "9/1/2021", "10/1/2021", "1/1/2021", "3/1/2021", "5/1/2021", "7/1/2021", "9/1/2021", "10/1/2021", "1/1/2021", "3/1/2021", "5/1/2021", "7/1/2021", "9/1/2021", "10/1/2021")), row.names = c(NA, -84L), class = "data.frame") # 计算图表高度 chart_height <- test_data %>% dplyr::select(Toys) %>% unique() %>% nrow() * 130 # 生成ggplot对象 p <- test_data %>% ggplot(aes(x = Dates, y = Price, text = paste(paste0("Price: $", sprintf("%.2f", Price)),"<br> Date: ", Dates), group = Toys)) + geom_point(size = 1.5) + geom_line() + facet_wrap(~Toys, scales = "free", ncol = 1) + theme_bw() + theme( title = element_text(colour='black'), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(size = rel(0.85)), panel.grid.major = element_line(colour = "grey70", size = 0.1), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.spacing = unit(0.45,"cm") ) # 生成plotly对象并修复标签截断问题 ply <- ggplotly(p, height = chart_height, tooltip = "text") ply %>% # 增加画布底部留白,b参数单位为像素,可根据实际显示效果调整大小 layout(margin = list(b = 50)) %>% # 为所有x轴开启自动边距,避免标签被切割 style(automargin = TRUE, traces = "x")
如果调整后还是有截断,可以适当调大margin中b的数值,或者将计算chart_height的乘数从130适当调大,给每个分面预留更多高度。
内容的提问来源于stack exchange,提问作者QMan5
相关产品推荐
相关产品推荐

