我創建了一個資料集,但在繪制它時遇到了一些問題。我認為這是因為資料集非常小,因此 R 不會將我的 X 軸識別為年份,而是將其識別為連續資料。因此,我的圖表有 2020.5。所以,我必須使用轉換年份列,as.factor(AshQuads$Year)但它拒絕將年份與geom_line.
這是我一直在使用的代碼
ggplot(Ash_Quads, aes(x=Year, y=Quadrats, colour=factor(Ash)))
geom_point()
theme_bw()
這是我的資料框:
我想知道是否有人可以幫忙?
uj5u.com熱心網友回復:
我建議將 x 軸保持為連續,而不是將其轉換為因子。只需為您的 x 軸提供您想要的實際休息時間,在這種情況下,年份2020:2022
ggplot(df, aes(Year,Quadrats,color=Ash))
geom_point() geom_line()
scale_x_continuous(breaks=2020:2022)
theme_bw()

uj5u.com熱心網友回復:
一種選擇是設定Yearto 因子(如您所提到的),然后將組添加到aes(即,group = Ash)中。您還需要添加geom_line代碼中沒有的 。
library(tidyverse)
ggplot(Ash_Quads, aes(x = factor(Year), y = Quadrats, colour = factor(Ash), group = Ash))
geom_point()
geom_line()
theme_bw()

或者您可以使用scale_x_continuous設定 x 軸(因此保留為連續資料而不轉換Year為因子):
ggplot(Ash_Quads, aes(x = Year, y = Quadrats, colour = factor(Ash)))
geom_point()
geom_line()
scale_x_continuous(breaks = 2020:2022)
theme_bw()
資料
Ash_Quads <- structure(list(Year = c(2020, 2021, 2022, 2020, 2021, 2022),
Quadrats = c(37, 31, 13, 54, 56, 38), Ash = c("No Ash", "No Ash",
"No Ash", "Ash", "Ash", "Ash")), class = "data.frame", row.names = c(NA,
-6L))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/482741.html
上一篇:為什么gplot2::labs()不覆寫/更新scales函式的name引數(即ggplot2::scale_*_(name=))?
