Reputation:
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?
Upvotes: 1
Views: 875
Reputation: 2979
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))
Upvotes: 2