cha*_*has 5 r ggplot2 ggplotly r-plotly
下面的代码使用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)
Run Code Online (Sandbox Code Playgroud)
好吧,这不是一个完整的答案,但它是一个可能帮助其他人查明问题的开始,而且它太长,无法放入评论。尽管我不确定问题出在哪里(即在哪个包内),但可能值得提出一个问题。
我从 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))
Run Code Online (Sandbox Code Playgroud)
然后你的代码就可以工作了。您需要使用(而不是)修改gpl2
对象,并且还需要更改布局中的 y 值以使图例位于底部:fill=score.f
fill=score
ggplotly(gpl2) %>%
layout(legend = list(orientation = 'h', x = 0.45, y = -.07))
Run Code Online (Sandbox Code Playgroud)
然而,一旦您将填充组更改为连续
score<-c(500, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
data<-data.frame(measure,score,score.f=score)
Run Code Online (Sandbox Code Playgroud)
传说回到右边:
归档时间: |
|
查看次数: |
3637 次 |
最近记录: |