4

I'd like to set the minimum bounds for a violin plot, similar to this question: set only lower bound of a limit for ggplot

For this:

p <- ggplot(somedf, aes(factor(user1), pq)) + aes(ymin = -50)
p + geom_violin(aes(fill = user1))+ aes(ymin=-50)

I've tried adding

+ expand_limits(y=-50)

and

+ aes(ymin = -50)

to set lower bounds with no effect.

Here's a sample dataframe that results in the same problem:

structure(list(pq = c(-20L, -12L, 10L, -13L, 11L, -16L), time = c(1214.1333, 
1214.1833, 1214.2667, 1214.2833, 1214.35, 1214.5167), pq.1 = c(-20L, 
-12L, 10L, -13L, 11L, -16L), time.1 = c(1214.1333, 1214.1833, 
1214.2667, 1214.2833, 1214.35, 1214.5167), time.2 = c(1214.1333, 
1214.1833, 1214.2667, 1214.2833, 1214.35, 1214.5167), pq.2 = c(-20L, 
-12L, 10L, -13L, 11L, -16L), user1 = structure(c(1L, 1L, 2L, 
1L, 2L, 1L), .Label = c("someguy3", "someguy4", "someguy6", "someguy4", 
"someguy5", "someguy6"), class = "factor"), pq.3 = c(-20L, -12L, 10L, 
-13L, 11L, -16L), time.3 = c(1214.1333, 1214.1833, 1214.2667, 
1214.2833, 1214.35, 1214.5167), user1.1 = structure(c(1L, 1L, 
2L, 1L, 2L, 1L), .Label = c("someguy3", "someguy4", "someguy6", 
"someguy4", "someguy5", "someguy6"), class = "factor")), .Names = c("pq", 
"time", "pq.1", "time.1", "time.2", "pq.2", "user1", "pq.3", 
"time.3", "user1.1"), row.names = c(565L, 566L, 568L, 569L, 570L, 
574L), class = "data.frame")
Community
  • 1
  • 1
d-cubed
  • 1,034
  • 5
  • 30
  • 58

1 Answers1

2

ggplot will pay attention to the aes() directive if you add a call to geom_blank().

## A reproducible example
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))

## This doesn't work:
p + aes(ymin = -10) + geom_violin()

## But this does:    
p + aes(ymin = -10) + geom_violin() + geom_blank()

(Note: For this example at least, expand_limits(y = -10) works with or without an accompanying call to geom_blank().)

enter image description here

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • Your example works perfectly, but it's not working with my data. I'll have to provide a small sample. I'll need to find a way to grab the extreme points. Would data points outside the ymin I'm trying to set interfere? – d-cubed Oct 27 '12 at 21:03
  • 1
    @Donnied -- Yes, supplying a small reproducible example will get you the most helpful answer (and you may discover the problem yourself in the process of creating it). – Josh O'Brien Oct 27 '12 at 21:13
  • Good advice. I don't know how many times this has happened to me where I make the reproducible example only to discover the problem in the act of creating the rep. example. +1 answer and comment – Tyler Rinker Oct 27 '12 at 22:18