【问题标题】:Assigning alpha values to ggplot将 alpha 值分配给 ggplot
【发布时间】:2017-08-01 04:51:55
【问题描述】:

我有以下代码,它的设置使得任何分配 $time“未来”的行的 alpha 为 0.6,任何分配 $time“过去”的行的 alpha 为 1。这允许我的 geom_bar 中的值在我的“未来”数据上显示为略微透明,在我的“过去”数据上显示为完全可靠。

但是我的问题是,当我的 input$date_range 介于过去的两个日期之间时,我所有的 geom_bars 现在的 alpha 为 0.6(代码没有将特定的 alpha 值分配给特定的 $time 值,这就是我想要)。

我尝试创建一个新的 $alpha 列,其中包含要用作 alpha 值的特定整数,但它只是让我的“未来”数据变得非常不透明,我不知道为什么......

allocated <- Project       Date      value       time
               A         2017-05-15    4         past
               B         2017-06-18    8         past
               C         2017-07-25    3         past
               D         2017-08-20    9         future
               E         2017-09-14    4         future



ui <- dashboardPage(
 dashboardSidebar(
 sidebarMenu(

  menuItem(
    dateRangeInput('date_range', label = "Date Range",format = "mm/dd/yyyy", start = Sys.Date()-17, end = Sys.Date()+17, startview = "month", weekstart = 0, separator = " to ", width = 200)
  )
)
),

fluidRow(
  box(plotOutput("plot1", width = 1000, height = 500))
)
)


server <- function(input, output) {

  output$plot1 <- renderPlot({

date_data <- reactive({
  subset(allocated, variable >= input$date_range[1] & variable <= input$date_range[2], value != 0)
})



   ggplot(data = date_data(), aes(x = variable, y = value, alpha = time, fill = Project)) +
  geom_bar(stat = 'identity') +
  scale_alpha_discrete(range = c(0.6, 1), guide = 'none')
  })
}


shinyApp(ui, server)

【问题讨论】:

  • 尝试一个 minimal 可重现的例子。听起来问题与 Shiny 没有任何关系,所以摆脱所有 Shiny 代码。然后分享一些数据。 See here for tips on data sharing (either use built-in data, share code to simulate data, or use dput())。 “非常不透明”是什么意思?正常,不透明?
  • @Gregor 感谢您的评论,因为您可能会说我是该网站的新手。我用我的数据框的简写更新了我的内容。然而,我保留了闪亮的代码以保持上下文。这更有帮助吗?至于“极端不透明”,我的意思是条形图几乎完全透明,远低于我试图将它们保持在的 0.6 设置......
  • 这是一个改进。如果您的数据代码是可复制/可粘贴的,那就太好了。上面的链接和我的评论为此建议dputdput(head(allocated)) 是共享复制/粘贴数据的好方法。 (或head(allocated) 以外的其他子集,足以说明问题)
  • 自言自语,如果您可以将问题隔离到ggplot,我可能会尝试解决它 - 我很擅长 ggplot,尝试帮助您似乎又快又容易。如果你把它留在 Shiny 中,它看起来会更复杂——我已经有一年多没有使用 Shiny 并且它没有安装在我的笔记本电脑上——所以我不会费心去解决它。也许其他人会,但你将潜在的回答者限制在那些同时擅长 ggplot 和 Shiny 的人中。
  • @Gregor 非常感谢您的建议。在最终弄清楚如何解决我的问题后,我发布了一个答案,但我一定会确保我遵循你所说的,因为我会继续为这个论坛做出贡献。作为一名新开发人员,我正在努力学习如何与他人进行适当的协作,因此我非常感谢您的帮助,并且您花时间考虑我的问题 :)

标签: r dataframe ggplot2 shiny alpha


【解决方案1】:

对不起,我终于明白了。添加一个分配了 alpha 数字的分配 $alpha 列,然后将 scale_alpha_identity() 添加到我的 ggplot 最终让我到达了我想要的位置!

allocated <- Project       Date      value       time      alpha
               A         2017-05-15    4         past       1
               B         2017-06-18    8         past       1
               C         2017-07-25    3         past       1
               D         2017-08-20    9         future     0.6
               E         2017-09-14    4         future     0.6



ui <- dashboardPage(
 dashboardSidebar(
 sidebarMenu(

  menuItem(
    dateRangeInput('date_range', label = "Date Range",format = "mm/dd/yyyy", start = Sys.Date()-17, end = Sys.Date()+17, startview = "month", weekstart = 0, separator = " to ", width = 200)
 )
)
),

fluidRow(
  box(plotOutput("plot1", width = 1000, height = 500))
)
)


server <- function(input, output) {

  output$plot1 <- renderPlot({

date_data <- reactive({
  subset(allocated, variable >= input$date_range[1] & variable <= 
input$date_range[2], value != 0)
})



   ggplot(data = date_data(), aes(x = variable, y = value, alpha = alpha, fill = Project)) +
  geom_bar(stat = 'identity') +
  scale_alpha_identity()
  })
}


shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 2017-02-09
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 2021-12-17
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多