【问题标题】:ggplot2: boxplot with all points distributed evenly in a rowggplot2:所有点均匀分布在一行中的箱线图
【发布时间】:2023-04-06 18:04:01
【问题描述】:

我有一个使用以下代码生成的箱线图:

b.males <- c(6, 7, 8, 8, 8, 9, 10, 10, 11, 11, 12, 12, 12, 12, 13, 14, 15)
b.females <- c(14, 13, 12, 12, 11, 10, 10, 9, 9, 9, 9, 9, 8, 8, 8, 7, 7, 7, 7)
b.total<-c(b.males,b.females)

b.m<-data.frame(b.males)
b.f<-data.frame(b.females)
b.t<-data.frame(b.total)

myList<-list(b.m, b.f, b.t)
df<-melt(myList)

colnames(df) <- c("class","count")
plt<-ggplot(df, aes(x=class,y=count))+geom_boxplot() 
plt + geom_point(aes(x = as.numeric(class) + 0, colour=class))

我想做的是,对于任何给定的 y 轴点,连续显示所有单独的点。例如,对于 b.males,我希望在 8 处看到 3 个点,中间的点正好在中心,另外两个点就在它旁边。

我尝试过:

plt + geom_point(aes(x = as.numeric(class) + 0, colour=class)) +
      geom_jitter(position=position_jitter(width=.1, height=0))

但这并没有使这些点保持紧密。此外,在某些情况下,它会将多个点放在框中间的右侧或左侧,而不是按照我的意愿均匀分布。

【问题讨论】:

    标签: r ggplot2 boxplot


    【解决方案1】:

    您可以使用 geom_dotplot() 添加点 - 使用参数 binaxis="y" 您的值根据 y 值 (counts) 和参数 stackdir="center" 确保点居中。要更改点的大小,请使用参数dotsize=

    ggplot(df,aes(class,count))+geom_boxplot()+
      geom_dotplot(aes(fill=class),binaxis="y",stackdir="center",dotsize=0.5)
    

    【讨论】:

    • 非常好,正是我想要的。谢谢。
    猜你喜欢
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多