绘图轴标签重叠且太大
Plotly axis labels overlapping and being too big
我正在与 R
和 shiny
以及 ggplot2
一起使用 plotly。输出刻度标签太大的图(如下图)时,轴标签与刻度标签重叠,如图:
#UI.R
library(shiny)
library(plotly)
library(ggplot2)
shinyUI(fluidPage(
plotlyOutput("Plot")
))
#Server.R
shinyServer(function(input, output) {
output$Plot <- renderPlotly({
plot <- ggplot(diamonds, aes(x=cut)) +
geom_bar()+
theme(axis.text.x = element_text(angle = 90, size = 25))
ggplotly(plot)
})
})
输出:
您可以看到较大的刻度线与标签重叠(在本例中 "cut")。虽然我通过放大字体来制作这个例子,但同样的事情也会发生在长标签上。此外,有时整个标签不可见(即它变为 'out of frame'),就像这样(取自不同的项目):
我想让它的表现更像 ggplot2,输出:
有谁知道这样做的方法吗?我找不到很多关于如何更改来自 ggplotly 的内容的好文档。
使用plotly_build()
调整页边距。来自文档:
Using this function can be useful for overriding defaults provided by
ggplotly/plot_ly or for debugging rendering errors.
pb <- plotly_build(plot)
str(pb)
pb$layout$margin$b <- 220
pb
我正在与 R
和 shiny
以及 ggplot2
一起使用 plotly。输出刻度标签太大的图(如下图)时,轴标签与刻度标签重叠,如图:
#UI.R
library(shiny)
library(plotly)
library(ggplot2)
shinyUI(fluidPage(
plotlyOutput("Plot")
))
#Server.R
shinyServer(function(input, output) {
output$Plot <- renderPlotly({
plot <- ggplot(diamonds, aes(x=cut)) +
geom_bar()+
theme(axis.text.x = element_text(angle = 90, size = 25))
ggplotly(plot)
})
})
输出:
您可以看到较大的刻度线与标签重叠(在本例中 "cut")。虽然我通过放大字体来制作这个例子,但同样的事情也会发生在长标签上。此外,有时整个标签不可见(即它变为 'out of frame'),就像这样(取自不同的项目):
我想让它的表现更像 ggplot2,输出:
有谁知道这样做的方法吗?我找不到很多关于如何更改来自 ggplotly 的内容的好文档。
使用plotly_build()
调整页边距。来自文档:
Using this function can be useful for overriding defaults provided by ggplotly/plot_ly or for debugging rendering errors.
pb <- plotly_build(plot)
str(pb)
pb$layout$margin$b <- 220
pb