user2987808
user2987808

Reputation: 1437

ggplot2: mapping color to geom_line creates jagged line

I'm trying to plot a line graph where the colors change depending on the value of a different variable (e.g. y > 3 makes it red, otherwise green). However, when doing this with a line size greater than 1, the individual segments making up the line become very obvious, which is not the case when having a single color for the line. This especially becomes a problem when plotting e.g. daily data for several years, which is what I have in my real dataset. Here is a reproducible example:

es_sub <- structure(list(date = structure(c(15323, 15324, 15327, 15328, 
15329, 15330, 15331, 15334, 15335, 15336, 15337, 15338, 15341, 
15342, 15343, 15344, 15345, 15348, 15349, 15350, 15351, 15352, 
15355, 15356, 15357, 15358, 15359, 15362, 15363, 15364, 15365, 
15366, 15369, 15370, 15371, 15372, 15373, 15376, 15377, 15378, 
15379, 15380, 15383, 15384, 15385, 15386, 15387, 15390, 15391, 
15392, 15393), class = "Date"), yield = c(4.3923, 4.2818, 4.22, 
4.15263, 4.07348, 3.97288, 3.86626, 3.77447, 3.71084, 3.63926, 
3.59491, 3.58036, 3.57425, 3.57158, 3.56352, 3.56215, 3.57652, 
3.57602, 3.56521, 3.541, 3.50181, 3.47117, 3.43809, 3.39826, 
3.35215, 3.3009, 3.23324, 3.16079, 3.09816, 3.05649, 3.00415, 
2.93571, 2.87404, 2.81528, 2.7418, 2.65998, 2.57848, 2.51727, 
2.46631, 2.43695, 2.43333, 2.45101, 2.46015, 2.47799, 2.51173, 
2.54209, 2.56716, 2.59079, 2.60407, 2.60738, 2.60619), col = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("2", "3"), class = "factor")), .Names = c("date", 
"yield", "col"), row.names = 500:550, class = "data.frame")

ggplot(aes(x=date, y=yield, group=1), data=es_sub) + 
geom_line(aes(color=ifelse(es_sub$yield>3,"red","green")), size=3) +
theme(legend.position="none")

ggplot(aes(x=date, y=yield, group=1), data=es_sub) + geom_line(size=3)

The individual segments are obvious in the first graph, but not the second. Is there a way around this?

UPDATE: I tried what Joran suggested (i.e. mapping group in the same way as color), but that causes this problem instead (see chart) (this is my real plot. It has about 1000 observations, and I didn't want to put them all in the reproducible example).

[1]: https://i.sstatic.net/38KPB.png

Upvotes: 0

Views: 2306

Answers (2)

Ian
Ian

Reputation: 21

This is a bodge rather than a fix but it does improve the plot. Add lineend = "round" to geom_line.

ggplot(aes(x = date, y = yield, group = 1), data = es_sub) +
geom_line(aes(color = ifelse(es_sub$yield > 3, "red", "green")), size = 3, lineend = "round" ) +
theme(legend.position = "none")

Upvotes: 2

joran
joran

Reputation: 173577

Try this:

ggplot(aes(x=date, y=yield, group=1), data=es_sub) + 
    geom_line(aes(color=ifelse(yield>3,"red","green"),
                  group = ifelse(yield>3,"red","green")), size=3) +
    theme(legend.position="none")

(Note that I did not use $ to refer to the yield variable! Don't do that!)

Upvotes: 1

Related Questions