The below code generates two plots using ggplot
and ggplotly
. Despite of using the layout()
to ggplotly the legend is still at the right side. The legend is required to be at the bottom. Could anyone help to move the legend to bottom in the ggplotly? I have tried the solution at R + shiny + plotly: ggplotly moves the legend to the right and is not working here. Can someone help if im missing the obvious.
measure<-c("MSAT","MSAT","GPA","MSAT","MSAT","GPA","GPA","GPA")
score<-c(500, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
data<-data.frame(measure,score)
ui <- fluidPage(
mainPanel(
plotOutput("myplot" ),
plotlyOutput("myplot2" )
)
)
server <- function(input, output) {
myplot <- reactive({
gpl1 <- ggplot(data,aes(y=reorder(measure, score),x=score,fill=score)) +
geom_bar(stat="identity")+
theme(legend.position="bottom")+
xlab("x")+
ylab("y")+
labs(title = NULL)
gpl1
})
myplot2 <- reactive({
gpl2 <- ggplot(data,aes(y=reorder(measure, score),x=score,fill=score)) +
geom_bar(stat="identity") +
theme(legend.position="bottom")+
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)