Ann*_*nna 6 r scatter line ggplot2 categorical-data
我正在尝试为 3 个组绘制一个简单的散点图,每个组具有不同的水平线(线段):例如,组“a”的 hline 为 3,组“b”的 hline 为 2.5,hline 为组“b”。 “c”组为 6。
library(ggplot2)
df <- data.frame(tt = rep(c("a","b","c"),40),
val = round(rnorm(120, m = rep(c(4, 5, 7), each = 40))))
ggplot(df, aes(tt, val))+
geom_jitter(aes(tt, val), data = df, colour = I("red"),
position = position_jitter(width = 0.05))
Run Code Online (Sandbox Code Playgroud)
我真的很感谢你的帮助!
当一个点就足够时,永远不要发送一行:
library(ggplot2)
df <- data.frame(tt = rep(c("a","b","c"),40),
val = round(rnorm(120, m = rep(c(4, 5, 7), each = 40))))
hline <- data.frame(tt=c("a", "b", "c"), v=c(3, 2.5, 6))
ggplot(df, aes(tt, val))+
geom_point(data=hline, aes(tt, v), shape=95, size=20) +
geom_jitter(aes(tt, val), data = df, colour = I("red"),
position = position_jitter(width = 0.05))
Run Code Online (Sandbox Code Playgroud)
如果这不可接受,还有其他方法,例如:
hline <- data.frame(tt=c(1, 2, 3), v=c(3, 2.5, 6))
ggplot(df, aes(tt, val))+
geom_jitter(aes(tt, val), data = df, colour = I("red"),
position = position_jitter(width = 0.05)) +
geom_segment(data=hline, aes(x=tt-0.25, xend=tt+0.25, y=v, yend=v))
Run Code Online (Sandbox Code Playgroud)
该点的缺点是厚度过大且无法控制宽度。
该段的缺点是需要使用数字来表示离散轴位置与因子。
我还应该设置随机种子以确保可重复性。