我试图为3组绘制一个简单的散点图,每组有不同的水平线(线段):例如,对于组“a”,hline为3,对于组“b”,hline为2.5小组“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))
我真的很感谢你的帮助!
答案 0 :(得分:1)
当一个点足够时,永远不要发送一条线:
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))
如果这是不可接受的,还有其他方法,例如:
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))
这一点的缺点是严重的厚度,无法控制宽度。
该细分市场的缺点是需要使用数值作为离散轴位置与因子。
我也应该设置随机种子以确保重现性。
答案 1 :(得分:0)
谢谢它工作正常。但是,当我使用气泡图时,第二种解决方案不起作用。
df <- data.frame(tt = rep(c("a","b","c"),40),
val = round(rnorm(120, m = rep(c(4, 5, 7), each = 40))),s=rep(c(1,10,5,50),each=30))
hline <- data.frame(tt=c(1, 2, 3), v=c(3, 2.5, 6))
ggplot(df, aes(tt, val,size=s))+
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))
eval(expr,envir,enclos)中的错误:object&#39; s&#39;找不到