How do I get ggplot to read the colors I am feeding it

前端 未结 1 1856
萌比男神i
萌比男神i 2020-12-22 05:31

My data basically looks like this:

Populations alpha   SE  Z   sample  color
Pop1    0.029000    0.003589    8.116   9   red
Pop2    0.031868    0.003498             


        
1条回答
  •  既然无缘
    2020-12-22 06:05

    How about this?

    I'm still new to R (and SO) but afaik aes(colour=xxx) doesn't set the actual colour, it determines the scale_color_xxx to apply. You seem to want the colour to change with each Populations2, so change the colour series based on that, if that makes sense.

    Sorry, don't have enough rep (yet) to post an image of the output I get as well.

    txt <- "Populations alpha   SE  Z   sample  color
    Pop1    0.029000    0.003589    8.116   9   red
    Pop2    0.031868    0.003498    8.231   9   red
    Pop3    0.028969    0.003765    7.942   8   blue
    Pop4    0.030651    0.003479    8.792   10  green"
    
    tbl <- read.table(text = txt, header = TRUE)
    
    require(ggplot2)
    tbl$Populations2 <- factor(tbl$Populations, as.character(tbl$Populations))
    
    ggplot(tbl, aes(y = Populations2, 
                    x = alpha, 
                    xmin = alpha - SE, 
                    xmax = alpha + SE, 
                    label = Populations2,
                    colour = Populations2
                    )) + 
        geom_point(colour = "black") + 
      geom_text(hjust = 1.2) + 
      theme_classic() + 
      theme(axis.title = element_blank(),
            axis.ticks = element_blank(), 
            axis.text.y = element_blank(), 
            legend.position = "none") + 
      geom_errorbarh(height = .1) +
      scale_color_manual(values = as.character(tbl$color))
    

    0 讨论(0)
提交回复
热议问题