[英]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. 然后,您可以将
width
和height
参数添加到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.width
和fig.height
参数完成:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.