When I select and copy all of the below code and then call reprex::reprex()
, it successfully produces two plots. However, when I run the code by selecting all of the code and then pressing Cntrl + Enter from RStudio, it only produces the first plot and then throws the below error during the second ggplot
call:
Error in seq.default(from, to, by) : invalid '(to - from)/by
What is causing this and how might I address it?
Considerations:
- The problem is resolved by using
geom_bar()
instead ofgeom_bar_pattern()
. However, I'd like to use the pattern support of the latter. - I've tried
rm(list=ls()
and restarting RStudio to no avail. - I followed Visualizing with ggplot yields different output using reprex vs. without reprex and swapped to Cairo and other graphics devices with no success.
Output when I run this with reprex()
library(dplyr)
library(ggplot2)
library(ggpattern)
df <- structure(list(Proportion = c(0.41, 0.47, 0.23, 0.21, 0.36, 0.32,
0.36, 0.33, 0.38, 0.42, 0.27, 0.25, 0.51, 0.49, 0.49, 0.51, 0.45,
0.44, 0.27, 0.28, 0.27, 0.27), Group = c("18-44", "18-44", "45-54",
"45-54", "55+", "55+", "4-Year or bachelors degree", "4-Year or bachelors degree",
"High school diploma or less", "High school diploma or less",
"Postgraduate degree", "Postgraduate degree", "Female", "Female",
"Male", "Male", "$150,000 - $199,999", "$150,000 - $199,999",
"$200,000 - $269,999", "$200,000 - $269,999", "$270,000+", "$270,000+"
), `Data Source` = c("src2", "src1", "src2", "src1", "src2",
"src1", "src2", "src1", "src2", "src1", "src2", "src1", "src2",
"src1", "src2", "src1", "src2", "src1", "src2", "src1", "src2",
"src1"), Variable = c("Age", "Age", "Age", "Age", "Age", "Age",
"Education", "Education", "Education", "Education", "Education",
"Education", "Gender", "Gender", "Gender", "Gender", "Income",
"Income", "Income", "Income", "Income", "Income")), row.names = c(NA,
-22L), class = c("tbl_df", "tbl", "data.frame"))
# geom_bar() - success!
df %>%
ggplot(aes(x = Proportion, y = Group, fill = `Data Source`)) +
geom_bar(stat = "identity",
position = "dodge")
# geom_bar_pattern() - error
df %>%
ggplot(aes(x = Proportion, y = Group, fill = `Data Source`)) +
geom_bar_pattern(stat = "identity",
position = "dodge")