从 ggplot2 转换时,Plotly 绘图未正确呈现

Posted

技术标签:

【中文标题】从 ggplot2 转换时,Plotly 绘图未正确呈现【英文标题】:Plotly plot is not rendering correctly when it is converted from ggplot2 【发布时间】:2021-12-11 22:38:51 【问题描述】:

当我从 ggplot 转换为 plotly 时,构面图上的日期似乎不太适合页面。我希望在最后一个多面图上的页面末尾和 x 轴上的日期之间获得更多空间。我试图动态地做到这一点,但它似乎不起作用。有谁知道解决这个问题的方法吗?

这是我的代码:

library(ggplot2)
library(plotly)

#reproducible data here
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")                                                                                                                                                                    "collector")), delim = ","), class = "col_spec")\



chart_height <- test_data %>% 
  dplyr::select(Toys) %>% 
  unique() %>% 
  nrow() * 130

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")
  )

ggplotly(p, height = chart_height, tooltip = "text", xaxis = list(automargin = T))

这是我的意思的图片(突出显示的部分是我在说什么):

【问题讨论】:

【参考方案1】:

不完全确定您所说的“x 轴被切断”是什么意思(因为从您的图片中很清楚实际上可以看到 x 轴和刻度值等)。

我认为有一个问题是您的 Dates 列是字符。您可以通过以下方式解决此问题:

test_data <- test_data %>% mutate(Dates=as.Date(Dates, format="%m/%d/%Y"))

要在下面添加空格,您可以使用layoutmargin

ggplotly(p, height = chart_height, tooltip = "text", xaxis = list(automargin = T)) %>% 
  layout(margin=list(b=50))

【讨论】:

我试图在页面末尾和 x 轴上显示的日期之间留出更多空间。我会尝试在问题中澄清这一点。 好的,这很有帮助 - 您可以在布局中使用边距

以上是关于从 ggplot2 转换时,Plotly 绘图未正确呈现的主要内容,如果未能解决你的问题,请参考以下文章

用 plotly 绘制多个 ggplot2 绘图

r 常见绘图任务的语法与ggplot2和plotly

向ggplotly绘图添加垂直线

使用 plotly 或 Network 3D 将 igraph 转换为交互式绘图

使用 Plotly 和 R 的悬停模式

R中的交互式绘图