2011-09-14 58 views
2

用兩個這樣的因子生成點圖的最佳方法是什麼,最好使用標準R圖(而不是ggplot)和2×2數據幀。水平線應該是手段。我曾嘗試克利夫蘭點的圖表,但無法弄清楚如何得到兩個數據系列,並具有點抖動:使用R繪圖進行R點繪製

example dotplot

+4

你能告訴我們你已經嘗試過(使得Q聽起來並不像「別爲我工作」)。我們也不介意使用一些虛假的數據。 –

+0

那麼ggplot和/或格子有什麼不好呢?學習一些新功能可能需要一點時間,但從長遠來看,製作圖表會更加容易。 –

回答

12

下面的代碼應該做的伎倆:

set.seed(1) 
t1 = rnorm(10); t2 = rnorm(10, 2) 
t1_g2 = rnorm(10, 4);t2_g2 = rnorm(10) 

##Don't print the axes labels 
par(ann=FALSE) 

##Plot first set of data. 
##Need to check for sensible ranges 
##Use the jitter function to spread data out. 
plot(jitter(rep(0,10),amount=0.2), t1, 
    xlim=range(-0.5,3.5), ylim=range(-3,8), 
    axes=FALSE,frame.plot=TRUE) 
points(jitter(rep(1,10), amount=0.2), t1_g2, col=2) 
points(jitter(rep(2,10), amount=0.2), t2) 
points(jitter(rep(3,10), amount=0.2), t2_g2, col=2) 

##Add in the y-axis 
axis(2, seq(-4,8,by=2)) 

##Add in the x-axis labels 
mtext("Treatment 1", side = 1, at=0.5) 
mtext("Treatment 2", side = 1, at=2.5) 

##Add in the means 
segments(-0.25, mean(t1), 0.25, mean(t1)) 
segments(0.75, mean(t1_g2), 1.25, mean(t1_g2)) 
segments(1.75, mean(t2), 2.25, mean(t2)) 
segments(2.75, mean(t2_g2), 3.25, mean(t2_g2)) 

##Add in the legend 
legend(0, 8, c("Group 1", "Group 2"), col=1:2, pch=1) 

這給:

enter image description here

+1

就是這麼簡單,呵呵?好的工作+1 – Chase