Skip to content

multiple arrows in geom_path if color specified, but only one arrow otherwise #4133

Closed

Description

@bob-carpenter

Summary

When using geom_path I find that

  • if I use only geom_path(arrow = arrow()), the result is a single arrow at the end of the path, but
  • if I add a colour aesthetic, geom_path(aes(colour = t, arrow = arrow())), the result is an arrow on each segment.

Minimal reproducible example

To reproduce, I set up some points on a circle and used a geom_path with and without a colour aesthetic.

library('ggplot2')

t <- seq(0, 2 * pi, by = pi / 12)
x <- cos(t)
y <- sin(t)
df <- data.frame(t = t, x = x, y = y)

plot1 <-
  ggplot(df, aes(x = x, y = y)) +
  geom_point() +
  geom_path(arrow = arrow())
plot1 

plot2 <-
  ggplot(df, aes(x = x, y = y)) +
  geom_point() +
  geom_path(aes(colour = t), arrow = arrow())
plot2 

Here's plot1.

plot1

Here's plot2:

plot2

Related issue

This is related to #2280 (geom_path should support intermediate arrows), which was marked won't fix because of difficulty.

Activity

  1. JTC-R commented on Jul 11, 2020

    @JTC-R

    I think the difference is that in the first section, you're applying arrow=arrow() as an attribute without an aesthetic so it just puts one arrow on. Whereas in the second gplot, you're applying a color as an aesthetic which is putting color over each segment which is then read as different segments, so then the arrow still is applied as an attribute but as an attribute over each segment, thus giving multiple arrows.

  2. yutannihilation commented on Jul 12, 2020

    @yutannihilation
    Member

    Thanks. I think this can be fixed somehow. For now, you can tweak the arrow like this (I don't recommend this as nothing ensures the arrow is the same length as data, though...):

    library('ggplot2')
    
    t <- seq(0, 2 * pi, by = pi / 12)
    x <- cos(t)
    y <- sin(t)
    df <- data.frame(t = t, x = x, y = y)
    
    ggplot(df, aes(x = x, y = y)) +
      geom_point() +
      geom_path(
        aes(colour = t),
        arrow = arrow(length = unit(c(rep(0, nrow(df) - 2), 0.25), "inches"))
      )

    Created on 2020-07-12 by the reprex package (v0.3.0)

  3. JTC-R commented on Jul 12, 2020

    @JTC-R

    Try:

    `plot3<-
    ggplot(df, aes(x=x, y=y)) +
    geom_point() +
    geom_path(arrow=arrow())+
    geom_path(aes(color=t))

    plot3

    Screen Shot 2020-07-11 at 11 43 23 PM

    `

    Applying two geom_path() helps differentiate the aesthetic from the attribute. But the two paths will over-ride eachother to only show which one is computed last; so the aes(color=t) should come last.

  4. yutannihilation commented on Jul 12, 2020

    @yutannihilation
    Member

    Here's an implementation. Still not sure if this should be supported or not, though... (mainly because I'm not sure if this way of tweaking the arrow is actually allowed or not)

    5035e31

    t <- seq(0, 2 * pi, by = pi / 12)
    x <- cos(t)
    y <- sin(t)
    df <- data.frame(
      t = t,
      x = x,
      y = y,
      g = rep(c("a", "b", "c", "d"), times = c(2, 4, 6, 13))
    )
    
    devtools::load_all("~/repo/ggplot2")
    #> Loading ggplot2
    
    do_plot <- function(ends) {
      ggplot(df, aes(x = x, y = y, group = g)) +
        geom_point() +
        geom_path(
          aes(colour = t),
          arrow = arrow(ends = ends)
        )
    }
    
    do_plot("last")

    do_plot("first")

    do_plot("both")

    Created on 2020-07-12 by the reprex package (v0.3.0)

  5. bob-carpenter commented on Jul 13, 2020

    @bob-carpenter
    Author

    Thanks, @JTC-R and @yutannihilation for the explanation and alternatives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions