r - 格式化闪亮的 Plotly 子图 - 单个标题和图形大小
问题描述
我正在尝试在plotly
'ssubplot
函数中为我的每个图表提供单独的标题,我找到了一个帖子,您可以在其中扩展子图使用%>% layout(title = "Main Title)
,但我想要每个图表的单独标题(我正在使用ggtitle
但只绘制最后一个标题提供(情节 4)。我发现了一个类似的帖子为每个子情节提供标题 - R Shiny但我认为facet_wrap
在我的场景中我不能。
此外 - 我想知道如何增加子图中图表之间的边距,因为它们似乎真的被压在一起了。
任何帮助表示赞赏!
ui <- fluidPage(
sidebarPanel("This is a sidebar"),
mainPanel(plotlyOutput("myplot"))
)
server <- function(input, output){
output$myplot <- renderPlotly({
gg1 <- ggplotly(
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point() +
theme_minimal() +
ggtitle("Plot 1")
)
gg2 <- ggplotly(
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot() +
theme_minimal() +
ggtitle("Plot 2")
)
gg3 <- ggplotly(
ggplot(iris, aes(x=Petal.Width)) +
geom_histogram() +
ggtitle("Plot 3")
)
gg4 <- ggplotly(
ggplot(iris, aes(x=Petal.Length)) +
geom_histogram() +
ggtitle("Plot 4")
)
subplot(list(gg1,gg2,gg3,gg4), nrows = 2)
})
}
shinyApp(ui = ui, server = server)
解决方案
正如@Edo subplot 标题已经提到的那样,关于 R 的 plotly api是一个未解决的问题。目前我们需要使用注解。这是一个官方的例子。
该subplot
函数为我们提供了一个参数margin
来添加一些空间。
library(shiny)
library(plotly)
ui <- fluidPage(
sidebarPanel("This is a sidebar"),
mainPanel(plotlyOutput("myplot"))
)
server <- function(input, output, session){
output$myplot <- renderPlotly({
gg1 <- ggplotly(
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point() +
theme_minimal()
) %>% add_annotations(
text = "Plot 1",
x = 0,
y = 1,
yref = "paper",
xref = "paper",
xanchor = "left",
yanchor = "top",
yshift = 20,
showarrow = FALSE,
font = list(size = 15)
)
gg2 <- ggplotly(
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot() +
theme_minimal()
) %>% add_annotations(
text = "Plot 2",
x = 0,
y = 1,
yref = "paper",
xref = "paper",
xanchor = "left",
yanchor = "top",
yshift = 20,
showarrow = FALSE,
font = list(size = 15)
)
gg3 <- ggplotly(
ggplot(iris, aes(x=Petal.Width)) +
geom_histogram()
) %>% add_annotations(
text = "Plot 3",
x = 0,
y = 1,
yref = "paper",
xref = "paper",
xanchor = "left",
yanchor = "top",
yshift = 20,
showarrow = FALSE,
font = list(size = 15)
)
gg4 <- ggplotly(
ggplot(iris, aes(x=Petal.Length)) +
geom_histogram()
) %>% add_annotations(
text = "Plot 4",
x = 0,
y = 1,
yref = "paper",
xref = "paper",
xanchor = "left",
yanchor = "top",
yshift = 20,
showarrow = FALSE,
font = list(size = 15)
)
subplot(list(gg1,gg2,gg3,gg4), nrows = 2, margin = 0.06)
})
}
shinyApp(ui = ui, server = server)
顺便说一句,您可以schema()
用来浏览 plotly 的属性。
推荐阅读
- single-sign-on - 在单点登录过程中,用户会话检查应该在 SP 级别还是 IDP 级别进行?
- sql - 查找至少喜欢 2 的人的查询
- symfony - 在 Symfony 4.2 中捆绑安装后如何运行脚本或方法?
- jquery - 如何找到 iOS 应用程序的播放音频路径?
- c# - 尝试为文件“”附加自动命名数据库失败
- android - 使用 altbeacon 停止和重新启动前台服务扫描
- vba - 在 PrintOptions 下添加范围并在变量中确定它
- asp.net - 如何让 dropdownlist_selectedindexchange 先触发
- charts - 在 Google Chart 上显示独立的 x 轴系列
- xamarin - Xamarin 是否将 Forms 应用程序转换为 Java,如果没有,那么它如何在 Android 上运行?