我尝试为我的目的重新安装this闪亮的应用。由于我想在其上面添加一些文本,我使用Rmd-Format。以下代码与链接完全相同,只是我删除了server-和ui-functions,因为否则你看不到所有的输出。 (更新:我还抛出了click,zoom和brush-events,以使代码更具可读性)
```{r cars, echo=FALSE}
library(plotly)
library(shiny)
fluidPage(
radioButtons("plotType", "Plot Type:", choices = c("ggplotly", "plotly")),
plotlyOutput("plot", height = "700px"),
verbatimTextOutput("hover")
)
output$plot <- renderPlotly({
# use the key aesthetic/argument to help uniquely identify selected observations
key <- row.names(mtcars)
if (identical(input$plotType, "ggplotly")) {
p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) +
geom_point()
ggplotly(p) %>% layout(dragmode = "select")
} else {
plot_ly(mtcars, x = ~mpg, y = ~wt, key = ~key) %>%
layout(dragmode = "select")
}
})
output$hover <- renderPrint({
d <- event_data("plotly_hover")
if (is.null(d)) "Hover events appear here (unhover to clear)" else d
})
```
我将height=700px
添加到plotlyOutput
,因为我的ggplot图中有一个更大的数据集和一个长图例。然而,当情节图调整时 - ggplot-graph不会大于~500px。
根据this thread,这是ggplotly
的问题。问题是我需要在height=700px
中设置plotlyOutput
以阻止ggplotly
- 图形过度绘制文本输出。 我要搜索的是:
height=700px
中设置plotlyOutput
,或者ggplotly
- 图形为了实现后者,我尝试将布局函数的高度添加到ggplotly
。我甚至在相应的layout
中set autosize=FALSE
。我也试过specify the height in an extra renderUI
-function。
答案 0 :(得分:1)
您可以在运行ggplotly后更改plotly属性。不确定你是否意味着调整布局,但如果是这样,那么我无法复制你的问题,因为它超过500绝对罚款。
例如,在这种情况下:
pt<-ggplotly(p) %>% layout(dragmode = "select")
# have a look to all attributes
str(pt)
#Specifically height is
pt$height<-700
你可以测试差异:绘图为500,ggplotly为700(不要在plotlyOutput
本身设置高度)
output$plot <- renderPlotly({
# use the key aesthetic/argument to help uniquely identify selected observations
key <- row.names(mtcars)
if (identical(input$plotType, "ggplotly")) {
p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) +
geom_point()
pt<- ggplotly(p) %>% layout(dragmode = "select")
pt$height<-700
pt
# ggplotly(p) %>% layout(dragmode = "select")
} else {
plot_ly(mtcars, x = ~mpg, y = ~wt, key = ~key) %>%
layout(dragmode = "select",height = 500)
}
})
编辑:ggplotly和plotly具有相同的高度,并且不与文本输出重叠,前提是ui中的高度大于ggplotly布局参数中的高度:
output$plot <- renderPlotly({
key <- row.names(mtcars)
if (identical(input$plotType, "ggplotly")) {
p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) +
geom_point()
ggplotly(p) %>% layout(dragmode = "select", height=800)
} else {
plot_ly(mtcars, x = ~mpg, y = ~wt, key = ~key) %>%
layout(dragmode = "select")
}
})
plotlyOutput("plot", height = 800)