EDIT:
I asked a question which boils down to this:
\"How can I get ggplot to use hexadecimal colors?\"
The answer by MrFick was exce
If you want to specify a literal color value in a geom_segment, you should not include it in the aes()
. For example using this test data
DF_for_plotting <- data.frame(
variable=rep("StrpCnCor",4),
value=c(0, 50.79330935, 81.127731, 100)
)
you can do
ggplot() +
geom_segment(data=DF_for_plotting, aes(x=value[1], xend=value[2]-0.001, y=1, yend=1), colour="green", size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[2], xend=value[3]-0.001, y=1, yend=1), colour="blue", size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[3], xend=value[4]-0.001, y=1, yend=1), colour="red", size=10)
or with hex colors
ggplot() +
geom_segment(data=DF_for_plotting, aes(x=value[1], xend=value[2]-0.001, y=1, yend=1), colour="#9999CC", size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[2], xend=value[3]-0.001, y=1, yend=1), colour="#66CC99", size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[3], xend=value[4]-0.001, y=1, yend=1), colour="#CC6666", size=10)
Although because you're not mapping anything to the color aesthetic, no legend will be provided.
When you put it in the aes()
, you're not specifying a literal value, you are just specifying a literal value to associate with a color it doesn't matter if you use aes(color="red")
or aes(color="determination")
; it just treats it as a literal character value and will use it's own color palate to assign a color to that character value. You can specify your own colors with scale_fill_manual
For example
ggplot() +
geom_segment(data=DF_for_plotting, aes(x=value[1], xend=value[2]-0.001, y=1, yend=1, colour="a"), , size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[2], xend=value[3]-0.001, y=1, yend=1, colour="b"), , size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[3], xend=value[4]-0.001, y=1, yend=1, colour="c"), size=10) +
scale_color_manual(values=c(a="green",b="blue",c="red"))
ggplot() +
geom_segment(data=DF_for_plotting, aes(x=value[1], xend=value[2]-0.001, y=1, yend=1, colour="a"), , size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[2], xend=value[3]-0.001, y=1, yend=1, colour="b"), , size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[3], xend=value[4]-0.001, y=1, yend=1, colour="c"), size=10) +
scale_color_manual(values=c(a="#9999CC",b="#66CC99",c="#CC6666"))
Here i called the three groups "a", "b", and "c" but you could also call then "green","blue","red" if you want -- it just seems odd to have a legend that tells you what color is green.