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

Dominik Schneider dosc3612 at colorado.edu
Sat Sep 17 05:52:05 CEST 2016


ggplot will assign, or map if you will, the color based on the default
color scale when color is specified with the mapping argument such as
mapping = aes(color=...). You have two options:

1. if you want the color of your arrow to be based on a column in your
data, then manually scale the color with
scale_colour_manual(values=c('green')):
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())+
scale_colour_manual(values=c('green'))

2. If the color doesn't need to be "mapped" based on your data, then you
can simply specify colour *outside* the aes() like this:
ggplot()+
geom_segment(mapping = aes(x = as.Date(test[,"date"]), y = y1, xend =
as.Date(test[,"date"]), yend = y2), color='green', data=test, arrow=arrow())

keep in mind that only the first option will produce a legend, if you need
that.



On Friday, September 16, 2016, John <miaojpm at gmail.com> wrote:

> Hi,
>
>    I have a dataset "test". I try to produce a "green" arrow but it gives a
> "red" arrow (as attached). Could someone tell me how I can fix it? Thanks,
>
> > 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())
> >
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list