【问题标题】:Resolution of text in Shiny (R)Shiny (R) 中的文本分辨率
【发布时间】:2021-03-18 22:13:39
【问题描述】:

未解决的问题: 有很多关于这个的帖子没有解决方案。见R Shiny plot transparent background: low resolution (white pixels around axis text and labels, and title)和low resolution in R ggplot2 plots with transparent background on Shiny app

问题: 当有人为闪亮的绘图应用透明背景颜色时,就会出现问题。这是在 renderPlot 函数中使用 bg = "transparent" 完成的。 任何关于如何解决这个问题的想法都非常感谢!

这是透明背景下的样子(只需查看该文本的分辨率):

这是没有透明背景的样子(非常完美!):

请参见此处查看可重现的示例:

library(shiny)
library(shinythemes)
library(readr)
library(tidyverse)

dat_in <- read_table2("term_year_fct   mean    value   Program name    subgroup_fct    Year
2017 7.647482014 139 STEMLearningCenter  AllUsers    AllUsers    1
2018   6.34741784  213 STEMLearningCenter  AllUsers    AllUsers    2
Summer&Fall2018 7.166666667 246 STEMLearningCenter  AllUsers    AllUsers    3
2019   7.759036145 249 STEMLearningCenter  AllUsers    AllUsers    4
Summer&Fall2019 11.97986577 149 STEMLearningCenter  AllUsers    AllUsers    5
2017 8.769230769 104 STEMLearningCenter  STEMUsers   STEMUsers   1
2018   8.563380282 142 STEMLearningCenter  STEMUsers   STEMUsers   2
Summer&Fall2018 9.51497006  167 STEMLearningCenter  STEMUsers   STEMUsers   3
2019   9.675824176 182 STEMLearningCenter  STEMUsers   STEMUsers   4
Summer&Fall2019 12.44680851 141 STEMLearningCenter  STEMUsers   STEMUsers   5
")



ui <- fluidPage(theme = shinytheme("superhero"),  # shinythemes::themeSelector(), #
                
                headerPanel("Test App"),
                sidebarLayout(
                  sidebarPanel(
                    uiOutput("choose_prog"),
                    
                    uiOutput("choose_name"),
                    br(),
                  ),
                  
                  mainPanel(
                    plotOutput("plot")
                  )
                )
                
)


server <- function(input, output) {
  # Drop down selection to chose the program 
  output$choose_prog <- renderUI({
    selectInput("program", "STEM Program", unique(dat_in$Program))
  })
  
  output$choose_name <- renderUI({

    
    # Get the program with the appropriate names
    req(input$program)
    dat <- subset(dat_in, Program == input$program)
    name_options <- unique(dat$name)
    
    # Create the check boxes & select them all by default 
    checkboxGroupInput("names", "select category of interest", 
                       choices = name_options,
                       selected = name_options)
    
  })

  
  output$plot <- renderPlot({
    # Output the plot
    req(input$program,input$names)
    dat <-subset(dat_in, Program %in% input$program & name %in% input$names)
    
    # head(dat)
    # head(dat)
    dat %>% 
      ggplot(aes(x=Year,y = value,fill = subgroup_fct)) + 
      geom_area(aes(colour = subgroup_fct, fill = subgroup_fct), color = "white", position = "identity") +
      ggtitle(unique(dat$Program))+ 
      scale_fill_manual(values = c("#285560", "#57a6b9", "#8fa13a")) +
      scale_y_continuous(limits = c(0, 300), expand = c(0, 20)) +
      scale_x_continuous(limits = c(1, 5), breaks = c(1:5),
                         labels = str_wrap(unique(dat$term_year_fct),
                                           width = 15)) + 
      xlab("") + 
      ylab("Count of \nUnique Users\n") + 
      
      geom_text(aes(label = ifelse(name == "All Users", value, "")), 
                hjust = 0.5, vjust = -1, show.legend = FALSE, color = "white")  +
      geom_text(aes(label = ifelse(name == "STEM Users", value, "")), 
                hjust = 0.5, vjust = 2, show.legend = FALSE) +
      geom_text(aes(label = ifelse(name == "Frequent Users", value, "")), 
                hjust = 0.5, vjust = 2, show.legend = FALSE) + 
      theme(
        # panel.background = element_blank(),  # Remove gray panel background
        panel.background = element_rect(fill = "transparent", colour = NA),
        plot.background = element_rect(fill = "transparent", colour = NA),
        text = element_text(size = 12, family = "Calibri"),  # Specify text size
        plot.title = element_text(hjust = 0.5,size = 20, vjust = 2.5, face = "bold", color ="white"),  # Center the title
        plot.subtitle = element_text(hjust = 0.5, size = 18, color ="white"),  # Center text
        axis.title.y = element_text(angle = 0, vjust = 0.5, margin = margin(r = 10), size = 15, face = "bold", color ="white"), 
        axis.ticks = element_blank(),  # Remove ticks
        axis.text.x = element_text(size = 15, hjust = 0.5, family = "Calibri", face = "bold", color ="white"),
        axis.text.y = element_text(size = 15, hjust = 0.5, family = "Calibri", face = "bold", color ="white"),# Set labels at 45 degree angle
        legend.title = element_blank(),  # Remove legend title
        legend.key = element_rect(fill = NA),
        legend.text=element_text(size=14),
        legend.position = "bottom",
        plot.margin=unit(c(1,1,1.5,1.2),"cm")) # Remove gray fill for legend keys
    
    
  }, bg = "transparent")
    
    
}  

shinyApp(ui = ui, server = server)

【问题讨论】:

  • 对我来说很好用
  • @AdamK 你没看到文本的分辨率是如何关闭的吗?情节中的文字不清楚
  • 运行您的代码时看起来不错:ibb.co/hHqH7jM
  • @AdamK 这太奇怪了。我想知道为什么它对你来说看起来不同......

标签: r shiny shiny-reactivity


【解决方案1】:

终于弄明白了:补充:

 type = "cairo"

到 renderPlot() 调用。

像这样:

renderPlot({},bg = "transparent", type = "cairo")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    相关资源
    最近更新 更多