【发布时间】:2020-03-02 12:34:38
【问题描述】:
我注意到当背景为非白色时,在 Shiny 的 UI 中,renderPlot() 中显示的 sf::geom_sf() 对象周围出现了意想不到的白框。
对于 Shiny 中的其他 geom_ 对象,似乎不会出现此问题(但是...请参阅帖子末尾)。
理想情况下,我想弄清楚如何让ggplot2 对象以闪亮的方式显示,以便它们与背景颜色相匹配。
下面的代码似乎可以工作:
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", color = NA),
legend.background = element_rect("#E5C595", color = NA),
legend.box.background = element_rect("#E5C595", color = NA),
#panel.border = element_rect("transparent", color = NA)
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
我将使用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"),
# 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 = 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(),
plot.background = element_rect("#E5C595", color = NA),
panel.background = element_rect("#E5C595", color = NA),
legend.background = element_rect("#E5C595", color = NA),
legend.box.background = element_rect("#E5C595", color = NA),
#panel.border = element_rect("transparent", color = NA)
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
请注意,为了简单起见,我使用了library(shinyWidgets)。如果您在 .css 文件中设置以下内容,则会出现同样的问题:
身体{ 背景颜色:#E5C595; }
另一个奇怪的事情是,似乎可以工作的例子仍然有一个你可以看到的白线伪影......
【问题讨论】:
-
这与这里讨论的问题基本相同:stackoverflow.com/q/23349181/4975218