【问题标题】:ggplot specify position of vertical segments for categorical x rggplot 为分类 x r 指定垂直段的位置
【发布时间】:2015-04-24 17:06:43
【问题描述】:

我正在绘制行数据,并为统计计算的拟合值添加了一段可信区间和一个黑点。

我的问题是我想让这些线(和黑点)相对于行数据稍微(水平)移动。

我尝试了 jitter 和它的所有可能组合,结果很糟糕,因为我从不同的列中获取了 y start 和 end 的值......因此,jitter 线条不再是水平的。

我尝试向 x (pb_values) 添加一个固定值,但它给了我一个错误,因为我的 x 是一个因素。

dat_stack1(我的数据集):

dat_stack1<-data.frame(pb_cluster= rep(3:5, 6))
dat_stack1$pb_cluster<-factor(dat_stack1$pb_cluster)
dat_stack1$total<-c(28,  12,   3, 326,  14, 125,  74,  40, 115, 382,  70,  36,  36,  23,  28,185,19,107)
dat_stack1$couple<-factor(c(1, 1,  1,  2,  2,  2,  5,  5,  5,  8,  8,  8,  9,  9,  9,  11, 11, 11))
dat_stack1$lower<-c(55, 0.1851138,  8.9413495, 55.5002200,  0.1851138,  8.9413495, 55.5002200,  0.1851138,  8.9413495, 55.5002200,  0.1851138,  8.9413495, 55.5002200,  0.1851138,  8.9413495, 55.5002200,  0.1851138,  8.9413495)
dat_stack1$upper<-c(225.47047,  68.04097, 114.92182, 225.47047,  68.04097, 114.92182, 225.47047,  68.04097, 114.92182, 225.47047,68.04097,114.92182, 225.47047,  68.04097, 114.92182, 225.47047,  68.04097, 114.92182)
dat_stack1$fit<-c(124.93260,  18.87026, 46.84022,124.93260, 18.87026,46.84022,124.93260, 18.87026, 46.84022, 124.93260, 18.87026,  46.84022,124.93260,  18.87026,  46.84022, 124.93260 ,18.87026 ,46.84022)


g<-g_stack<-ggplot(data=dat_stack1, aes(x=pb_cluster,  y = total, shape=couple))+
geom_point()+
scale_x_discrete(limits=c("3","4","5"),                                                                                        labels=c("mate", "familiar","unfamiliar"))+
theme(axis.text.x  = element_text(angle=90))+
geom_segment(aes(x=pb_cluster, xend=pb_cluster, y = lower, yend = upper))+
geom_point(aes(x=pb_cluster,  y = fit),color="black")

不幸的是,我的声誉太低,无法发布图片!但代码现在可以重现

知道如何移动这些垂直线吗?

我尝试了什么:

geom_segment(aes(x=pb_cluster, xend=pb_cluster,
             y = lower, yend = upper)+position="jitter")

geom_segment(aes(x=pb_cluster +0.1, xend=pb_cluster+0.1, 
             y = lower, yend = upper))

以及所有可能的括号组合。

当然我在网上找了类似的图表,但没有找到-->通常垂直线是直接从数据点计算出来的,而不是添加到不同列的行数据中!

【问题讨论】:

    标签: r ggplot2 jitter credible-interval


    【解决方案1】:

    如果您的 x 值确实是因子,那么您可以在 geom_segment() 和最后一个 geom_point() 中的 x 值周围使用 as.numeric(),以便能够添加一些小常数,然后移动线和点。

    ggplot(data=dat_stack1, aes(x=pb_cluster,  y = total, shape=couple))+
          geom_point()+
          scale_x_discrete(limits=c("3","4","5"),labels=c("mate", "familiar","unfamiliar"))+
          theme(axis.text.x  = element_text(angle=90))+
          geom_segment(aes(x=as.numeric(pb_cluster)+.1, xend=as.numeric(pb_cluster)+.1,
                                                      y = lower, yend = upper))+
          geom_point(aes(x=as.numeric(pb_cluster)+0.1,  y = fit),color="black")
    

    【讨论】:

    • 好的,谢谢,我试过了,但只用numeric而不是as.numeric,我收到以下错误:Error in numeric(pb_cluster) : invalid 'length' argument
    • 在数值图中绘制分类变量的出色解决方法。应该能够在许多其他几何图形中使用这种方法。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多