Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

geom_line with mapped linewidth has ugly line joins #5491

Closed
banbh opened this issue Oct 24, 2023 · 3 comments
Closed

geom_line with mapped linewidth has ugly line joins #5491

banbh opened this issue Oct 24, 2023 · 3 comments

Comments

@banbh
Copy link

banbh commented Oct 24, 2023

When a geom_line has a variable linewidth the joins between the individual line segments are either ugly or nonexistent. The problem can be observed by running the code below: the blue line (which has constant linewidth) looks as expected (and good), while the red line (which has a linewidth that varies slightly) does not look as expected (and looks ugly).

By the way, I suspect the problem is that joining rectangles of varying widths is tricky. Indeed, should they even be rectangles or trapezoids? So perhaps this is actually a feature a request.

library(ggplot2) # ggplot2_3.4.2

data.frame(x = seq(0, 15, len = 50)) |> 
  ggplot(aes(x)) +
  geom_line(aes(y = sin(x), linewidth = 10 + 1e-6*x), alpha = .5, color = 'red') +
  geom_line(aes(y = sin(x) + 1, linewidth = 10 + 0*x), alpha = .5, color = 'blue') +
  scale_linewidth_identity()

image

@clauswilke
Copy link
Member

This is a limitation of the grid graphics library. It cannot draw lines that change in width or color along the way. Unfortunately there's nothing that can be done at the ggplot2 level.

@banbh
Copy link
Author

banbh commented Oct 25, 2023

you might want to have a look at: https://www.stat.auckland.ac.nz/~paul/Reports/VWline/vwline-intro/power-curve.html https://cran.r-project.org/web/packages/vwline/ https://github.com/Hy4m/ggvwline

Thank you! I was literally experimenting with ggvwline! As an example this package produces the grey line in the plot below.

tibble(x = seq(0, 15, len = 50), y = sin(x)) |> 
  ggplot(aes(x, alpha = .5)) +
  geom_line(aes(y = y, linewidth = 10 + 1e-6*x, color = 'variable')) +
  geom_line(aes(y = y + 1, linewidth = 10 + 0*x, color = 'constant')) +
  geom_vwline(aes(y = y + 2, width = (10 + 1*x)/2), color = NA) +
  scale_linewidth_identity() +
  scale_alpha_identity()

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants