'ggplot2 - Is there a way to stop geom_freqpoly lines continuing beyond data range?

When I combine two geom_freqpolys on the same chart, the one with a narrower range on the x-axis at y=0 will continue until it reaches the other one.

An example will explain this better...

library(tidyr)
library(ggplot2)

set.seed(123)
a <- rnorm(10000, 0, 1)
b <- rnorm(10000, 1, 4)

df <- data.frame(a, b)
dfl <- pivot_longer(df, cols = c(a, b),
                    names_to = 'distribution',
                    values_to = 'values')

ggplot(dfl) +
  geom_freqpoly(aes(values, colour = distribution), bins = 100) +
  geom_point(aes(x = min(`values`[`distribution` == 'a']), y = 0), size = 3) +
  geom_point(aes(x = max(`values`[`distribution` == 'a']), y = 0), size = 3) +
  scale_x_continuous(breaks = seq(-14,17,1))

ggplot() +
  geom_freqpoly(data = df, aes(x = a), bins = 100, colour = 'red') +
  geom_freqpoly(data = df, aes(x = b), bins = 100, colour = 'blue') +
  geom_point(aes(x = min(df$a), y = 0), size = 3) +
  geom_point(aes(x = max(df$a), y = 0), size = 3) +
  scale_x_continuous(breaks = seq(-14,17,1))

Whether I use the long data to use one geom, or use the original data to use two seperate ones, I'm getting the undesirable result of the red line continuing beyond the black points. Is there a way to stop this from happening?

enter image description here

r


Solution 1:[1]

You can set breaks instead of bins.

breaksa <- seq(from = min(df$a), to = max(df$a), by = 0.1)
breaksb <- seq(from = min(df$b), to = max(df$b), by = 0.1)

ggplot() +
    geom_freqpoly(data = df, aes(x = a), breaks = breaksa, colour = 'red') +
    geom_point(aes(x = min(df$a), y = 0), size = 3) +
    geom_point(aes(x = max(df$a), y = 0), size = 3) + 
    geom_freqpoly(data = df, aes(x = b), breaks = breaksb, colour = 'blue') 

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 stomper