2013-10-17 64 views
5

我有一個包含12個變量的圖形,分爲兩組。我不能使用方面,但使用顏色和形狀,我能夠使可視化易於理解。但是,有些重疊(部分或全部)。我使用抖動來處理這些問題,但正如您從附圖中看到的那樣,這會導致所有點被移動,而不僅僅是那些有重疊的點。 enter image description here使用geom_point有條件使用ggplot2中的抖動

有沒有一種方法可以有條件地使用抖動或閃避?更妙的是,有沒有辦法將部分重疊的點並排放置?正如你所看到的,我的X軸是離散的類別,稍微向左/右移動並不重要。我試圖用binaxis='y'使用dotplot,但是這完全破壞了x軸。

編輯:This graph已經成功地做了我正在尋找的東西。

進一步編輯:添加此可視化背後的代碼。

disciplines <- c("Comp. Sc.\n(17.2%)", "Physics\n(19.6%)", "Maths\n(29.4%)", "Pol.Sc.\n(40.4%)", "Psychology\n(69.8%)") 

# To stop ggplot from imposing alphabetical ordering on x-axis 
disciplines <- factor(disciplines, levels=disciplines, ordered=T) 

# involved aspects 
intensive <- c(0.660, 0.438, 0.515, 0.028, 0.443) 
comparative <- c(0.361, 0.928, 0.270, 0.285, 0.311) 
wh_adverbs <- c(0.431, 0.454, 0.069, 0.330, 0.577) 
past_tense <- c(0.334, 0.229, 0.668, 0.566, 0.838) 
present_tense <- c(0.680, 0.408, 0.432, 0.009, 0.996) 
conjunctions <- c(0.928, 0.207, 0.162, -0.299, -0.045) 
personal  <- c(0.498, 0.521, 0.332, 0.01, 0.01) 
interrogative <- c(0.266, 0.202, 0.236, 0.02, 0.02) 
sbj_objective <- c(0.913, 0.755, 0.863, 0.803, 0.913) 
possessive <- c(0.896, 0.802, 0.960, 0.611, 0.994) 
thrd_person <- c(-0.244, -0.265, -0.310, -0.008, -0.384) 
nouns  <- c(-0.602, -0.519, -0.388, -0.244, -0.196) 

df1 <- data.frame(disciplines, 
       "Intensive Adverbs"=intensive, 
       "Comparative Adverbs"=comparative, 
       "Wh-adverbs (WRB)"=wh_adverbs, 
       "Verb: Past Tense"=past_tense, 
       "Verb: Present Tense"=present_tense, 
       "Conjunctions"=conjunctions, 
       "Personal Pronouns"=personal, 
       "Interrogative Pronouns"=interrogative, 
       "Subjective/Objective Pronouns"=sbj_objective, 
       "Possessive Pronouns"=possessive, 
       "3rd-person verbs"=thrd_person, 
       "Nouns"=nouns, 
       check.names=F) 

df1.m <- melt(df1) 
grp <- ifelse(df1.m$variable %in% c('3rd-person verbs','Nouns'), 'Informational Features', 'Involved Features') 
g <- ggplot(df1.m, aes(group=grp, disciplines, value, shape=grp, colour=variable)) 
g <- g + geom_hline(yintercept=0, size=9, color="white") 
g <- g + geom_smooth(method=loess, span=0.75, level=0.95, alpha=I(0.16), linetype="dashed") 
g <- g + geom_point(size=4, alpha=I(0.7), position=position_jitter(width=0.1, height=0)) 
g <- g + scale_shape_manual(values=c(17,19)) 
+0

你應該提供一個可重複的例子(數據+代碼),讓別人用它玩... – agstudy

+0

添加的代碼。希望這有助於:-) –

+1

感謝您的代碼。附:您的繪圖看起來不像生物醫學示例那樣乾淨,因爲您的Y值遍佈整個地方,但您仍然可以按照下面的的順序排列x值。 – beroe

回答

3

我很好奇,可能會建議別人,但要獲得並排的副作用,你可以編寫主要x軸類別,數字(10,20,50 ..)加/減一個根據您用於顏色的類別,(0..10)/ 2這樣的小數量。所以你可以得到X軸爲9.6,9.8,10.0,10.2 ......然後是20.0,20.2,20.4。這可以創建一個有組織的情節,而不是隨機分配這些分數調整。

這是您的數據集的這個想法的快速實現。它通過variable子類的六分之一抵消主x變量disciplines並使用該無抖動的x值...

M = df1.m 
ScaleFactor = 6 
xadj = as.numeric(M$variable)/ScaleFactor 
xadj = xadj - mean(xadj) # shift it to center around zero 
x10 = as.numeric(M$disciplines) * 10 
M$x = x10 + xadj 
g = ggplot(M, aes(group=grp, x, value, shape=grp, colour=variable)) 
g + geom_point(size=4,alpha=I(0.7)) + scale_x_discrete(breaks=x10,labels=disciplines) 

注意,每個類別內的值發生均勻地並以相同的間隔訂購。 (該代碼不包括圖中所示的所有曲線擬合等)。

enter image description here

變化:你甚至可以更清楚的看到效果,如果你「量化」的y值,由側方的情節讓更多的人。

M$valmod = M$value - M$value %% 0.2 + .1 

然後使用valmod代替valueaes()語句來看看效果。

要獲取類別標籤,請使用scale_x_discrete手動設置。此版本使用更廣泛的間距不同ScaleFactor和量化的Y軸:

M=df1.m 
ScaleFactor = 3 
# Note this could just be xadj instead of adding to data frame 
M$xadj = as.numeric(M$variable)/ScaleFactor 
M$xadj = M$xadj - mean(M$xadj) # shift it to center around zero 
M$x10 = as.numeric(M$disciplines) * 10 
M$x = M$x10 + M$xadj 

Qfact = 0.2 # resolution to quantize y values 
M$valmod = M$value - M$value %% Qfact + Qfact/2 # clump y to given resolution 

g = ggplot(M, aes(group=grp, x, valmod, shape=grp, colour=variable)) + 
    scale_x_discrete(limits = M$x10, breaks=unique(M$x10),labels=levels(M$disciplines)) 
g + geom_point(size=3,alpha=I(0.7)) 

quantized

+0

我在抖動中使用'height = 0'。所有點的垂直位置直接來自數據。即使在附近沒有其他數據點時,我也不喜歡水平位置如何移動。 –

+0

這看起來不錯!但是......有沒有辦法在x軸上取回原始類別名稱而不是數字值? –

+0

是的,這是「爲讀者做的一個練習」,但是我把它添加進了...我想看到這個「統一的抖動」添加到基礎ggplot2中。也許一些R大師也會有另一種方法。 – beroe

相關問題