【问题标题】:how to use fill and color geom_col vs geom_point如何使用填充和颜色 geom_col vs geom_point
【发布时间】:2021-02-20 22:19:42
【问题描述】:

我有一个关于 geom_point 的问题。

ui <- fluidPage(
  titlePanel("Test"),
  sidebarLayout(sidebarPanel(
    selectInput(
      "selectedColX",
      "Select colum for X axis",
      choices = c("tooling1","tooling2"),
      selected = "tooling1"
    ),
    
    selectInput(
      "selectedfill",
      "Energie X is vulling",
      choices = c("tooling1Energie","tooling2Energie"),
      selected = "tooling1Energie"
    ) ,
    
    
  selectInput(
    "selectedColY",
    "Select colum for Y axis",
    choices = c("subject1","subject2"),
    selected = "subject1"
  ) , 
  
  selectInput(
    "selectedcolor",
    "Energie Y is omlijning",
    choices = c("subject1Energie","subject2Energie"),
    selected = "subject1Energie"
  ) , 
  

  ),
  mainPanel(plotOutput("distPlot")))
)



server <- function(input, output) {
  output$distPlot <- renderPlot({
    x_axis <- input$selectedColX
    y_axis <- input$selectedColY
    fill <- input$selectedfill
    color <- input$selectedcolor
    
    gg <-
      ggplot(KEK, aes_string(x = x_axis, y = y_axis, fill = fill, col = color))
    gg <- gg + geom_col(size = 1.5) +
      facet_wrap(~anoniem)
    gg
  })
}

当我使用此代码时,我可以深入了解一些,但使用 geom_col,不可能添加形状。所以我想我把它改成geom_point,但是如果我这样做并添加形状并保持填充和颜色相同,那么填充会变成黑色(AccesEnergie)。见添加的图片。为什么会这样,有人可以帮帮我吗?

【问题讨论】:

  • 如果您创建一个小的可重现示例以及预期的输出,这将更容易提供帮助。阅读how to give a reproducible example
  • 谢谢,此时我希望人们有足够的信息来帮助我。部分数据是机密的。
  • 请注意,您可以发布虚拟数据。例如,可以使用 apple、orange 等代替实名。此外,变量名可以修改为 var1、var2 等。

标签: r shiny geom-col geom-point


【解决方案1】:

您需要用guide_legend 覆盖fill 颜色,如下所示

gg <- ggplot(KEK, aes_string(x = x_axis, y = y_axis, fill = fill, col = color)) + 
  geom_point() +
  guides(fill = guide_legend(override.aes=list(color=color) ) 

编辑

您可以使用下面的示例 MRE 进行测试。

fill <- c("blue") # input$selectedfill
mycolor <-  c("green")

gg <- ggplot(mtcars, aes(x = carb, y = mpg, fill = fill, col = mycolor)) + geom_point() +
 scale_color_manual(values=mycolor) +
  guides(fill = guide_legend(override.aes=list(color=mycolor) ) #,
         #color= 'none' ## do not display color legend
         )   
gg

【讨论】:

  • 感谢您的回复,但是当我使用此代码 guides(fill = guide_legend(override.aes=list(color=color)) 时,我收到以下错误警告:错误:未知颜色名称:1. Energie
  • 抱歉,我假设color &lt;- input$selectedcolor 定义了您自己的颜色。否则,请发帖MRE
  • 请发布来自KEK 的一些示例数据。您可以使用dput(KEK[1:10,]),然后将该输出在控制台中发布到您的问题中。
猜你喜欢
  • 2018-02-15
  • 1970-01-01
  • 2022-01-02
  • 2013-04-04
  • 2021-09-14
  • 1970-01-01
  • 2020-07-24
  • 2021-11-09
相关资源
最近更新 更多