Problem when creating a Bland-Altman Plot using ggplot2 with facet_wrap

182 views
Skip to first unread message

Robert

unread,
Apr 19, 2016, 2:43:46 PM4/19/16
to ggplot2
Dear ggplot2 group, When I run the r script below from http://stackoverflow.com/questions/17187287/ggplot2-add-geom-line-for-each-facet-in-bland-altman-plot below I get a message "Error: Aesthetics must be either length 1 or the same as the data (5): yintercept" and I cannot determine where the problem is. Could anyone please assist?

# http://stackoverflow.com/questions/17187287/ggplot2-add-geom-line-for-each-facet-in-bland-altman-plot

library(plyr)

library(ggplot2)

df_melt <- structure(list(Lightbox = c(84L, 67L, 80L, 63L, 76L, 66L, 79L, 81L, 77L, 82L, 84L, 67L, 80L, 63L, 76L, 66L, 79L, 81L, 77L, 82L, 84L, 67L, 80L, 63L, 76L, 66L, 79L, 81L, 77L, 82L, 84L, 67L, 80L, 63L, 76L, 66L, 79L, 81L, 77L, 82L, 84L, 67L, 80L, 63L, 76L, 66L, 79L, 81L, 77L, 82L), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("S1", "S2", "S3", "S4", "S5"), class = "factor"), value = c(82L, 65L, 73L, 50L, 50L, 50L, 72L, 56L, 76L, 78L, 88L, 66L, 71L, 60L, 54L, 55L, 63L, 68L, 73L, 75L, 73L, 65L, 76L, 57L, 51L, 57L, 75L, 65L, 69L, 66L, 77L, 67L, 79L, 58L, 55L, 56L, 77L, 66L, 73L, 80L, 78L, 62L, 78L, 52L, 63L, 59L, 71L, 64L, 69L, 89L), mean = c(83, 66, 76.5, 56.5, 63, 58, 75.5, 68.5, 76.5, 80, 86, 66.5, 75.5, 61.5, 65, 60.5, 71, 74.5, 75, 78.5, 78.5, 66, 78, 60, 63.5, 61.5, 77, 73, 73, 74, 80.5, 67, 79.5, 60.5, 65.5, 61, 78, 73.5, 75, 81, 81, 64.5, 79, 57.5, 69.5, 62.5, 75, 72.5, 73, 85.5), diff = c(2L, 2L, 7L, 13L, 26L, 16L, 7L, 25L, 1L, 4L, -4L, 1L, 9L, 3L, 22L, 11L, 16L, 13L, 4L, 7L, 11L, 2L, 4L, 6L, 25L, 9L, 4L, 16L, 8L, 16L, 7L, 0L, 1L, 5L, 21L, 10L, 2L, 15L, 4L, 2L, 6L, 5L, 2L, 11L, 13L, 7L, 8L, 17L, 8L, -7L)), .Names = c("Lightbox", "variable", "value", "mean", "diff"), row.names = c(NA, -50L), class = "data.frame")

p <- ggplot(df_melt, aes(mean, diff))+ geom_point(na.rm=TRUE)+ facet_wrap(~variable)

print(p)

# yintercepts_mean <- c(mean(df_melt$diff, na.rm = TRUE))
# yintercepts_mean_r <- round(yintercepts_mean,3)
# yintercepts_sd_p <- c(mean(df_melt$diff, na.rm = TRUE) + c(2) * sd(df_melt$diff, na.rm = TRUE))
# yintercepts_sd_n <- c(mean(df_melt$diff, na.rm = TRUE) + c(-2) * sd(df_melt$diff, na.rm = TRUE))
# yintercepts_sd_p_r <- round(yintercepts_sd_p,3)
# yintercepts_sd_n_r <- round(yintercepts_sd_n,3)
#
# #ylabels <- c("- 2SD", "+ 2SD", "Mean")
# ylabels <- c("mean")
# ylabels2 <- c("+ 2SD")
# ylabels3 <- c("- 2SD")
#
# p <- geom_hline(yintercept = yintercepts_mean_r, linetype=1, color='blue') +
# geom_hline(yintercept = yintercepts_sd_p_r, linetype=2, color='blue') +
# geom_hline(yintercept = yintercepts_sd_n_r, linetype=2, color='blue')
# print(p)


df2 <- ddply(df_melt,.(variable),summarise,mean=mean(diff, na.rm = TRUE),
sd=sd(diff, na.rm = TRUE))

p <- ggplot(df_melt, aes(mean, diff)) +
geom_point(na.rm=TRUE) +
geom_hline(data=df2,aes(yintercept=c(round(mean,3),
round(mean+2*sd,3),
round(mean-2*sd,3))),
linetype=c(1,2,2), color='blue') +
facet_wrap(~variable)

print(p)
 

Crump, Ron

unread,
Apr 20, 2016, 3:45:31 AM4/20/16
to Robert, ggplot2
Hi Robert,

>p <- ggplot(df_melt, aes(mean, diff)) +
> geom_point(na.rm=TRUE) +
> geom_hline(data=df2,aes(yintercept=c(round(mean,3),
> round(mean+2*sd,3),
> round(mean-2*sd,3))),
> linetype=c(1,2,2), color='blue') +
> facet_wrap(~variable)

Based on the error message, I split it into three calls to geom_hline:

p <- ggplot(df_melt, aes(mean, diff)) +
geom_point(na.rm=TRUE) +
geom_hline(data=df2,aes(yintercept=round(mean,3)),linetype=1,
color='blue') +
geom_hline(data=df2,aes(yintercept=round(mean+2*sd,3)),linetype=2,
color='blue') +
geom_hline(data=df2,aes(yintercept=round(mean-2*sd,3)),linetype=2,
color='blue') +
facet_wrap(~variable)

And that seems to work.

Ron.

Reply all
Reply to author
Forward
0 new messages