1

I would like to change the line size with a continuous variable. Now I used the geom_line with aesthetics size. For example:

x <- 1:100
y <- x * x 
z <- abs(cos(x * pi / (max(x))))

df <- data.frame(x = x, y = y, z = z)
library(ggplot2)

ggplot(df, aes(x, y, size = z)) + geom_line()

But there are some spaces among segments (see figure below. Please zoom in to see the spaces). it seems ggplot2 uses rectangles to plot each segment.

enter image description here

I have increased the point number, but spaces till exist for bigger curvature.

My question is how to remove these spaces. I really appreciate it for any suggestions.

Bangyou
  • 9,462
  • 16
  • 62
  • 94

1 Answers1

2

Adjust the multiplier to your preference:

mult <- 200
ggplot(df, aes(x, y)) + geom_line() + geom_ribbon(aes(ymin=y-mult*z, ymax=y+mult*z))

enter image description here

tonytonov
  • 25,060
  • 16
  • 82
  • 98