使用renderPlot()删除R Shiny ggplot中geom_sf()对象周围的空白边距

当背景为非白色时,我从Shiny的UI中注意到sf::geom_sf()中显示的renderPlot()对象周围有意外的白框。

对于Shiny中的其他geom_对象,不会出现似乎问题(但...请参阅文章结尾)。

理想情况下,我想弄清楚如何使ggplot2对象以Shiny的方式显示,以便它们与背景色匹配。

这里是link to a short GIF on twitter showing the discrepancy I noticed between regular ggplot2() objects and geom_sf() objects on my data

下面的代码似乎起作用

library(shiny)
library(shinyWidgets)
library(ggplot2)


# Define UI for application that draws a histogram
ui <- fluidPage(#setBackgroundColor("#E5C595"),# Application title
        titlePanel("Old Faithful Geyser Data"),# Sidebar with a slider input for number of bins 
        sidebarLayout(
                sidebarPanel(
                        sliderInput("bins","Number of bins:",min = 1,max = 50,value = 30)
                ),# Show a plot of the generated distribution
                mainPanel(
                        plotOutput("distPlot")
                )
        )
)

# Define server logic required to draw a histogram
server <- function(input,output) {

        output$distPlot <- renderPlot({
                # generate bins based on input$bins from ui.R
                data    <- as.data.frame(faithful[,2])
                colnames(data) <- "value"
                bins <- seq(min(data),max(data),length.out = input$bins + 1)

                ggplot(data,aes(x=value)) +
                        geom_histogram() +
                        theme(
                                axis.line = element_blank(),axis.text.x = element_blank(),axis.text.y = element_blank(),axis.ticks = element_blank(),axis.title.x = element_blank(),axis.title.y = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),plot.background = element_rect("#E5C595",color = NA),panel.background = element_rect("#E5C595",legend.background = element_rect("#E5C595",legend.box.background = element_rect("#E5C595",#panel.border = element_rect("transparent",color = NA)
                        )
        })
}

# Run the application 
shinyApp(ui = ui,server = server)

使用renderPlot()删除R Shiny ggplot中geom_sf()对象周围的空白边距

我将使用r-spatial.com中的一个简单示例来说明geom_sf()对象的问题。

library(shiny)
library(shinyWidgets)
library(ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(rgeos)

world <- ne_countries(scale = "medium",returnclass = "sf")
class(world)

# Define UI for application that draws a histogram
ui <- fluidPage(#setBackgroundColor("#E5C595"),# use shinywidgets to set background color
        includeCSS("style.css"),length.out = input$bins + 1)

                ggplot(data = world) +
                        geom_sf() +
                        theme(
                                axis.line = element_blank(),server = server)

使用renderPlot()删除R Shiny ggplot中geom_sf()对象周围的空白边距

请注意,为简单起见,我使用了library(shinyWidgets)。如果在.css文件中设置以下内容,则会发生相同的问题:

  

body {     背景颜色:#E5C595;   }

另一个奇怪的事情是,似乎正常工作的例子中仍然有白线伪像,您可以看到...

使用renderPlot()删除R Shiny ggplot中geom_sf()对象周围的空白边距

msch0628bb 回答:使用renderPlot()删除R Shiny ggplot中geom_sf()对象周围的空白边距

您可以在renderPlot中添加参数bg来设置背景色。为了避免在调整大小(Check this question)时出现问题,还应该添加参数execOnResize=TRUE

server <- function(input,output) {
  output$distPlot <- renderPlot({
    # generate bins based on input$bins from ui.R
    data    <- as.data.frame(faithful[,2])
    colnames(data) <- "value"
    bins <- seq(min(data),max(data),length.out = input$bins + 1)

    ggplot(data = world) +
      geom_sf() +
      theme(
        axis.line = element_blank(),axis.text.x = element_blank(),axis.text.y = element_blank(),axis.ticks = element_blank(),axis.title.x = element_blank(),axis.title.y = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.border = element_blank(),plot.background = element_blank(),panel.background = element_blank(),)
  },bg="#E5C595",execOnResize=T)
}

# Run the application 
shinyApp(ui = ui,server = server)

enter image description here

本文链接:https://www.f2er.com/3154966.html

大家都在问