我正在为一些数据生成曲线图,但刻度数太少,我需要更精确的读数。
有什么方法可以增加ggplot2中轴刻度的数量吗?
我知道我可以告诉ggplot使用一个向量作为轴刻度,但我想要的是增加所有数据的刻度数。换句话说,我希望从数据中计算出刻度数。
也许ggplot在内部使用了一些算法,但我找不到它是如何做到这一点的,根据我想要的改变。
发布于 2012-07-05 06:15:37
您可以通过修改scale_x_continuous
和/或scale_y_continuous
来覆盖ggplots的默认比例。例如:
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()
。也可以在here中找到好的洞察力来理解其他论点。
发布于 2015-07-14 21:45:47
基于Daniel Krizian's comment,您还可以使用自动导入的scales
库中的pretty_breaks
函数:
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))
您所要做的就是插入n
所需的刻度数。
一个稍微不太有用的解决方案(因为您必须再次指定数据变量),您可以使用内置的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))
发布于 2013-06-23 11:16:15
您可以向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))
https://stackoverflow.com/questions/11335836
复制相似问题