Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional colouring of a geom_smooth

Tags:

r

ggplot2

I'm analyzing a series that varies around zero. And to see where there are parts of the series with a tendency to be mostly positive or mostly negative I'm plotting a geom_smooth. I was wondering if it is possible to have the color of the smooth line be dependent on whether or not it is above or below 0. Below is some code that produces a graph much like what I am trying to create.

set.seed(5)
r <- runif(22, max = 5, min = -5)
t <- rep(-5:5,2)
df <- data.frame(r+t,1:22)
colnames(df) <- c("x1","x2")
ggplot(df, aes(x = x2, y = x1)) + geom_hline() + geom_line() + geom_smooth()

I considered calculating the smoothed values, adding them to the df and then using a scale_color_gradient, but I was wondering if there is a way to achieve this in ggplot directly.

like image 812
Marijn Stevering Avatar asked Oct 02 '15 13:10

Marijn Stevering


People also ask

What is the difference between Geom_line and Geom_smooth?

Geom_line creates a single line for both panels and distributes the colors according to the colour variable, while geom_smooth does not draw the smooth line in the 2nd panel.

What is the difference between Geom_smooth and Stat_smooth?

geom_smooth() and stat_smooth() are effectively aliases: they both use the same arguments. Use stat_smooth() if you want to display the results with a non-standard geom.

What does Geom_smooth () function do in R?

The geom smooth function is a function for the ggplot2 visualization package in R. Essentially, geom_smooth() adds a trend line over an existing plot. What is this? By default, the trend line that's added is a LOESS smooth line.

Is Geom_smooth a line of best fit?

geom_smooth does not plot line of best fit.


1 Answers

You may use the n argument in geom_smooth to increase "number of points to evaluate smoother at" in order to create some more y values close to zero. Then use ggplot_build to grab the smoothed values from the ggplot object. These values are used in a geom_line, which is added on top of the original plot. Last we overplot the y = 0 values with the geom_hline.

# basic plot with a larger number of smoothed values
p <- ggplot(df, aes(x = x2, y = x1)) +
  geom_line() +
  geom_smooth(linetype = "blank", n = 10000)

# grab smoothed values
df2 <- ggplot_build(p)[[1]][[2]][ , c("x", "y")]

# add smoothed values with conditional color
p +
  geom_line(data = df2, aes(x = x, y = y, color = y > 0)) +
  geom_hline(yintercept = 0)

enter image description here

like image 164
Henrik Avatar answered Nov 06 '22 07:11

Henrik