1

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

socialscientist
  • 3,759
  • 5
  • 23
  • 58
  • 1
    Using a reprex or running in the console both work for me. Does the error always appear with the same graph? Is there still this error if you swap the two graphs? – bretauv Aug 04 '22 at 08:11
  • Following your comment, I tried running it on 2 other machines and it worked fine on both of those while it still failed to work on the original machine. Going to delete packages and try again, as well as try swapping the order of the graphs. Will report back. – socialscientist Aug 04 '22 at 09:02

0 Answers0