在ggplot2中使所有刻度标记之间的距离相等。

huangapple go评论57阅读模式
英文:

make distance between all tick marks equal in ggplot2

问题

我已经创建了以下图表:

在ggplot2中使所有刻度标记之间的距离相等。

但是,我不喜欢数字4、12和24上的刻度标记看起来挤在一起的事实。是否有办法让所有刻度标记之间的距离相等,而不考虑与之关联的数字?

这是我用来制作这个图表的数据:

# 数据示例
# A tibble: 10 × 3
 hour  mean   SE
<dbl> <dbl> <dbl>
   4  1.02 0.00568
   8  0.83 0.00671
  12  0.76 0.00831
  24  0.68 0.0129 
  48  0.6  0.0116 
  72  0.56 0.0247 
  96  0.57 0.0259 
 120  0.6  0.0548 
 144  0.55 0.0429 
 168  0.42 0.0243 

以下是我用来生成图表的代码:

# 代码示例
library(ggplot2)    
ggplot(data3, aes(x = hour, y = mean)) +
      geom_errorbar(aes(ymin=mean-SE, ymax=mean+SE), width=.2) +
      geom_point() +
      geom_line() +
      ylab("平均速度(km/h)") +
      scale_x_continuous("两地之间的小时数", breaks = breaks) +
      theme_classic()
英文:

I have created the following graph:

在ggplot2中使所有刻度标记之间的距离相等。

However, I do not like the fact that the tick marks with the numbers 4,12 and 24 look scrunched together. Is there a way for me to make the distance between all tick marks equal, regardless of the numbers associated with them?

Here is the data I used to make this graph:

# A tibble: 10 × 3
hour  mean   SE
<dbl> <dbl> <dbl>
  4  1.02 0.00568
  8  0.83 0.00671
 12  0.76 0.00831
 24  0.68 0.0129 
 48  0.6  0.0116 
 72  0.56 0.0247 
 96  0.57 0.0259 
120  0.6  0.0548 
144  0.55 0.0429 
168  0.42 0.0243 

and here is the code I used to generate the graph:

library(ggplot2)    
ggplot(data3, aes(x = hour, y = mean)) +
      geom_errorbar(aes(ymin=mean-SE, ymax=mean+SE), width=.2) +
      geom_point() +
      geom_line()+
      ylab("Mean speed (km/h)") +
      scale_x_continuous("Hours between locations", breaks = breaks) +
      theme_classic()

答案1

得分: 2

这通常不是一个好主意,将连续刻度更改为以你所寻求的任意间距方式使用 - 结果可视化将无法准确显示随时间变化的速率。

而是,以下是一些替代方法,您可以单独使用或组合使用,以清除断点,同时保持刻度并覆盖左侧的较小增量。

使用主要和次要网格线:

library(ggplot2)

breaks <- seq(0, 168, by = 24)
minor_breaks <- seq(0, 168, by = 4)

p <- ggplot(data3, aes(x = hour, y = mean)) +
  geom_errorbar(aes(ymin=mean-SE, ymax=mean+SE), width=.2) +
  geom_point() +
  geom_line() +
  ylab("平均速度(公里/小时)")

p +
  scale_x_continuous("位置之间的小时数", breaks = breaks, minor_breaks = minor_breaks) +
  theme_bw()

或者主要和次要坐标轴刻度:

library(ggh4x)

p +
  scale_x_continuous(
   "位置之间的小时数", 
   breaks = breaks, 
   minor_breaks = minor_breaks,
   guide = "axis_minor"
  ) +
  theme_classic()

或者直接标记数据点:

p +
  geom_text(
    aes(label = hour), 
	size = 8/.pt,
	nudge_x = 5, 
	nudge_y = 0.025,
  ) +
  scale_x_continuous(
   "位置之间的小时数", 
   breaks = breaks
  ) +
  theme_classic()

或者结合这三种方法:

p +
  geom_text(
    aes(label = ifelse(hour %% 24 != 0, paste0(hour, "h"), "")), 
	size = 8/.pt,
	nudge_x = 7.5
  ) +
  scale_x_continuous(
   "位置之间的小时数", 
   breaks = breaks, 
   minor_breaks = minor_breaks,
   guide = "axis_minor"
  ) +
  theme_bw()
英文:

It’s generally a bad idea to change a continuous scale to use arbitrary spacing in the way you’re looking to — the resulting viz won’t accurately show the rate of change over time.

Instead, here are some alternatives you can use, alone or in combination, to clean up the breaks while maintaining scale and covering the smaller increments on the left.

Use major and minor gridlines:

library(ggplot2)

breaks <- seq(0, 168, by = 24)
minor_breaks <- seq(0, 168, by = 4)

p <- ggplot(data3, aes(x = hour, y = mean)) +
  geom_errorbar(aes(ymin=mean-SE, ymax=mean+SE), width=.2) +
  geom_point() +
  geom_line()+
  ylab("Mean speed (km/h)") 

p +
  scale_x_continuous("Hours between locations", breaks = breaks, minor_breaks = minor_breaks) +
  theme_bw()

在ggplot2中使所有刻度标记之间的距离相等。

Or major and minor axis ticks:

library(ggh4x)

p +
  scale_x_continuous(
   "Hours between locations", 
   breaks = breaks, 
   minor_breaks = minor_breaks,
   guide = "axis_minor"
  ) +
  theme_classic()

在ggplot2中使所有刻度标记之间的距离相等。

Or label the points directly:

p +
  geom_text(
    aes(label = hour), 
	size = 8/.pt,
	nudge_x = 5, 
	nudge_y = 0.025,
  ) +
  scale_x_continuous(
   "Hours between locations", 
   breaks = breaks
  ) +
  theme_classic()

在ggplot2中使所有刻度标记之间的距离相等。

Or combine all three approaches:

p +
  geom_text(
    aes(label = ifelse(hour %% 24 != 0, paste0(hour, "h"), "")), 
	size = 8/.pt,
	nudge_x = 7.5
  ) +
  scale_x_continuous(
   "Hours between locations", 
   breaks = breaks, 
   minor_breaks = minor_breaks,
   guide = "axis_minor"
  ) +
  theme_bw()

在ggplot2中使所有刻度标记之间的距离相等。

huangapple
  • 本文由 发表于 2023年6月9日 08:03:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定