2015-02-05 86 views
1

我無法在ggplot2geom_boxplot()圖層頂部繪製geom_point()圖層,並且已經完成了一些研究,並且似乎沒有任何精確的報告問題這種性質。我的數據集中有3個因子:name,genotyperegion,我的響應變量爲volume。我有工作代碼來產生兩個圖層的情節。問題是這些點忽略fill因子geom_point(),但不是geom_boxplot()。結果是所有點都繪製在一組盒形圖的中間,每個值爲name。這是我構建劇情的代碼。R - 在geom_boxplot上繪製geom_point(ggplot2)

meansPlot = ggplot(data=meansData,aes(x=factor(name), y=volume, fill=factor(genotype))) 
meansPlot = meansPlot + 
geom_boxplot() + 
geom_point() + 
facet_wrap(~ region, scales='free') 

我很抱歉沒有創建一個可重現的數據集 - 我不是很精通模擬數據。如果沒有簡單的答案(我期望有這個答案,而且我可能只是錯過了某些東西),我會添加模擬數據來幫助回答問題。

謝謝!

+0

'fill'不適用於點(一般),但是'顏色'。所以你必須在點上使用'color'。此外,[this](http://stackoverflow.com/q/10493084/324364)可能是一個很好的參考資料(只需使用'fill'作爲箱形圖)。 – joran 2015-02-05 21:45:19

+0

您可能會看到不同的情節符號[** here **](http://www.cookbook-r.com/Graphs/Shapes_and_line_types/)。對於'pch' 21-25,你可以指定填充顏色。 – Henrik 2015-02-05 21:56:47

+0

感謝您的參考joran。它證明是非常有用的。 – user26665 2015-02-06 21:06:31

回答

-1

我最終解決了它。此代碼錯開geom_point()geom_boxplot()內聯。

meansPlot = ggplot(data=meansData, aes(x=name, y=volume, fill=genotype, color=genotype)) 
meansPlot = meansPlot + 
geom_point(position=position_jitterdodge(dodge.width=0.9)) + 
geom_boxplot(fill="white", position=position_dodge(width=0.9), alpha=0.5) + 
facet_wrap(~ region, scales='free') 

感謝大家的努力。

1

geom_point()應該使用color屬性,而不是fill屬性(除非使用不尋常的shape s)。看看這是否適合你:

meansPlot = ggplot(data=meansData,aes(x=factor(name), y=volume, fill=factor(genotype)), color = factor(genotype)) 
meansPlot = meansPlot + 
geom_boxplot() + 
geom_point() + 
facet_wrap(~ region, scales='free') 
+0

嗯,仍然無法正常工作。有趣的是,現在至少有一些點具有正確的水平對齊。 – user26665 2015-02-06 20:03:00

+0

此外,無論何時進行繪圖,我會得到以下警告: '警告級別< - (* tmp *,value = if(nl == nL)as.character(labels)else paste0(labels,: duplicate因素水平不推薦使用 – user26665 2015-02-06 20:21:18