我有一個資料集,其中包含一列失業、一列月和一列年。
我想做一個線圖,其中 x 軸上有月份數,y 軸上有失業率,每條線代表不同的年份。
我首先按年份過濾資料框以分別獲得每年的 y 值,然后我嘗試了以下代碼:
y1 = df %>% filter(year == 1996)
y1 = y1$unemploy
y2 = df %>% filter(year == 1997)
y2 = y2$unemploy
y3 = df %>% filter(year == 1998)
y3 = y3$unemploy
plot1 = ggplot()
geom_line(mapping = aes(x = df$month, y = y1), color = "navyblue")
geom_line(mapping = aes(x = df$month,y = y2), color = "black")
geom_line(mapping = aes(x = df$month,y = y3), color = "red")
scale_y_continuous(limits=c(0,10))
scale_x_continuous(limits=c(1,15))
plot1
但是當我嘗試列印繪圖時,我收到以下錯誤訊息:
Error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (128): y
Run `rlang::last_error()` to see where the error occurred.
有誰知道這個情節可能有什么問題?
uj5u.com熱心網友回復:
對于您使用的方法,您需要為每個幾何圖形使用不同的資料集;month您將df 中變數的所有行分配給,x并且只有 unemploy 列的一個子集分配給y,因此,ggplot 中不同數量的 x 和 y 物體會回傳錯誤。
y1 = df %>% filter(year == 1996)
y2 = df %>% filter(year == 1997)
y3 = df %>% filter(year == 1998)
plot1 = ggplot()
geom_line(y1, aes(x = month, y = unemploy), color = "navyblue")
geom_line(y2, aes(x = month, y = unemploy), color = "black")
geom_line(y3, aes(x = month, y = unemploy), color = "red")
scale_y_continuous(limits=c(0,10))
scale_x_continuous(limits=c(1,15))
plot1
但更好的做法是color在映射中使用:
df %>%
filter(year %in% c("1996", "1997", "1998")) %>%
ggplot()
geom_line(aes(x = month, y = unemploy, color = year))
scale_y_continuous(limits=c(0,10))
scale_x_continuous(limits=c(1,15))
scale_color_manual如果您想要這些特定顏色并且不喜歡默認的 ggplot 顏色,您可以稍后使用。
uj5u.com熱心網友回復:
而不是按年份過濾并包括三個單獨geom_lines()的,只需將完整的資料框傳遞給ggplot()并映射year到color和group美學。您可以使用 指定顏色scale_color_manual()。
library(ggplot2)
library(scales)
ggplot(data = df)
geom_line(aes(month, unemploy, color = year, group = year))
scale_color_manual(values = c("navyblue", "black", "red"))
scale_y_continuous(
limits = c(0, .1),
label = percent
)
theme_light()

示例資料:
set.seed(13)
df <- expand.grid(month = factor(1:12), year = factor(1996:1998))
unemploy <- vector("double", 36)
unemploy[[1]] <- .05
for (i in 2:36) {
unemploy[[i]] <- unemploy[[i - 1]] rnorm(1, 0, .005)
}
df$unemploy <- unemploy
uj5u.com熱心網友回復:
不要假設這些年僅適用于 1996 年至 1998 年,首先只保留那些年,強迫他們考慮因素并將美學設定在最初的呼叫中ggplot。美學color將照顧線條分組。
suppressPackageStartupMessages({
library(dplyr)
library(ggplot2)
})
df %>%
filter(year %in% 1996:1998) %>%
mutate(year = factor(year)) %>%
ggplot(aes(x = month, y = unemploy, color = year))
geom_line(linewidth = 1)
scale_color_manual(values = c("navyblue", "black", "red"))
scale_y_continuous(limits = c(0, 10))
scale_x_continuous(limits = c(1, 12), breaks = 1:12)
labs(x = "Month", y = "Unemployment")
theme_bw()

使用reprex v2.0.2創建于 2022-11-14
資料創建代碼
資料創建代碼是從zephryl 的答案中借用的,有一些更改:
- 年份被延長,所以后來
filter實際上只保留了問題中的年份; month并且factor是數字,而不是因數;- 不需要
for回圈,它的值unemploy與cumsumy 軸限制一致; - 無需創建額外的變數 ,
unemploy然后將其分配給 data.frame。
set.seed(13)
df <- expand.grid(month = 1:12, year = 1995:1999)
df$unemploy <- 8 cumsum(rnorm(nrow(df), sd = 0.25))
使用reprex v2.0.2創建于 2022-11-14
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/533031.html
上一篇:在幾秒鐘內跟蹤運行時間
