图例改变闪亮的 ggplot 图的大小
Legend changing the size of ggplot figure in shiny
我在 Shiny 中有一个 ggplot,它使用 geom_point 来绘制一些数据。我对其进行了设置,以便在选中复选框时添加一种美学,将数据着色为两个单独的变量。这也创造了一个传奇。我的问题是,当这个图例出现时,它 'takes' space 从情节和情节变得略小。有没有一种方法可以修复绘图的大小,以便在不改变绘图大小的情况下显示图例?
ui <- fluidPage(
titlePanel("Transfers Analysis App"),
sidebarLayout(
sidebarPanel(
checkboxInput("Outage", "Show Outages", FALSE)
),
mainPanel(
plotOutput("plot1", height = "600px", width = "100%", hover = hoverOpts(id = "plot_hover")),
verbatimTextOutput("hover_info")
)
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
Outage <- input$Outage
g <- ggplot(data, aes(Date, NUMBER_OF_TRANSFERS)) + geom_point()
if (Outage == TRUE)
g <- g + geom_point(aes(color = Outage)) + scale_colour_manual(breaks = c("Outage", "No Outage", "Day After an Outage", "Both"), name= "Legend", values=c( "black", "red", "blue")) + theme(legend.position="bottom")
plot(g)
})
}
shinyApp(ui, server)
注意:我的实际代码具有比这更多的功能,为了简单起见,我删除了这些功能。
也许有人有更好的主意,但这里是一个建议。您只能绘制同一图形的图例。您没有提供数据集,所以我以 iris 数据集为例。如果您单击中断,它将在第一个图表的底部生成一个图例。如果取消单击,它将产生一个您看不到的空白图。如您所见,图例不会改变您的第一个图表的大小。
使用此 post (How to plot just the legends in ggplot2?),您可以:
#function to extract the legend
g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}
ui <- fluidPage(
titlePanel("Transfers Analysis App"),
sidebarLayout(
sidebarPanel(
checkboxInput("Outage", "Show Outages", FALSE)
),
mainPanel(
plotOutput("plot1", height = "600px", width = "100%", hover = hoverOpts(id = "plot_hover")),
plotOutput("plot2"),
verbatimTextOutput("hover_info")
)
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
Outage <- input$Outage
g <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point()
if (Outage == TRUE)
g <- g + geom_point(aes(color = Species)) + scale_colour_manual(breaks = c("setosa", "virginica", "versicolor"), values=c( "black", "red", "blue")) +
theme(legend.position="none")
plot(g)
})
output$plot2 <- renderPlot({
Outage <- input$Outage
if (Outage == TRUE) {
g <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point()
g <- g + geom_point(aes(color = Species)) + scale_colour_manual(breaks = c("setosa", "virginica", "versicolor"), name= "Legend", values=c( "black", "red", "blue")) +
theme(legend.position="bottom") +
theme(legend.text=element_text(size=15)) # you can change the size of the legend
legend <- g_legend(g)
grid.draw(legend)
} else {
g <- ggplot() + theme_bw(base_size=0) +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
plot(g)
}
})
}
shinyApp(ui, server)
我在 Shiny 中有一个 ggplot,它使用 geom_point 来绘制一些数据。我对其进行了设置,以便在选中复选框时添加一种美学,将数据着色为两个单独的变量。这也创造了一个传奇。我的问题是,当这个图例出现时,它 'takes' space 从情节和情节变得略小。有没有一种方法可以修复绘图的大小,以便在不改变绘图大小的情况下显示图例?
ui <- fluidPage(
titlePanel("Transfers Analysis App"),
sidebarLayout(
sidebarPanel(
checkboxInput("Outage", "Show Outages", FALSE)
),
mainPanel(
plotOutput("plot1", height = "600px", width = "100%", hover = hoverOpts(id = "plot_hover")),
verbatimTextOutput("hover_info")
)
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
Outage <- input$Outage
g <- ggplot(data, aes(Date, NUMBER_OF_TRANSFERS)) + geom_point()
if (Outage == TRUE)
g <- g + geom_point(aes(color = Outage)) + scale_colour_manual(breaks = c("Outage", "No Outage", "Day After an Outage", "Both"), name= "Legend", values=c( "black", "red", "blue")) + theme(legend.position="bottom")
plot(g)
})
}
shinyApp(ui, server)
注意:我的实际代码具有比这更多的功能,为了简单起见,我删除了这些功能。
也许有人有更好的主意,但这里是一个建议。您只能绘制同一图形的图例。您没有提供数据集,所以我以 iris 数据集为例。如果您单击中断,它将在第一个图表的底部生成一个图例。如果取消单击,它将产生一个您看不到的空白图。如您所见,图例不会改变您的第一个图表的大小。
使用此 post (How to plot just the legends in ggplot2?),您可以:
#function to extract the legend
g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}
ui <- fluidPage(
titlePanel("Transfers Analysis App"),
sidebarLayout(
sidebarPanel(
checkboxInput("Outage", "Show Outages", FALSE)
),
mainPanel(
plotOutput("plot1", height = "600px", width = "100%", hover = hoverOpts(id = "plot_hover")),
plotOutput("plot2"),
verbatimTextOutput("hover_info")
)
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
Outage <- input$Outage
g <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point()
if (Outage == TRUE)
g <- g + geom_point(aes(color = Species)) + scale_colour_manual(breaks = c("setosa", "virginica", "versicolor"), values=c( "black", "red", "blue")) +
theme(legend.position="none")
plot(g)
})
output$plot2 <- renderPlot({
Outage <- input$Outage
if (Outage == TRUE) {
g <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point()
g <- g + geom_point(aes(color = Species)) + scale_colour_manual(breaks = c("setosa", "virginica", "versicolor"), name= "Legend", values=c( "black", "red", "blue")) +
theme(legend.position="bottom") +
theme(legend.text=element_text(size=15)) # you can change the size of the legend
legend <- g_legend(g)
grid.draw(legend)
} else {
g <- ggplot() + theme_bw(base_size=0) +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
plot(g)
}
})
}
shinyApp(ui, server)