Error geom_vline and facet_wrap in ggplot

I am trying to run the following code:

temp_plotdata <- data.table(Treatment_Code = c('Control', 'Control', 
                                               'Second Mailing', 'Second Mailing', 
                                               'First Mailing', 'First Mailing'),
                            Q9 = c(9, 14, 10, 3, 1, 4))

output_gg <-
  ggplot(temp_plotdata, aes(x = Q9)) +
  geom_histogram(binwidth = 1, fill = 'lightblue') +
  geom_vline(data = temp_plotdata[, summary(Q9)[c(2,3,5)], by=Treatment_Code],
             aes(xintercept = V1),
             linetype = 'dashed', color = 'darkred') +
  facet_wrap(~Treatment_Code, ncol = 1)

I am returning an error:

Error in allowDimnames (x, sep = sep, base = base) parameters: "dimnames" applies to a non-array

I know that the problem is part of the code geom_vline, because when I run it without these lines or run it with something like geom_vline(xintercept = c(3, 5, 8)), it works fine. I also tried first converting the data from geom_vlineto a separate data framework, but that didn't work.

Last year I used a very similar code, and it worked fine, so I'm not sure if something changed with geom_vlineor if my code is simply not correct due to new data or a small change that I might have accidentally made.

, .

+4
1

, V1 ( , data.table) - , . , .

output_gg <-
  ggplot(temp_plotdata, aes(x=Q9)) +
  geom_histogram(binwidth=1, fill='lightblue') +
  geom_vline(data=temp_plotdata[, as.vector(summary(Q9)[c(2,3,5)]), by=Treatment_Code],
             aes(xintercept=V1),
             linetype='dashed', color='darkred') +
  facet_wrap(~ Treatment_Code, ncol=1)

:

str(temp_plotdata[, summary(Q9)[c(2,3,5)], by=Treatment_Code])
Classes ‘data.table’ and 'data.frame':    9 obs. of  2 variables:
  $ Treatment_Code: chr  "Control" "Control" "Control" "Second Mailing" ...
  $ V1            :Class 'table'  num [1:9] 10.25 11.5 12.75 4.75 6.5 ...
- attr(*, ".internal.selfref")=<externalptr>
str(temp_plotdata[, as.vector(summary(Q9)[c(2,3,5)]), by=Treatment_Code])
Classes ‘data.table’ and 'data.frame':    9 obs. of  2 variables:
  $ Treatment_Code: chr  "Control" "Control" "Control" "Second Mailing" ...
  $ V1            : num  10.25 11.5 12.75 4.75 6.5 ...
- attr(*, ".internal.selfref")=<externalptr>
+2

Source: https://habr.com/ru/post/1658483/


All Articles