Variable line size using ggplot2

2019-08-06 05:08发布

问题:

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.

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.

回答1:

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))



标签: r ggplot2