【问题标题】:Assign points to a specific range on x-axis将点分配给 x 轴上的特定范围
【发布时间】:2022-01-06 23:39:40
【问题描述】:

我用 ggplot2 生成了这张图,(我手动添加了红色矩形)

我想以某种方式“标记” x 轴间隔。

例如;从 1.45e+08 开始。到 1.46e+08 被命名为“低”,1.46e+08。到 1.47e+08 为“中”,我只想在 x 轴上显示这个标签而不是值。

我有它所属的标签/间隔的每个点的列表,如果有用,还有该范围的起始点和结束点的间隔。

我在生成图表时使用了这段代码

ggplot(erpeaks, aes(x=pos, y=score), position=position_jitter(w=0,h=0)) + 
  geom_point(stat = "identity", width=0.5, aes(colour = factor(label)))  +
  theme(plot.title=element_text(hjust=0.5))

我试图添加这个,但他只适用于确定间隔

 coord_cartesian(xlim = c(144018895.5,146957774.5))

还有这个,但这没有给出结果。

scale_x_discrete(c(144018895.5,146957774.5),labels = c("low")) 

谢谢。

【问题讨论】:

    标签: r ggplot2 scatter-plot x-axis geom-point


    【解决方案1】:

    您可以使用facet_grid() 中的cut() 函数在某些方面显示某些逆向。

    library(ggplot2)
    #> Warning: package 'ggplot2' was built under R version 4.1.1
    
    ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) +
      geom_point() +
      facet_grid(~ cut(Sepal.Width, c(-Inf, 2.75, 3.75, Inf)),
                 scales = "free_x", space = "free_x")
    

    如果您想分配不同的标签,您可以预先重新标记剪切。

    df <- iris
    df$facet <- cut(df$Sepal.Width, c(-Inf, 2.75, 3.75, Inf))
    levels(df$facet) <- c("low", "medium", "high")
    
    ggplot(df, aes(Sepal.Width, Sepal.Length, colour = Species)) +
      geom_point() +
      facet_grid(~ facet,
                 scales = "free_x", space = "free_x")
    

    reprex package (v2.0.1) 于 2021 年 11 月 30 日创建

    【讨论】:

    • 你的意思是像下面这样吗? + geom_hline(yintercept = your_interval_breaks, linetype = "dashed")
    • 成功了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 2021-07-10
    • 2018-05-09
    • 1970-01-01
    相关资源
    最近更新 更多