【问题标题】:R ggplot2: Setting additional specific axis tick marksR ggplot2:设置额外的特定轴刻度线
【发布时间】:2018-06-25 08:38:48
【问题描述】:

我有一个ggplot:

ggplot()+geom_line(data = data.frame(y = c(1,2,3), x=c(1,2,3)), aes(y=y,x=x))

我想保留默认的轴中断和标签(在我的程序中,我不知道先验图的限制。)

在 x= 1.5 时,我想在 x 轴上添加一个额外的刻度线,标签为“hi”。

我知道scale_x_continuous(),但我不知道如何访问“由转换对象计算的默认中断”。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    ggplot2 使用基本函数pretty(间接通过scales::pretty_breaks)用于非转换轴。充分利用这一点:

    df <- data.frame(y = c(1,2,3), x=c(1,2,3))
    
    ggplot(df, aes(x, y)) + 
      geom_line() +
      scale_x_continuous(breaks = c(pretty(df$x), 1.5), labels = c(pretty(df$x), 'hi'))
    

    在 1.5 时,它当然会过度绘制(你写的是“附加”,而不是“替换”,所以我不确定你在追求什么)。如果您不希望这样做,则需要执行以下操作:

    pretty_br <- pretty(df$x)[abs(pretty(df$x) - 1.5) > 0.25]
    ggplot(df, aes(x, y)) + 
      geom_line() +
      scale_x_continuous(breaks = c(pretty_br, 1.5), labels = c(pretty_br, 'hi'))
    

    【讨论】:

    • 另外,我的意思是“替换”如果已经有标签,如果没有,则添加。因此,第二个选项就可以了。 (本来可以更清楚的!)谢谢!
    • 你会如何处理一个有切面的情节?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多