R + shiny + plotly:ggplotly 将图例向右移动
R + shiny + plotly: ggplotly moves the legend to the right
请查看 post 末尾的 reprex。
本质上,我在 shiny 中创建了一个非常简单的 ggplot2 可视化。
当我在其上调用 ggplotly 以获得情节交互式可视化时,情节顶部的情节图例向右移动。
知道如何解决这个问题吗?
非常感谢!
library(shiny)
library(plotly)
library(ggplot2)
library(dplyr)
df <- tibble(x=seq(20), y=seq(20), type=c(rep("a", 10), rep("b", 10)))
ui <- fluidPage(
mainPanel(
plotOutput("myplot" ),
plotlyOutput("myplot2" )
)
)
server <- function(input, output) {
myplot <- reactive({
gpl1 <- df%>%
ggplot(aes(x = x, y = y, fill=type)) +
geom_col()+
theme(legend.position="top")+
xlab("x")+
ylab("y")+
labs(title = NULL)
gpl1
})
myplot2 <- reactive({
gpl2 <- df%>%
ggplot(aes(x = x, y = y, fill=type)) +
geom_col()+
theme(legend.position="top")+
xlab("x")+
ylab("y")+
labs(title = NULL)
ggplotly(gpl2)
})
output$myplot <- renderPlot({
myplot()
})
output$myplot2 <- renderPlotly({
myplot2()
})
}
shinyApp(ui = ui, server = server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
静态 R Markdown 文档不支持闪亮的应用程序
由 reprex package (v2.0.1)
于 2021-09-23 创建
我们可以添加一些 plotly 语法来修复它。
layout(legend = list(orientation = 'h', x = 0.45, y = 1.1))
library(shiny)
library(plotly)
library(ggplot2)
library(dplyr)
df <- tibble(x=seq(20), y=seq(20), type=c(rep("a", 10), rep("b", 10)))
ui <- fluidPage(
mainPanel(
plotOutput("myplot" ),
plotlyOutput("myplot2" )
)
)
server <- function(input, output) {
myplot <- reactive({
gpl1 <- df%>%
ggplot(aes(x = x, y = y, fill=type)) +
geom_col()+
theme(legend.position="top")+
xlab("x")+
ylab("y")+
labs(title = NULL)
gpl1
})
myplot2 <- reactive({
gpl2 <- df%>%
ggplot(aes(x = x, y = y, fill=type)) +
geom_col()+
theme(legend.position="top")+
xlab("x")+
ylab("y")+
labs(title = NULL)
ggplotly(gpl2) %>%
layout(legend = list(orientation = 'h', x = 0.45, y = 1.1))
})
output$myplot <- renderPlot({
myplot()
})
output$myplot2 <- renderPlotly({
myplot2()
})
}
shinyApp(ui = ui, server = server)
请查看 post 末尾的 reprex。 本质上,我在 shiny 中创建了一个非常简单的 ggplot2 可视化。 当我在其上调用 ggplotly 以获得情节交互式可视化时,情节顶部的情节图例向右移动。 知道如何解决这个问题吗?
非常感谢!
library(shiny)
library(plotly)
library(ggplot2)
library(dplyr)
df <- tibble(x=seq(20), y=seq(20), type=c(rep("a", 10), rep("b", 10)))
ui <- fluidPage(
mainPanel(
plotOutput("myplot" ),
plotlyOutput("myplot2" )
)
)
server <- function(input, output) {
myplot <- reactive({
gpl1 <- df%>%
ggplot(aes(x = x, y = y, fill=type)) +
geom_col()+
theme(legend.position="top")+
xlab("x")+
ylab("y")+
labs(title = NULL)
gpl1
})
myplot2 <- reactive({
gpl2 <- df%>%
ggplot(aes(x = x, y = y, fill=type)) +
geom_col()+
theme(legend.position="top")+
xlab("x")+
ylab("y")+
labs(title = NULL)
ggplotly(gpl2)
})
output$myplot <- renderPlot({
myplot()
})
output$myplot2 <- renderPlotly({
myplot2()
})
}
shinyApp(ui = ui, server = server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
静态 R Markdown 文档不支持闪亮的应用程序
由 reprex package (v2.0.1)
于 2021-09-23 创建我们可以添加一些 plotly 语法来修复它。
layout(legend = list(orientation = 'h', x = 0.45, y = 1.1))
library(shiny)
library(plotly)
library(ggplot2)
library(dplyr)
df <- tibble(x=seq(20), y=seq(20), type=c(rep("a", 10), rep("b", 10)))
ui <- fluidPage(
mainPanel(
plotOutput("myplot" ),
plotlyOutput("myplot2" )
)
)
server <- function(input, output) {
myplot <- reactive({
gpl1 <- df%>%
ggplot(aes(x = x, y = y, fill=type)) +
geom_col()+
theme(legend.position="top")+
xlab("x")+
ylab("y")+
labs(title = NULL)
gpl1
})
myplot2 <- reactive({
gpl2 <- df%>%
ggplot(aes(x = x, y = y, fill=type)) +
geom_col()+
theme(legend.position="top")+
xlab("x")+
ylab("y")+
labs(title = NULL)
ggplotly(gpl2) %>%
layout(legend = list(orientation = 'h', x = 0.45, y = 1.1))
})
output$myplot <- renderPlot({
myplot()
})
output$myplot2 <- renderPlotly({
myplot2()
})
}
shinyApp(ui = ui, server = server)