R ggplot2:设置其他特定轴刻度线 [英] R ggplot2: Setting additional specific axis tick marks

查看:78
本文介绍了R ggplot2:设置其他特定轴刻度线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ggplot:

I have a ggplot:

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

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

I want to keep the default axis breaks and labels (In my program, I do not know the limits of the plot a priori.)

在x = 1.5时,我想在x轴上添加一个带有标签"hi"的刻度线.

At x= 1.5 I want to add an additional tick mark to the x-axis with label "hi".

我了解 scale_x_continuous(),但是我不知道如何访问由转换对象计算的默认中断".

I know about scale_x_continuous(), but I do not know how to access the "default breaks computed by the transformation object".

推荐答案

ggplot2 使用基本函数 pretty (间接通过 scales :: pretty_breaks >)用于非转换轴.利用这个优势:

ggplot2 uses the base function pretty (indirectly through scales::pretty_breaks) for non-transformed axes. Use this to your advantage:

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时,它当然会超绘(您写"additional"而不是"replace",所以我不确定您追求的是什么).如果您不想这样做,则需要执行以下操作:

At 1.5 it will overplot of course (you write 'additional', not 'replace', so I'm not sure what you're after). If you don't want that, you'll need to do something like:

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

这篇关于R ggplot2:设置其他特定轴刻度线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆