R: Running code with reprex() produces expected output but running it from RStudio console produces error

67 Views Asked by At

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:

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")

Running from RStudio without reprex() throws error for 2nd plot

enter image description here

0

There are 0 best solutions below