R { ggplot2 } 是否可以查询绘图的刻度线是什么?

发布于 2024-11-17 19:52:15 字数 731 浏览 7 评论 0原文

使用 Hadley 网站 中的示例:

> (m <- qplot( rating, votes, data=subset(movies, votes > 1000), na.rm = T))

创建:

examplefigure

我的问题:创建绘图对象后是否可以确定刻度线标记的内容? (我想删除第一个自动生成的断点)

背景:在上图中,可以清楚地看到 x 轴断点位于 2 到 9 处。要手动获取此断点,请使用:

< code>m +scale_x_continuous(breaks = c(2:9))

但我想从图中确定刻度线是什么,以便我可以删除其中一些刻度线。换句话说,是否有一个函数会返回刻度线:

myBreaks <-tickMarks(m)

以便我可以随后调用:

m + scale_x_continuous(breaks = myBreaks[-1 ] )

我已经从数组中删除了第一个中断。

Using the example from Hadley's website:

> (m <- qplot(rating, votes, data=subset(movies, votes > 1000), na.rm = T))

Which creates:

example figure

My Question: Is it possible to determine what the ticks marks after creating the plot object? (I want to remove the first auto-generated breakpoint)

Background: In the above plot, one can clearly see that the x-axis breaks are at 2 through 9. To obtain this manually, use:

m + scale_x_continuous( breaks = c(2:9) )

But I would like to determine, from the figure, what the tick marks are so that I can remove some of them. In other words, is there a function which will return the tick marks:

myBreaks <- tickMarks(m)

So that I can subsequently call:

m + scale_x_continuous( breaks = myBreaks[-1] )

where I've removed the first break from the array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水微香 2024-11-24 19:52:15

我不确定这是否是您想要的,但您可以通过以下方式进行破解:

# drop first break
sx <- scale_x_continuous()
sx$.tr$input_breaks <- function(., range) grid.pretty(range)[-1]
m <- qplot(rating, votes, data=subset(movies, votes > 1000), na.rm = T)
m + sx

# reduce the breaks into half
sx$.tr$input_breaks <- function(., range) {
  r <- grid.pretty(range); r[seq_len(length(r)/2)*2]
}
m + sx

# set the (rough) number of breaks  
sx$.tr$input_breaks <- function(., range) pretty(range, 3)
m + sx

但请注意,这也会影响 y 轴...

并且可能这是制作您自己的变换对象的简单方法。

TransIdentity2 <- Trans$new("identity2", "force", "force", "force")
TransIdentity2$input_breaks <- function(., range) pretty(range, 3)

m + scale_x_continuous(trans="identity2")

在这种情况下,它不会影响 y 轴。

I'm not sure this is what you want, but you can do a hack by:

# drop first break
sx <- scale_x_continuous()
sx$.tr$input_breaks <- function(., range) grid.pretty(range)[-1]
m <- qplot(rating, votes, data=subset(movies, votes > 1000), na.rm = T)
m + sx

# reduce the breaks into half
sx$.tr$input_breaks <- function(., range) {
  r <- grid.pretty(range); r[seq_len(length(r)/2)*2]
}
m + sx

# set the (rough) number of breaks  
sx$.tr$input_breaks <- function(., range) pretty(range, 3)
m + sx

but note that this also affects y-axis...

And probably it is the easy way to make your own transform object.

TransIdentity2 <- Trans$new("identity2", "force", "force", "force")
TransIdentity2$input_breaks <- function(., range) pretty(range, 3)

m + scale_x_continuous(trans="identity2")

in this case, it does not affect y-axis.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文