我試圖繪製預期值和實際值,隨着時間的推移。我有一些數據可以在一張圖上顯示。我對R
還很新,我一直在卡住。如何爲多個變量數據添加曲線擬合線?
到目前爲止,我已經能夠在單獨的圖表上得到我想要的,或者如果我把它們放在一起,我似乎無法讓它做我想做的事。
我幾乎在那裏,但我想要的點(點是預期值)與虛線連接。我試着用幾種不同的方法添加一個LOESS
行(我的代碼中有一個散列),但是我一直在收到錯誤。
我對R
(以及一般編碼)還是一個新手,但我知道除了手動構建圖表外,還必須有一種方法可以做到這一點。但是,我嘗試的每個示例都會執行,但我似乎無法立即讓所有內容都工作。我開始明白每件事情都做了什麼,但有時我迷失在與什麼一起工作的東西中。
Error in xy.coords(x, y, xlabel, ylabel) : 'x' is a list, but does not have components 'x' and 'y'
Error: Don't know how to add RHS to a theme object
我的情節:(無連接的鏈路)
我的數據集
Year,SC_CE_5AGG,SC_ACA,TA_CE_5AGG,TA_ACA,OA_CE_5AGG,OA_ACA,CO_CE_5AGG,CO_ACA
2005,8,12,5,0,140,100,23,23
2006,,13,,0,,100,,25
2007,,13,,0,,102,,37
2008,,14,,0,,104,,36
2009,,16,,3,,104,,35
2010,10,17,6,4,179,106,29,36
2011,,20,,7,,111,,36
2012,,23,,7,,116,,33
2013,,22,,10,,118,,37
2014,,23,,12,,107,,40
2015,12,23,8,14,229,112,37,46
2016,,25,,14,,119,,56
2017,,28,,13,,120,,60
2018,,,,,,,,
2019,,,,,,,,
2020,16,,10,,292,,48,
2025,20,,20,,372,,61,
我的代碼
setwd("C:Users/X/Documents/PROJECTS/R_RcW/Data")
install.packages("ggplot2")
install.packages("GGally")
library(ggplot2)
library(GGally)
ALL <- read.csv(file="Rcw_data.csv", header = TRUE)
#To plot multiple lines, (for a small number of variables) you can use build up the plot manually yourself
ggplot(data=ALL, aes(Year)) +
geom_line(aes(y = SC_ACA, colour = "Shoal Creek")) +
lines(scatter.smooth(aes(y = SC_CE_5AGG, colour = "Shoal Creek"))) +
geom_line(aes(y = TA_ACA, colour = "Talladega")) +
lines(scatter.smooth(aes(y = TA_CE_5AGG, colour = "Talladega"))) +
geom_line(aes(y = OA_ACA, colour = "Oakmulgee")) +
lines(scatter.smooth(aes(y = OA_CE_5AGG, colour = "Oakmulgee"))) +
geom_line(aes(y = CO_ACA, colour = "Conecuh")) +
lines(scatter.smooth(aes(y = CO_CE_5AGG, colour = "Conecuh"))) +
#lines(lowess(SC_CE_5AGG), col="Shoal Creek") + # lowess line (x,y)
#lines(lowess(TA_CE_5AGG), col="Talladega") + # lowess line (x,y)
#lines(lowess(OA_CE_5AGG), col="Oakmulgee") + # lowess line (x,y)
#lines(lowess(CO_CE_5AGG), col="Conecuh") # lowess line (x,y)
theme_classic() +
ggtitle("Active clusters of Red-cockaded Woodpeckers") +
theme(plot.title = element_text(hjust = 0.5)) +
labs(colour="District") +
theme(legend.title.align=0.5) +
theme(panel.border = element_rect(colour = "black", fill=NA, size=)) +
scale_x_continuous(limits=c(2005, 2025), breaks=c(2005,2010,2015,2020,2025)) +
xlab("Year") + ylab("Number of active clusters")
你能改變你的數據在這裏的表現嗎?請參閱在https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –
中複製您的數據我不知道大部分含義。我嘗試了「最糟糕的情況」,但仍然出現錯誤。 「掃描時出錯(file = file,what = what,sep = sep,quote = quote,dec = dec,: 第2行沒有9個元素」 – nxl14
添加圖像內聯,格式化清理 – Patrick