2012-10-27 57 views
4

我想設定的最低界限爲小提琴情節,類似這樣的問題:set only lower bound of a limit for ggplot設定下限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") 
+0

有趣的問題。如果您提供可複製的數據,則您更有可能得到及時的答覆。 –

回答

2

ggplot將注重aes()指令,如果添加了一個調用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

+0

你的例子完美地工作,但它不適用於我的數據。我將不得不提供一個小樣本。我需要找到一種方法來抓住極端的點。我試圖設置的ymin之外的數據點會干擾嗎? – Donnied

+1

@Donnied - 是的,提供一個可重複使用的小例子將爲您提供最有幫助的答案(您可以在創建過程中自己發現問題)。 –

+0

很好的建議。我不知道發生過多少次這樣的事情,我只是爲了發現創建代表行爲中的問題而做出可重複的例子。例。 +1回答和評論 –