简体   繁体   English

如何在ggplot2中重新排序数字x轴?

[英]How to reorder a numeric x-axis in ggplot2?

First time posting, so please forgive any errors I make.第一次发帖,如有错误请见谅。

I'm attempting to plot some monthly values over the course of a year, starting in July.我正在尝试从 7 月开始绘制一年中的一些月度值。 . . Here is some sample data:以下是一些示例数据:

x <- seq(1, 12, 1)
set.seed(2022)
y <- rnorm(n = 12)

df <- data.frame("x" = x, "y" = y)

ggplot(data = df, aes(x = x, y = y))+
geom_line()

In this case, I would like to start the x-axis in July (x = 7).在这种情况下,我想在 7 月开始 x 轴(x = 7)。 This is easy enough if I convert my x-axis variables to a factor.如果我将 x 轴变量转换为因子,这很容易。 But, I need to keep the x-axis as a numeric scale because I'm attempting to use geom_tile to plot a sort of nominal color scale in the background, like so:但是,我需要将 x 轴保持为数字刻度,因为我正在尝试使用 geom_tile 在背景中绘制一种标称色标,如下所示:

tile.df <- data.frame(
"x" = seq(1, 12, by = 1/12), # Note how the color scale is much higher resolution than the data
"y" = -4
)

ggplot(data = df, aes(x = x, y = y))+
theme_classic()+
geom_line()+
scale_x_continuous(breaks = seq(1, 12, 1))+
scale_fill_gradient2(low = "black", mid = "gray", high = "white", midpoint = 6)+
theme(legend.position = "none")+
geom_tile(data = tile.df, aes(y = y, fill = x), height = 0.5)

In my actual dataset, the 'white' portion of the geom_tile() actually starts in July, which is why I'd like my x-axis to start here.在我的实际数据集中,geom_tile() 的“白色”部分实际上是在 7 月开始的,这就是为什么我希望我的 x 轴从这里开始。

Any help in this would be greatly appreciated!对此的任何帮助将不胜感激!

Cheers,干杯,

I added a xlim(7,12) and changed de midpoint argument in scale_fill_gradient to 9.5我添加了一个 xlim(7,12) 并将 scale_fill_gradient 中的 de midpoint 参数更改为 9.5

ggplot(data = df, aes(x = x, y = y))+
  theme_classic()+
  geom_line()+
  scale_x_continuous(breaks = seq(1, 12, 1))+
  scale_fill_gradient2(low = "black", mid = "gray", high = "white", midpoint = 9.5)+
  theme(legend.position = "none")+
  xlim(7,12)+
  geom_tile(data = tile.df, aes(y = y, fill = x), height = 0.5)

1个输出

You could reorder your months numerically then add labels in the correct order:您可以按数字重新排序月份,然后以正确的顺序添加标签:

library(ggplot2)

x <- seq(1, 12, 1)
set.seed(2022)
y <- rnorm(n = 12)

df <- data.frame("x" = x, "y" = y)

df$x <- 1 +(df$x+5) %% 12

tile.df <- data.frame(
  "x" = seq(1, 12, by = 1/12), # Note how the color scale is much higher resolution than the data
  "y" = -4
)

ggplot(data = df, aes(x = x, y = y))+
  theme_classic()+
  geom_line()+
  scale_x_continuous(breaks = seq(1, 12, 1), labels = c(month.abb[7:12], month.abb[1:6]))+
  scale_fill_gradient2(low = "black", mid = "gray", high = "white", midpoint = 6)+
  theme(legend.position = "none")+
  geom_tile(data = tile.df, aes(y = y, fill = x), height = 0.5)

The line df$x <- 1 +(df$x+5) %% 12 is behind the scenes reordering your months so that July = 1 to be plotted, then the labels of the axis show months in the new order. df$x <- 1 +(df$x+5) %% 12行在幕后重新排序您的月份,以便绘制 July = 1,然后轴的标签以新顺序显示月份。

A more intuitive way may be to convert to a factor, put in the order you want, then convert back to an integer when plotting (whilst similarly adding the correctly ordered labels:更直观的方法可能是转换为一个因子,按您想要的顺序放置,然后在绘图时转换回整数(同时类似地添加正确排序的标签:

reordered_months <- c(month.abb[7:12], month.abb[1:6])

df$month <- factor(month.abb[df$x], levels = reordered_months)

ggplot(data = df, aes(x = as.numeric(month), y = y)) +
  theme_classic()+
  geom_line()+
  scale_x_continuous(breaks = seq(1, 12, 1), labels = reordered_months)+
  scale_fill_gradient2(low = "black", mid = "gray", high = "white", midpoint = 6)+
  theme(legend.position = "none")+
  geom_tile(data = tile.df, aes(y = y, x = x, fill = x), height = 0.5)

(plots the same graph) (绘制相同的图表)

Created on 2022-05-12 by the reprex package (v2.0.1)reprex 包于 2022-05-12 创建 (v2.0.1)

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

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