我生成图表的一些数据,但蜱虫的数量太少,我需要读取更精确 。
是否有某种方式来增加在GGPLOT2轴刻度的数量?
我知道我可以告诉ggplot使用矢量轴蜱,但我要的是增加刻度线的数量,对所有数据。 换句话说,我希望从该数据计算出的节拍数。
可能ggplot一些算法做内部,但是我无法找到它是怎么做的,按照我想要的改变。
我生成图表的一些数据,但蜱虫的数量太少,我需要读取更精确 。
是否有某种方式来增加在GGPLOT2轴刻度的数量?
我知道我可以告诉ggplot使用矢量轴蜱,但我要的是增加刻度线的数量,对所有数据。 换句话说,我希望从该数据计算出的节拍数。
可能ggplot一些算法做内部,但是我无法找到它是怎么做的,按照我想要的改变。
您可以通过修改覆盖ggplots默认秤scale_x_continuous
和/或scale_y_continuous
。 例如:
library(ggplot2)
dat <- data.frame(x = rnorm(100), y = rnorm(100))
ggplot(dat, aes(x,y)) +
geom_point()
给你这样的:
和压倒一切的尺度可以给你这样的事情:
ggplot(dat, aes(x,y)) +
geom_point() +
scale_x_continuous(breaks = round(seq(min(dat$x), max(dat$x), by = 0.5),1)) +
scale_y_continuous(breaks = round(seq(min(dat$y), max(dat$y), by = 0.5),1))
如果你想在一个情节中的特定部分简单地“放大”,看xlim()
和ylim()
分别。 良好的洞察力也可以找到这里来了解其他参数也是如此。
您可以使用内置的pretty
功能:
ggplot(dat, aes(x,y)) + geom_point() +
scale_x_continuous(breaks = pretty(dat$x, n = 10)) +
scale_y_continuous(breaks = pretty(dat$y, n = 10))
根据丹尼尔Krizian的评论 ,你还可以使用pretty_breaks
从功能scales
库,它会自动导入:
ggplot(dat, aes(x,y)) + geom_point() +
scale_x_continuous(breaks = scales::pretty_breaks(n = 10)) +
scale_y_continuous(breaks = scales::pretty_breaks(n = 10))
所有你需要做的是插入节拍数通缉。
你可以提供一个功能参数scale
,并ggplot将使用该函数来计算刻度位置。
library(ggplot2)
dat <- data.frame(x = rnorm(100), y = rnorm(100))
number_ticks <- function(n) {function(limits) pretty(limits, n)}
ggplot(dat, aes(x,y)) +
geom_point() +
scale_x_continuous(breaks=number_ticks(10)) +
scale_y_continuous(breaks=number_ticks(10))
另外,
ggplot(dat, aes(x,y)) +
geom_point() +
scale_x_continuous(breaks = seq(min(dat$x), max(dat$x), by = 0.05))
用于装箱或离散缩放x轴数据有效(即,舍去没有必要)。