Reputation: 43
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 getting back an error of:
Error in provideDimnames(x, sep = sep, base = base) : 'dimnames' applied to non-array
I know the problem is in the geom_vline
part of the code because when I run it without those lines or run it with something like geom_vline(xintercept = c(3, 5, 8))
it works fine. I also tried turning the data from the geom_vline
into a separate dataframe first, but it did not work.
I ran a very similar piece of code last year and it worked fine, so I'm not sure if something changed with geom_vline
or if my code is just not correct due to new data or some small change I might have accidentally made.
Thanks for any help you can give.
Upvotes: 4
Views: 294
Reputation: 93811
This is happening because the class of V1
(the summary column returned by data.table
) is table, rather than a numeric vector. Change it to a vector and it should work.
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)
Compare the structure of the data frame before and after:
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>
Upvotes: 2