我不知道為什么,但是當我運行 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()
uj5u.com熱心網友回復:
您可以指定要使用的中斷scale_x_continuous()
。
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()
每 N 年展示一次。例如每三年:
df %>%
ggplot(aes(x = Year, y = Sales, fill = Category))
geom_area() scale_x_continuous(breaks=seq(min(df$Year),max(df$Year),3))
uj5u.com熱心網友回復:
您可以在scale_x_continuous(breaks = seq(min(df$year), max(df$year), 1))
下面添加:
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))
由reprex 包于 2022-04-30 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/469534.html