[英]How to change legend position in ggplotly in R
下面的代碼使用ggplot
和ggplotly
生成兩個圖。 盡管使用layout()
來 ggplotly,但圖例仍然在右側。 圖例必須位於底部。 任何人都可以幫助將圖例移到 ggplotly 的底部嗎? 我已經在R + Shiny + plotly嘗試了解決方案:ggplotly 將圖例向右移動,但在這里不起作用。 如果我錯過了顯而易見的事情,有人可以幫忙嗎?
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)
好的,這不是一個完整的答案,但它是一個開始,可能會幫助其他人弄清楚,而且評論太長了。 有可能值得提出一個問題,盡管我不確定問題出在哪里(即哪個包)。
我重新運行了R + Shiny + plotly 中的代碼:ggplotly 將圖例向右移動,它按預期工作,但由於某種原因你的沒有。 唯一的區別是您的fill
是連續的,而那個填充是分類的。
如果您在此處添加分類組以獲得分數:
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,score.f=as.factor(score))
然后您擁有的代碼有效。 您需要使用fill=score.f
(而不是fill=score
)修改gpl2
對象,並且還需要更改布局中的 y 值以使圖例到達底部:
ggplotly(gpl2) %>%
layout(legend = list(orientation = 'h', x = 0.45, y = -.07))
然而,一旦您將填充組更改為連續
score<-c(500, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
data<-data.frame(measure,score,score.f=score)
傳說又回到了右邊:
這里的問題是,您正在處理顏色條而不是圖例。
目前 plotly不提供水平顏色條。
在 ggplotly 上下文中,更改顏色條的位置似乎也有問題:
library(shiny)
library(plotly)
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({
fig <- ggplotly(myplot())
fig <- style(fig, marker = list(colorbar = list(xanchor = "center", yanchor = "center", x = 0.5, y = -1, title = "test")), traces = NULL) # try traces = 1 or 1:2
# browser()
# plotly_json(fig)
})
output$myplot <- renderPlot({
myplot()
})
output$myplot2 <- renderPlotly({
myplot2()
})
}
shinyApp(ui = ui, server = server)
因此,目前我建議不要修改顏色欄並使用默認值。
正如@ismirsehregal 提到的,plotly 尚不支持水平顏色條,並且存在顏色條以支持連續值填充屬性。 因此,在 plotly 支持該方向之前,可能會暫時使用替代解決方案:將填充從連續值更改為離散值。 這是使用(a)離散值的切割和(b)底部圖例的繪圖錨點的反應。
myplot2 <- reactive({
gpl2 <- ggplot(data,aes(y=reorder(measure, score),
x=score,
fill=cut(score, breaks=seq(0,800,100)))) +
geom_bar(stat="identity") +
labs(x='x',y='y',title=NULL,fill='Score') +
theme(legend.position = 'bottom') +
# scale_fill_stepsn(colours = terrain.colors(10),n.breaks=10)
scale_fill_viridis_d()
ggplotly(gpl2) %>%
plotly::layout(legend=list(x=0,
xanchor='left',
yanchor='bottom',
orientation='h'))
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.