如何在GGPLOT2中重新排序数字X轴?

发布于 2025-01-28 09:06:06 字数 899 浏览 4 评论 0原文

第一次发布,所以请原谅我犯的任何错误。

我试图在一年的时间内(从7月开始)绘制一些每月的价值。 。这是一些示例数据:

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()

在这种情况下,我想从7月开始X轴(x = 7)。如果我将X轴变量转换为一个因素,这很容易。但是,我需要将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)

在我的实际数据集中,geom_tile的“白色”部分()实际上是从7月开始的,这就是为什么我希望我的X轴从这里开始。

在此方面的任何帮助将不胜感激!

干杯,

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. . 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). This is easy enough if I convert my x-axis variables to a factor. 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:

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.

Any help in this would be greatly appreciated!

Cheers,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人事已非 2025-02-04 09:06:06

您可以按数值重新排序几个月,然后按正确的顺序添加标签:

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)

“

df $ x&lt; -1 +(df $ x+5)%% 12在幕后重新排序您的月份,以便绘制7月= 1,然后按新订单显示轴的标签。

一种更直观的方法可能是将要素转换为您想要的顺序,然后在绘制绘制时转换回整数(同时添加正确订购的标签:(

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)

绘制同一图)

在2022-05创建-12由 reprex package (v2.0.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.

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)

亚希 2025-02-04 09:06:06

我添加了一个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)

”

I added a xlim(7,12) and changed de midpoint argument in scale_fill_gradient to 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)

1output

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文