R ggplotly 调整大小 [英] Resize plotly R ggplotly
问题描述
我在使用 plotly R 包时遇到了一些问题.我对 plotly 很陌生,但我喜欢我可以使用类似 ggplot 的语法,所以我正在努力让它发挥作用.
我创建了一个多面图,您可以将鼠标悬停在数据点上并查看有关该记录的详细信息.我对这个情节很满意,但我想调整它的大小,这样每个情节的 y 轴就不会那么短,因为我想调整整个情节的高度和宽度.
事实上,我不知道如何覆盖默认大小,我正在拔头发,因为我能找到的所有示例都使用 plot_ly() 而不是 ggplotly().除非我需要,否则我宁愿不重建情节只是为了调整大小.
我目前运行的代码真的很简单:
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(~ 区域,ncol = 1)(gg_plot <- ggplotly(绘图))
您可以在这里确切地看到我正在使用的内容:
I'm having some trouble using the plotly R package. I'm very new to plotly but I loved that I could use ggplot-like syntax so I'm trying to make it work.
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.
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(). 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
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? It still preserves the ggplot2
syntax but uses plotly_build()
instead of 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:
这篇关于R ggplotly 调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!