简体   繁体   English

在ggplot的连续轴中设置值之间的中断

[英]Set breaks between values in continuous axis of ggplot

How can we set the breaks of continous axis every 2 numbers if we have a very big range of values in an axis and cannot set it manually?如果我们在一个轴上有很大范围的值并且不能手动设置它,我们如何设置每 2 个数字的连续轴的中断?

p1 <- ggplot(mpg, aes(displ, hwy)) +
  geom_point()
p1 + scale_x_continuous(breaks = c(2, 4, 6))

From the ?scale_x_continuous help page, breaks can be (among other options)?scale_x_continuous帮助页面中,可以(以及其他选项) breaks

A function that takes the limits as input and returns breaks as output A function 将限制作为输入并返回中断为 output

Here's an anonymous function going from the (floored) min to the (ceilinged) max by 2:这是一个匿名的 function 从(地板)最小值到(天花板)最大值 2:

ggplot(mpg, aes(displ, hwy)) +
  geom_point() + 
  scale_x_continuous(breaks = \(x) seq(floor(x[1]), ceiling(x[2]), by = 2))

Alternately you could still use seq for finer control, more customizable, less generalizable:或者,您仍然可以使用seq进行更精细的控制、更可定制、更不通用:

ggplot(mpg, aes(displ, hwy)) +
  geom_point() + 
  scale_x_continuous(breaks = seq(2, 6, by = 2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM