[R] ggplot2: geom_segment does not produce the color I desire?

Erich Neuwirth erich.neuwirth at univie.ac.at
Sat Sep 17 08:06:07 CEST 2016


Here are 2 solutions to you problem:

If you only want to use one color (for possibly many arrows),
this will work:

ggplot()+geom_segment(mapping = aes(x = as.Date(test[,"date"]),
                                    y =y1,
                                    xend = as.Date(test[,"date"]),
                                    yend = y2), color="green", data=testdf,
                      arrow=arrow())


if you use a variable to colors different items differently,
you are using a mapping.
If you want to override ggplot’s default mapping, you set the palette explicitly:

ggplot()+geom_segment(mapping = aes(x = as.Date(test[,"date"]),
                                    y =y1,
                                    xend = as.Date(test[,"date"]),
                                    yend = y2, color=co), data=testdf,
                                    arrow=arrow()) +
      scale_color_manual(values=list(green="green"))


> On Sep 17, 2016, at 00:43, John <miaojpm at gmail.com> wrote:
> 
>> 
>> test
>        date    co       y1       y2
> 5 2011-11-28 green 196.6559 1.600267
>> dput(test)
> structure(list(date = structure(15306, class = "Date"), co = "green",
>    y1 = 196.655872, y2 = 1.600267), .Names = c("date", "co",
> "y1", "y2"), class = "data.frame", row.names = 5L)
>> ggplot()+    geom_segment(mapping = aes(x = as.Date(test[,"date"]), y =
> y1, xend = as.Date(test[,"date"]), yend = y2, color=co), data=test,
> arrow=arrow())
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 670 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20160917/1174e7cd/attachment.bin>


More information about the R-help mailing list