微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用填充和颜色 geom_col 与 geom_point

如何解决如何使用填充和颜色 geom_col 与 geom_point

我有一个关于 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

geom_point

解决方法

您需要使用 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,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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。