2017-08-07 113 views
-1

需要幫助繪製時間序列數據中的r。Y是溫度,X是日期(現在格式爲%d-%m-%y),需要繪圖像:以r爲單位繪製時間序列數據明智月份

temp data in month wise along with a box plot

我曾嘗試:

univariare溫

temperature = ts(r$temp, frequency = 12, start = 2011) plot(temperature, xaxt = "n") tsp = attributes(temperature)$tsp dates 
= seq(as.Date("2011-01-01"), by = "month", along = temperature) axis(1, at = seq(tsp[1], tsp[2], along = temperature), labels = format(dates, "%m-%y")) 

ggplot

gg2=ggplot(r,aes(mnth,temp)) + geom_line() 
windows() 
print(gg2) 

但格式不正確。

任何幫助將真正明顯!

由於 德維

+0

創建爲[此處描述]合適的可再現的示例(https://stackoverflow.com/questions/5963269/how-to-make-a-great -r-reproducible-example)以及'dput'您使用的數據 – parth

+0

請檢查[mcve]。也可以使用ctrl-g將圖形插入問題中(而不是鏈接到它)。 –

回答

0

Time Series Temperature Plot by Month

library(ggplot2) 

# Simulated temperature data 
temp <- runif(75) 

temp.avg <- vector() 

x <- 365 

for(i in 1:x){ 
    if(i <= round(.33 * x)) { 
    temp.avg[i] <- mean(sample(temp, 15, replace = TRUE)) 
    } else if (i <= round(.66 * x)) { 
    temp.avg[i] <- abs(log(mean(sample(temp, 15, replace = TRUE)))) 
    } else { 
    temp.avg[i] <- mean(sample(temp, 15, replace = TRUE)) * (i/x) + .15 
} 
} 

# Generate sequence of days in Date format "%d-%m-%y" 
from <- as.Date("01-1-11 12:00:00 EDT", "%d-%m-%y") 
to <- as.Date("31-12-11 12:00:00 EDT", "%d-%m-%y") 
times <- seq.Date(from, to, 1) 

# Put dates and temperatures into data frame 
Temperature_data <- data.frame(date = times, temp = temp.avg) 

# Plot in ggplot 
ggplot(Temperature_data, aes(date, temp)) + 
    geom_line() + 
    ylim(c(0, 1)) + 
    xlab("") + 
    ylab("Temperature") + 
    scale_x_date(date_breaks = "1 month", date_labels = "%b %y") + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
    ggtitle("Temperature fluctuations in 2011") 
+0

非常感謝您的快速回復!只是爲了添加我使用的數據集的日期格式爲'%d-%m-%y'和temp1,所以我們在這裏取每個月的平均值作爲temp並繪製爲2011年?所以在給定的代碼中,我應該使用我的臨時地點? –

+0

嗨@DeviVijayakumar,我認爲你提到的平均值是我試圖用虛假的溫度來模擬你發佈的圖像。這樣做可能不需要構建數據。 基本上,我剛剛使用隨機數的平均值爲一年中的每一天創建了一個溫度。所以,我有365個溫度,每個溫度與一個特定的日期相關聯。 我認爲你是正確的,雖然我不知道你的數據集「r」是什麼樣子。無論你的數字溫度數據是什麼,應該是一年中每天的一個數值,將這些值代入'temp.avg' – Jason

+0

@ Jason ..非常感謝你......我的數據文件就像每天的變量temp在2011年365天,需要是一個時間序列陰謀臨時爲全年即時星期五年節日星期幾工作日天氣溫度 1 1 2011-01-01春季0 1假星期六陰天/有霧0.344167 atemp哼風速臨時註冊cnt 1 0.363625 0.805833 0.160446 331 654 985 –