[英]Issue using position dodge vertically in ggplots with geom_linerange in R
I'm relatively new to R, and I am having issues with avoiding overlapping line ranges using geom_linerange and position_dodge with ggplot.我对 R 比较陌生,我在使用 geom_linerange 和 position_dodge 与 ggplot 避免重叠线范围时遇到问题。 I am comparing 2 different sets of age ranges for each individual in the study data.我正在比较研究数据中每个人的 2 组不同的年龄范围。 I would like to show the two ranges side-by-side, rather than display on the same ID line.我想并排显示两个范围,而不是显示在同一 ID 行上。 Here is the data:这是数据:
> dput(data)
structure(list(ID = c("A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z"), Age_Range1_Start = c(39L, 21L, 28L,
35L, 35L, 20L, 21L, 28L, 20L, 29L, 28L, 20L, 49L, 20L, 39L, 21L,
39L, 21L, 20L, 28L, 30L, 29L, 21L, 28L, 29L, 35L), Age_Range1_End = c(69L,
42L, 52L, 57L, 57L, 43L, 42L, 52L, 43L, 44L, 52L, 43L, 65L, 43L,
69L, 42L, 69L, 42L, 43L, 52L, 54L, 44L, 42L, 52L, 44L, 57L),
Age_Range2_Start = c(46L, 43L, 49L, 46L, 48L, 34L, 37L, 45L,
44L, 46L, 37L, 43L, 51L, 45L, 48L, 36L, 53L, 44L, 24L, 43L,
45L, 49L, 34L, 45L, 22L, 30L), Age_Range2_End = c(87L, 80L,
92L, 86L, 90L, 64L, 69L, 83L, 81L, 85L, 68L, 80L, 97L, 84L,
89L, 68L, 100L, 83L, 45L, 82L, 83L, 92L, 64L, 84L, 42L, 56L
)), class = "data.frame", row.names = c(NA, -26L))
>
Here is the R code I used这是我使用的 R 代码
ggplot(data) + theme_minimal() + coord_flip() +
geom_linerange(aes(x=ID, ymin=Age_Range1_Start, ymax=Age_Range1_End, color="Method 1", size=1.5)) +
geom_linerange(aes(x=ID, ymin=Age_Range2_Start, ymax=Age_Range2_End, color="Method 2", size=1.5), position=position_dodge(width=1)) +
labs(x="ID", y="Age Range", title="Method Age Range Comparison")
It produces the following plot:它产生以下情节:
Is it possible to shift (or dodge) the line ranges for Method 2 such that they appear slightly below or inferior to the age ranges for Method 1?是否可以移动(或躲避)方法 2 的线条范围,使其看起来略低于或低于方法 1 的年龄范围? I do not understand why the position_dodge code does not shift the values.我不明白为什么 position_dodge 代码不改变值。
Thank you in advance for any help or suggestions.预先感谢您提供任何帮助或建议。
If you organize your data in another format you can do the trick, as follows:如果您以另一种格式组织数据,则可以执行以下操作:
library(ggplot2)
data = data.frame(
ID = rep(c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'), 2),
Start = c(39, 21, 28, 35, 35, 20, 21, 28, 46, 43, 49, 46, 48, 34, 37, 45),
End = c(69, 42, 52, 57, 57, 43, 42, 52, 87, 80, 92, 86, 90, 64, 69, 83),
Method = c(rep(1, 8), rep(2, 8))
)
data$Method = as.factor(data$Method)
ggplot(data) +
theme_minimal() +
coord_flip() +
geom_linerange(aes(x = ID, ymin = Start, ymax = End, colour = Method),
size = 1.5, position = position_dodge2(width = 0.5)) +
labs(x = 'ID', y = 'Age Range', title = 'Method Age Range Comparison')
Here is the output:这是输出:
Note that I gather the starts together and the ends together.请注意,我将起点和终点放在一起。 Then, I labeled each of them as method 1 or 2.然后,我将它们分别标记为方法 1 或 2。
Also remember using position_dodge2()
when you flip the coordinates.还记得在翻转坐标时使用position_dodge2()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.