R {ggplot2}是否可以查询绘图的刻度标记? [英] R { ggplot2 } Is it possible to query what the tick marks are for a plot?
问题描述
使用哈德利网站中的示例:
> (m <-qplot(rating,votes,data = subset(movies,votes votes> 1000),na.rm = T))
<
p>
我的问题:是否可以确定创建绘图对象后标记的内容? (我想删除第一个自动生成的断点)
背景:在上面的图中,可以清楚地看到x轴你可以使用以下命令:
m + scale_x_continuous(breaks = c(2:9)) code>
但是我想从图中确定刻度线是什么,以便我可以删除其中的一些。换句话说,是否有一个函数会返回刻度标记:
myBreaks < - tickMarks(m)
这样我可以随后调用:
$ p $ m $ scale_x_continuous(breaks = myBreaks [-1])
其中我已经从数组中删除了第一个break。
<$ p $ ()函数(。,范围)grid.pretty(范围)[ - ]函数(。,范围) 1]
m < - qplot(rating,votes,data = subset(movies,votes> 1000),na.rm = T)
m + sx
#分为一半
sx $ .tr $ input_breaks< - function(。,range){
r< - grid.pretty(range); r [seq_len(length(r)/ 2)* 2]
}
m + sx
#设置中断的(粗略)数量
sx $ .tr $ input_breaks< - function(。,range)pretty(range,3)
m + sx
<但请注意,这也会影响y轴......
可能这是制作自己的转换对象的简单方法。
TransIdentity2 < - Trans $ new(identity2,force,force,force)
TransIdentity2 $ input_breaks< - 函数(。,范围)漂亮(范围,3)
m + scale_x_continuous(trans =identity2)
在这种情况下,它不会影响y轴。
Using the example from Hadley's website:
> (m <- qplot(rating, votes, data=subset(movies, votes > 1000), na.rm = T))
Which creates:
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.
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.
这篇关于R {ggplot2}是否可以查询绘图的刻度标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!