When creating very tall horizontal bar plots with ggplot2
, the plot rendered when using the package reprex
cuts off some of the data, while the axis labels remain in the correct positions. This is not the same behavior as the ggplot output for the same code.
reprex
output:
library(babynames)
library(dplyr)
library(ggplot2)
data("babynames")
bn <- babynames %>%
filter(year == 2015) %>%
arrange(-n) %>%
head(400) %>%
mutate(highlight = ifelse(n>12000, TRUE, FALSE)) %>%
arrange(name)
breaks <- bn %>% filter(highlight == TRUE) %>% pull(name)
ggplot(bn, aes(x=name, y=n, fill=highlight)) +
geom_col() +
scale_x_discrete(breaks = breaks)+
coord_flip() +
theme_classic()
Created on 2018-09-19 by the reprex package (v0.2.1)
Using ggsave()
to save a png and upload it to stackoverflow:
ggsave("long_example.png",
width = 4,
height = 6,
dpi=200)
In the ggsave()
version, the highlighted bar for Abigail is appearing correctly, whereas the bottom few bars, including Abigail's, have disappeared in the reprex version. What's going on here?