-1

I have a question regarding 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
  })
}

When I'm using this code I can great some insight, but with geom_col, it's impossible to add shape. So i thought i change it to geom_point,but if i do this and add shape and keep fill and col the same then the fill is turning black (AccesEnergie). see pictures added. Why this happens, can someone help me out please?

geom_col

geom_point

  • 1
    It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Feb 21 '21 at 01:51
  • Thank you, at this moment I hoped people had enough information to help me out. Parts of the data are confidential. – Steffie Szegedi Feb 21 '21 at 21:57
  • Please note that you can post dummy data. For example, instead of real names you can use apple, orange, etc. Also, variable names could be modified to var1, var2, etc. – YBS Feb 21 '21 at 22:37

1 Answers1

0

You need to override the fill color with guide_legend as shown below

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) ) 

EDIT

You can test that with a sample MRE below.

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
YBS
  • 19,324
  • 2
  • 9
  • 27
  • Thank you for the reply, but when i use this code guides(fill = guide_legend(override.aes=list(color=color) ) i get the following error Warning: Error in : Unknown colour name: `1. Energie` – Steffie Szegedi Feb 20 '21 at 23:04
  • Sorry, I assumed `color <- input$selectedcolor` has your own colors defined. Otherwise, please post a [MRE](https://stackoverflow.com/help/minimal-reproducible-example). – YBS Feb 20 '21 at 23:08
  • Please post some sample data from `KEK`. You can use `dput(KEK[1:10,])` and then post that output in console into your question. – YBS Feb 21 '21 at 13:05