gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:55:15 24 4
gpt4 key购买 nike

我注意到当背景为非白色时,sf::geom_sf() 对象周围出现意想不到的白框,这些对象显示在 Shiny 的 UI 中的 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", 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)

maybe works

我将使用 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)

geom issue

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

body { background-color: #E5C595; }

另一个奇怪的事情是那个似乎可以工作的例子仍然有一个你可以看到的白线工件......

maybe works

最佳答案

在您的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

关于css - 使用 renderPlot() 删除 R Shiny ggplot 中 geom_sf() 对象周围的白边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58723151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com