如何在X轴上设置数年作为GGPLOT R中的整数

发布于 2025-01-25 07:44:23 字数 554 浏览 4 评论 0原文

我不知道为什么,但是当我运行GGPLOT堆叠的区域图表时,X轴的数年是像2012.5这样的双打。...我将它们设置为整数,但仍然存在这个问题。有解决方案吗?

df$Year <- as.integer(df$Year)

order <- df %>% filter(Year ==max(df$Year) ) %>% arrange(desc(Sales)) %>% select(Category)
df$Category <- factor(df$Category, levels = order$Category)

df %>% 
  ggplot(aes(x = Year, y = Sales, fill = Category)) +
  geom_area()

I don't know why but when I run ggplot stacked area chart, years on the x-axis are doubles like 2012.5.... I set them as integer but still have this issue. Any solution?

df$Year <- as.integer(df$Year)

order <- df %>% filter(Year ==max(df$Year) ) %>% arrange(desc(Sales)) %>% select(Category)
df$Category <- factor(df$Category, levels = order$Category)

df %>% 
  ggplot(aes(x = Year, y = Sales, fill = Category)) +
  geom_area()

enter image description here

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

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

发布评论

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

评论(2

摘星┃星的人 2025-02-01 07:44:23

您可以指定与scale_x_continouul()一起使用的休息时间。

df %>% 
  ggplot(aes(x = Year, y = Sales, fill = Category)) +
  geom_area() + scale_x_continuous(breaks=2010:2020)

为了使其更具动态性并自动选择数据上的限制,请使用:

df %>% 
  ggplot(aes(x = Year, y = Sales, fill = Category)) +
  geom_area() + scale_x_continuous(breaks=min(df$Year):max(df$Year))

如果您不想显示每年,则可以使用seq()每年每年都有。例如每三年每三年:

df %>% 
  ggplot(aes(x = Year, y = Sales, fill = Category)) +
  geom_area() + scale_x_continuous(breaks=seq(min(df$Year),max(df$Year),3))

You can specify the breaks to use with scale_x_continuous().

df %>% 
  ggplot(aes(x = Year, y = Sales, fill = Category)) +
  geom_area() + scale_x_continuous(breaks=2010:2020)

To make it more dynamic and automatically choose the limits base on your data, use:

df %>% 
  ggplot(aes(x = Year, y = Sales, fill = Category)) +
  geom_area() + scale_x_continuous(breaks=min(df$Year):max(df$Year))

And if you don't want to show every year you can use seq() to just have every Nth year. E.g. for every third year:

df %>% 
  ggplot(aes(x = Year, y = Sales, fill = Category)) +
  geom_area() + scale_x_continuous(breaks=seq(min(df$Year),max(df$Year),3))
独享拥抱 2025-02-01 07:44:23

您可以添加scale_x_continouul(breaks = seq(df $ egar),max(df $ ear),1)) per:

library(tidyverse)

df <- tribble(
  ~year, ~sales, ~category,
  2020, 4, "a",
  2021, 5, "a",
  2022, 5, "a",
  2020, 4, "b",
  2021, 5, "b",
  2022, 5, "b"
  ) 

df %>% 
  ggplot(aes(year, sales, fill = category)) +
  geom_area() +
  scale_x_continuous(breaks = seq(min(df$year), max(df$year), 1))

在2022-04-30 (v2.0.1)

You could add scale_x_continuous(breaks = seq(min(df$year), max(df$year), 1)) per below:

library(tidyverse)

df <- tribble(
  ~year, ~sales, ~category,
  2020, 4, "a",
  2021, 5, "a",
  2022, 5, "a",
  2020, 4, "b",
  2021, 5, "b",
  2022, 5, "b"
  ) 

df %>% 
  ggplot(aes(year, sales, fill = category)) +
  geom_area() +
  scale_x_continuous(breaks = seq(min(df$year), max(df$year), 1))

Created on 2022-04-30 by the reprex package (v2.0.1)

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