> ggplot(test, aes(x = x1, y = reorder(Level, x2))) + + geom_hline(aes(yintercept = reorder(Level, x2)), color = "grey50", size = 0.25, alpha = 0.5) + + geom_point(size = 4) + + geom_text(aes(label = x1), vjust = -1) + + theme_bw() + + opts(title = "test", + panel.background = theme_rect(linetype = "solid", size = 1), + panel.grid.minor = theme_blank(), + panel.grid.major = theme_blank() + ) Error in x - from[1] : non-numeric argument to binary operator> ggplot(test, aes(x = x1, y = reorder(Level, x2))) + + geom_hline(yintercept = reorder(Level, x2), color = "grey50", size = 0.25, alpha = 0.5) + + geom_point(size = 4) + + geom_text(aes(label = x1), vjust = -1) + + theme_bw() + + opts(title = "test", + panel.background = theme_rect(linetype = "solid", size = 1), + panel.grid.minor = theme_blank(), + panel.grid.major = theme_blank() + ) Error in reorder(Level, x2) : object 'Level' not found> ggplot(test, aes(x = x1, y = reorder(Level, x2))) + + geom_hline(yintercept = Level, color = "grey50", size = 0.25, alpha = 0.5) + + geom_point(size = 4) + + geom_text(aes(label = x1), vjust = -1) + + theme_bw() + + opts(title = "test", + panel.background = theme_rect(linetype = "solid", size = 1), + panel.grid.minor = theme_blank(), + panel.grid.major = theme_blank() + ) Error in get(x, envir = this, inherits = inh)(this, ...) : object 'Level' not found > ggplot(test, aes(x = x1, y = reorder(Level, x2))) + + geom_hline(aes(yintercept = Level), color = "grey50", size = 0.25, alpha = 0.5) + + geom_point(size = 4) + + geom_text(aes(label = x1), vjust = -1) + + theme_bw() + + opts(title = "test", + panel.background = theme_rect(linetype = "solid", size = 1), + panel.grid.minor = theme_blank(), + panel.grid.major = theme_blank() + ) Error in x - from[1] : non-numeric argument to binary operator > ggplot(test, aes(x = x1, y = reorder(Level, x2))) + + geom_hline(aes(yintercept = reorder(test$Level, test$x2)), color = "grey50", size = 0.25, alpha = 0.5) + + geom_point(size = 4) + + geom_text(aes(label = x1), vjust = -1) + + theme_bw() + + opts(title = "test", + panel.background = theme_rect(linetype = "solid", size = 1), + panel.grid.minor = theme_blank(), + panel.grid.major = theme_blank() + ) Error in x - from[1] : non-numeric argument to binary operator
> x <- print(ggplot(test, aes(x = x1, y = reorder(Level, x2))) + + geom_hline(yintercept = reorder(test$Level, test$x2), color = "grey50", size = 0.25, alpha = 0.5) +
+ geom_point(size = 4) + + geom_text(aes(label = x1), vjust = -1) + + theme_bw() + + opts(title = "test", + panel.background = theme_rect(linetype = "solid", size = 1), + panel.grid.minor = theme_blank(), + panel.grid.major = theme_blank()
+ )) Error : Invalid intercept type: should be a numeric vector, a function, or a name of a function
Solution as follows produces the Cleveland style dot plot as desired:
test <- data.frame(Level = c("first", "Second", "third", "Fourth"),
x1 = c(100, 200, 300, 400),
x2 = c(300, 145, 185, 227)
)
ggplot(test, aes(x = x1, y = reorder(Level, x2))) +
scale_y_discrete() +
geom_hline(aes(yintercept = as.numeric(Level)), colour = "grey50", size = 0.25, alpha = 0.5) +