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,> testdate 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: test_plot1609.pdf Type: application/pdf Size: 4609 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20160916/1ac3dd1e/attachment.pdf>
Dominik Schneider
2016-Sep-17 03:52 UTC
[R] ggplot2: geom_segment does not produce the color I desire?
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]]
Erich Neuwirth
2016-Sep-17 06:06 UTC
[R] ggplot2: geom_segment does not produce the color I desire?
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>