geom_line with linetype as aesthetic unexpectedly fails

Hi,

I've been trying to use geom_line to produce a lineplot where linetype is mapped to a boolean. I want the line to not be 'broken' between both line types, i.e. I want a continuous line. I've ultimately been able to solve my problem as outlined in the comment above p2 in the code below (by adding an additional data point).

In any case, my question is: Why doesn't the code for p3 below achieve what I want? At the very least, the error message doesn't seem to be very informative (neither of colour linewidth or alpha change along the line).

library(tidyverse)
library(patchwork)

set.seed(123)
df <- tibble(
    x = 1:5,
    y = 3 * x
) %>%
    mutate(some_bool = y < 9)

# This is fine, obviously
p1 <- ggplot(df, aes(x = x, y = y)) +
    geom_line()
# This breaks the lines into 2 dicontiunous segments
# Could be fixed by adding an additional data point, but I don't find it a very clean/satisfying solution
p2 <- ggplot(df, aes(x = x, y = y, linetype = some_bool)) +
    geom_line()
# I hate this
p2.1 <- ggplot(rbind(
	rbind(
		df %>% filter(some_bool),
		df %>% filter(!some_bool) %>% head(1) %>% mutate(some_bool = TRUE)
		),
	df %>% filter(!some_bool)
), aes(x = x, y = y, linetype = some_bool)) +
    geom_line()
p3 <- ggplot(df, aes(x = x, y = y, linetype = some_bool, group = 1)) +
    geom_line()

print(p1)
print(p2)
print(p2.1)
print(p3) # This fails

print(p3) # This fails
I don't know enough about ggplot2 to understand why but linetype() seems to be conflicting with group()

Does this help?

p3 <- ggplot(df, aes(x = x, y = y, colour  = some_bool, group = 1)) +
    geom_line()

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.