![](/img/trans.png)
[英]How to centre single bar position with multiple bars in position_dodge in ggplot2
[英]ggplot position_dodge with multiple error bars
對於兩個分類變量的每個交叉分類,我有五個數據點。 我試圖在誤差條之間添加一些均勻的間距,以便它們在刻面的ggplot2圖中不重疊,但是失敗了。 數據很像這樣......
library(ggplot2)
library(dplyr)
df0 <- iris %>%
group_by(Species) %>%
mutate(long_sepal = ifelse(Sepal.Length > mean(Sepal.Length),
yes = "long", no = "short")) %>%
group_by(Species, long_sepal) %>%
mutate(petal_rank = order(Petal.Width)) %>%
filter(petal_rank <= 5)
> df0
# Source: local data frame [30 x 7]
# Groups: Species, long_sepal [6]
#
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species long_sepal petal_rank
# (dbl) (dbl) (dbl) (dbl) (fctr) (chr) (int)
# 1 5.4 3.9 1.7 0.4 setosa long 1
# 2 4.6 3.4 1.4 0.3 setosa short 1
# 3 5.0 3.4 1.5 0.2 setosa short 2
# 4 4.4 2.9 1.4 0.2 setosa short 3
# 5 4.9 3.1 1.5 0.1 setosa short 4
# 6 5.4 3.7 1.5 0.2 setosa long 3
# 7 5.8 4.0 1.2 0.2 setosa long 4
# 8 5.2 4.1 1.5 0.1 setosa long 2
# 9 5.5 4.2 1.4 0.2 setosa long 5
# 10 4.5 2.3 1.3 0.3 setosa short 5
# .. ... ... ... ... ... ... ...
到目前為止繪制代碼。
ggplot(data = df0,
aes(x = long_sepal, y = Petal.Width,
ymin = Petal.Width-0.05,
ymax = Petal.Width+0.05)) +
geom_pointrange(position = position_dodge(width = 0.4)) +
facet_wrap(~ Species, scales = "free")
我已經嘗試過使用width參數,但無論值如何,我都會得到相同的圖。
我之前使用過position = position_jitter()
來避免點重疊,我認為這可能是一個解決方案。
試試:
ggplot(data = df0,
aes(x = long_sepal, y = Petal.Width,
ymin = Petal.Width-0.05,
ymax = Petal.Width+0.05)) +
geom_pointrange(position = position_jitter()) +
facet_wrap(~ Species, scales = "free")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.