设置小提琴情节ggplot的最小限制

时间:2012-10-27 20:11:15

标签: r bounds ggplot2

我想设置小提琴情节的最小边界,类似于这个问题:set only lower bound of a limit for ggplot

为此:

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

我尝试过添加

+ expand_limits(y=-50)

+ aes(ymin = -50)

设置下限但没有效果。

以下是导致相同问题的示例数据框:

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

1 个答案:

答案 0 :(得分:2)

如果您添加对aes()的调用,

ggplot会关注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()

(注意:至少对于此示例,expand_limits(y = -10)无论是否伴随调用geom_blank()都可以使用。)

enter image description here