0

My data basically looks like this:

Populations alpha   SE  Z   sample  color
Pop1    0.02    0.003   7   9   red
Pop2    0.03    0.003   10  9   red
Pop3    0.02    0.003   11  8   blue
Pop4    0.03    0.003   14  10  green

and I and am plotting as point estimates with error bars. I am having difficulty getting ggplot2 to read my colors.

read.table("table.txt", header = TRUE) -> tbl 
require(ggplot2)
tbl$Populations2 <- factor(tbl$Populations, as.character(tbl$Populations))

#pdf(file="table.pdf", width=11)

ggplot(tbl, aes(y = Populations2, x = alpha, xmin = alpha - 
    SE, xmax = alpha + SE, label = Populations2, colour = 
    color)) + 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)
#dev.off()

When I run the code, the pops with the same color listed in the input end up being the same color, but they do not become the color I want them to be. I have tried this with the colors in quotes as well as with the colors listed in hexadecimal, but I get the same problem either way. Any idea how to fix this?

  • 2
    You don't need all these `tbl$` in `aes()`. You already say `ggplot` to use `tbl`. –  Jan 14 '16 at 23:26
  • 1
    You can use `scale_colour_manual`. Reference: http://docs.ggplot2.org/0.9.3.1/scale_manual.html – ytk Jan 14 '16 at 23:35
  • 1
    The first pointer in [this comment](https://stackoverflow.com/questions/34778302/plotting-point-estimates-with-error-bars-for-different-groups/34779537#comment57340243_34779537), [in your former questing](https://stackoverflow.com/questions/34778302/plotting-point-estimates-with-error-bars-for-different-groups), already say what Pascal say above. – Eric Fail Jan 14 '16 at 23:39
  • Maybe `tbl$color` is a factor? – ialm Jan 14 '16 at 23:44
  • you need to be very careful when you code Things. Look at the difference betwene these two statements `+ scale_colour_manual(values=tbl$color)` and `+ scale_colour_manual(values=color)` (the latter is the correct solution as the pointer pointed out above points out and as [Pascal points out in his first comment above](https://stackoverflow.com/questions/34801653/how-do-i-get-ggplot-to-read-the-colors-i-am-feeding-it#comment57346621_34801653)) – Eric Fail Jan 14 '16 at 23:45
  • If you supply a minimal reproducible example to go along with your question it would be much easier for us to help you identify the problem. – Eric Fail Jan 15 '16 at 00:36
  • Or look into `scale_color_identity()` – Heroka Jan 15 '16 at 13:43
  • Does this answer your question? [How to conditionally highlight points in ggplot2 facet plots - mapping color to column](https://stackoverflow.com/questions/15804504/how-to-conditionally-highlight-points-in-ggplot2-facet-plots-mapping-color-to) – tjebo Jul 01 '20 at 13:44

1 Answers1

1

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))
Liesel
  • 2,929
  • 2
  • 12
  • 18
  • +1 This works. Some nitpicking, though: I know you copied some of the code from the original post, but you do not need to make a new factor variable, `Populations2`, since `ggplot` coerces character to factor automatically, you can just use `Populations`. Also, you can set `colour = color` in the `ggplot(aes(...))` function, then use `scale_color_identity()` instead of `scale_color_manual(...)`, as per @Heroka 's suggestion in the comments. – ialm Jan 15 '16 at 17:12
  • Also, there is an argument for using `library` and not `require` in your code unless you explicitly need `require` [(see this nice post)](http://yihui.name/en/2014/07/library-vs-require/) – ialm Jan 15 '16 at 17:18