Add horizontal lines in categorical scatter plot using ggplot2 in R

我的梦境 提交于 2019-12-12 13:24:47

问题


I am trying to plot a simple scatter plot for 3 groups, with different horizontal lines (line segment) for each group: for instance a hline at 3 for group "a", a hline at 2.5 for group "b" and a hline at 6 for group "c".

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))

I really appreciate your help!


回答1:


Never send a line when a point can suffice:

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))

There are other ways if this isn't acceptable, such as:

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))

The downside for the point is the egregious thickness and no control over width.

The downside for the segment is the need to use numerics for the discrete axis position vs the factors.

I also should have set the random seed to ensure reproducibility.




回答2:


Thanks it works fine. However, the second solution doesn't work, when I use a bubble chart.

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))

Error in eval(expr, envir, enclos) : object 's' not found



来源:https://stackoverflow.com/questions/39601360/add-horizontal-lines-in-categorical-scatter-plot-using-ggplot2-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!