Bangyou
Bangyou

Reputation: 9816

Variable line size using ggplot2

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.

Upvotes: 1

Views: 541

Answers (1)

tonytonov
tonytonov

Reputation: 25608

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

Upvotes: 2

Related Questions