-1

我有一个关于 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)。见添加的图片。为什么会发生这种情况,有人可以帮我吗?

geom_col

几何点

4

1 回答 1

0

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

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
于 2021-02-20T22:46:42.647 回答