简体   繁体   English

重新调整R ggplotly

[英]Resize plotly R ggplotly

I'm having some trouble using the plotly R package. 我在使用情节R包时遇到了一些麻烦。 I'm very new to plotly but I loved that I could use ggplot-like syntax so I'm trying to make it work. 我很擅长剧情,但我喜欢我可以使用类似ggplot的语法,所以我试图让它工作。

I created a faceted plot where you can hover over a datapoint and see details about that record. 我创建了一个刻面图,您可以将鼠标悬停在数据点上,并查看有关该记录的详细信息。 I'm very happy with the plot, but I'd like to resize it so the y-axis of each plot isn't so short, as in I'd like to adjust the height and width of the overall plot. 我对这个情节很满意,但是我想调整它的大小,所以每个情节的y轴都不是那么短,因为我想调整整个情节的高度和宽度。

As is, I can't figure out how to override the default sizing and I'm pulling my hair out because all of the examples I can find use plot_ly() rather than ggplotly(). 就像我一样,我无法弄清楚如何覆盖默认的大小调整,我正在拉我的头发,因为我可以找到的所有示例都使用plot_ly()而不是ggplotly()。 I'd rather not rebuild the plot just to adjust the sizing unless I need to. 除非我需要,否则我宁愿不重建情节只是为了调整尺寸。

The code I'm running currently is really simple: 我目前运行的代码非常简单:

plot <- ggplot(data = counts_country, aes(x = Year, y = Count, color = Region, text = paste("country:", Country))) +
  geom_point(size= 2, alpha = (1/2)) + 
  facet_wrap(~ Region, ncol = 1)

(gg_plot <- ggplotly(plot))

You can see exactly what I'm working with here: http://rpubs.com/dbouquin/180894 你可以在这里看到我正在使用的确切内容: http//rpubs.com/dbouquin/180894

I tried adjusting the plot to show two rows of plots but still have trouble because the year labels get smashed together. 我尝试调整情节以显示两排情节,但仍然有问题,因为年份标签被粉碎在一起。 Resizing seems like all I need. 调整大小似乎就像我需要的一样。

Here's a workaround. 这是一个解决方法。 Seems to work in R-Markdown documents which I am guessing is what you need? 似乎在R-Markdown文档中工作我估计你需要什么? It still preserves the ggplot2 syntax but uses plotly_build() instead of ggplotly() 它仍保留ggplot2语法,但使用plotly_build()而不是ggplotly()

---
output: html_document
---

### Using ggplotly()
```{r, warning = F, message = F}
library(plotly)
library(dplyr)

gg <- mtcars %>% 
  ggplot(aes(wt, mpg, color = gear)) + 
  geom_point() + 
  facet_wrap(~hp)

ggplotly(gg)
```

### Using plotly_build()
```{r}
ggp_build <- plotly_build(gg)
ggp_build$layout$height = 800
ggp_build$layout$width = 600

ggp_build
```

Looks like so: 看起来像这样:

在此输入图像描述

在此输入图像描述

Just like plot_ly() , ggplotly() has a height and a width argument, see ?ggplotly . 就像plot_ly()ggplotly()有一个height和一个width参数,请参阅?ggplotly

Generally speaking it is good practice to always set height / width . 一般来说,总是设置height / width是一种好习惯。 If you don't, and you share your plot with someone else, the sizing might be different depending on the context. 如果您不这样做,并且您与其他人分享您的情节,则尺寸可能会因上下文而异。

This is especially important for ggplotly() . 这对ggplotly()尤为重要。 At print time, in order to convert the sizing of things, it has to assume a height / width . 在打印时,为了转换事物的大小,它必须假定height / width If you don't specify these, it uses the size of your R graphics device, and we can't always guarantee the resize behavior is entirely consistent with what you'd get from your R graphics device. 如果您没有指定这些,它会使用R图形设备的大小,我们无法始终保证调整大小行为与您从R图形设备获得的内容完全一致。

I recommend you update to the latest version of plotly. 我建议你更新到plotly的最新版本。

To see which version you're on: packageVersion("plotly") 要查看您所使用的版本: packageVersion("plotly")

You can then add the width and height parameters to your ggplotly() function calls. 然后,您可以将widthheight参数添加到ggplotly()函数调用中。

el <- as.data.frame(economics_long[,1:3])
econp <- ggplot(el, aes(date, value, group=variable)) + 
  geom_line()+
  facet_grid(variable ~ ., scale = "free_y")+
  labs(title="US Economic time series")

ggplotly(econp, height = 350, width=600)

An extra suggestion I would add is that if you're running this in a R Markdown document / R Notebook, the figure itself may also need some adjustment so your plotly (which is really a htmlwidget ) will not render with a scroller in an undersized window. 我要补充的一个额外建议是,如果你在R Markdown文档/ R笔记本中运行它,图形本身也可能需要一些调整,因此你的情节(实际上是一个htmlwidget )将不会使用尺寸较小的滚动条呈现窗口。 This could be done with the fig.width and fig.height parameters: 这可以通过fig.widthfig.height参数完成:

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R:使用ggplotly或plotly在工具提示上设置货币格式 - R: format currency on tooltip using ggplotly or plotly 使用 selectizeInput 和 plotly::ggplotly 在 R Shiny 中具有交互式 hover 文本 - Using selectizeInput and plotly::ggplotly to have interactive hover text in R Shiny R:如何在 plotly/ggplotly 中控制图例图的字体大小? - R: How to control font size of legend map in plotly/ggplotly? Plotly (ggplotly R) 滚动缩放,label 大小和工具提示选项 - Plotly (ggplotly R) Scroll zoom, label size and tooltip options 在R函数ggplotly(来自plotly包)中,如何调整标签内容 - In R function ggplotly ( from plotly package), how to adjust the label content 如何在R中为ggplotly/plotly悬停文本的一部分着色不同的颜色 - How to colour part of ggplotly/plotly hover text a different colour in R 如何将 add_trace 添加到 R 中从 ggplotly 创建的 plotly object - How to add_trace to a plotly object created from ggplotly in R 默认情况下,如何在 ggplotly 分组时间序列图中隐藏单行? (情节,R,Javascript) - How to a hide single line in ggplotly grouped time series graph by default? (plotly,R,Javascript) R plotly:使用ggplotly()时如何保持ggplot2 geom_bar()的宽度参数 - R plotly: How to keep width parameter of ggplot2 geom_bar() when using ggplotly() R plotly:用ggplotly转换ggplot2时,保留两个图例的外观 - R plotly: preserving appearance of TWO legends when converting ggplot2 with ggplotly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM