geom_hline with factor as yintercept

1,074 views
Skip to first unread message

Matt Neilson

unread,
Mar 2, 2012, 10:36:02 AM3/2/12
to ggp...@googlegroups.com
Hi all,

I've run into a problem as I'm testing some of my code with the transition from 0.8.9 to 0.9.0. I am trying to make a Cleveland style dotplot with horizontal lines to act as guides (e.g., like panel.grid.major but only on y axis). In 0.8.9, this code generally worked to produce the desired result:

## test dataset
test <- data.frame(Level = c("first", "Second", "third", "Fourth"),
                   x1 = c(100, 200, 300, 400),
                   x2 = c(300, 145, 185, 227)
                   )
## dotchart
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()
       )

The resultant plot was a dot chart in b/w with faint grey horizontal lines as guides. With 0.9.0, I cannot successfully generate this same plot. I've tried several different versions, but to no avail:

> 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

Any suggestions or solutions would be greatly appreciated.

Cheers,
-Matt

Matt Neilson

unread,
Mar 2, 2012, 3:51:05 PM3/2/12
to ggp...@googlegroups.com
The closest that I've come to solving this is:

> 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

This allows the plot to be rendered, but is still missing all of the horizontal lines (whereas all of the other plot ggplot statements did not render at all).

Matt Neilson

unread,
Mar 6, 2012, 5:05:37 PM3/6/12
to ggp...@googlegroups.com
I seem to have found a two-part solution to this:
  1. Setting the y-axis as discrete before the call to geom_hline
  2. forcing the factor variable to numeric using as.numeric()

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) +

Matt Neilson

unread,
Mar 6, 2012, 5:08:03 PM3/6/12
to ggp...@googlegroups.com
With thanks to Dennis Murphy for the as.numeric suggestion...
Reply all
Reply to author
Forward
0 new messages