2017-02-27 133 views
1

我想在R中創建一個qplot。我知道我可以重新格式化我的數據,但是我想嘗試按照我的計劃在qplot中創建它,以後將其連接到Shiny。(R)在qplot中的行,每個x的平均值爲y​​

的問題之前,這是我的數據:

Date | Price | Postnr 
2016-08-01  5000   101 
2016-08-01  4300   103 
2016-07-01  7000   105 
2016-07-01  4500   105 
2016-07-01  3000   103 
2016-06-01  3900   101 
2016-06-01  2700   103 
2016-06-01  2900   105 
2016-05-01  7100   101 

我試圖創建一個使用情節線圖。 我想用Postnr進行分組。

我的問題是: 我希望日期位於X軸上,價格是Y,通過每天獲得平均價格創建的繪圖點,但我不知道如何去創建它在qplot本身。

CNC中 包括重放數據

mydata <- structure(list(Date = structure(c(4L, 4L, 3L, 3L, 3L, 2L, 
2L, 2L, 1L), .Label = c("2016-05-01", "2016-06-01", "2016-07-01", 
"2016-08-01"), class = "factor"), Price = c(5000L, 4300L, 7000L, 
4500L, 3000L, 3900L, 2700L, 2900L, 7100L), Postnr = c(101L, 103L, 
105L, 105L, 103L, 101L, 103L, 105L, 101L)), .Names = c("Date", 
"Price", "Postnr"), row.names = c(NA, 9L), class = "data.frame") 
+0

那麼你嘗試到底是什麼?另外請注意,不鼓勵使用'qplot'。最好使用適當的'ggplot()'函數。此外,以[可重現格式]分享您的數據更好(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example),所以我們可以複製/粘貼它進入R來測試可能的解決方案。 – MrFlick

+0

正如暗示讓你走上正確的道路,看看幫助(stat_summary) –

+0

謝謝Ian Fellows,這讓我走上了正確的道路 – Atius

回答

1

研究員伊恩後讓我在正確的道路,我終於找到了我一直在尋找的:

ggplot(data = mydata, 
    aes(x = Date, y = Price, colour = Postnr, group=Postnr)) + 
     stat_summary(fun.y=mean, geom="point")+ 
     stat_summary(fun.y=mean, geom="line") 
0

這是你正在尋找,@Atius的想法?

date = runif(100,0,10)+as.Date("1980-01-01") 
Price = runif(100,0,5000) 
Postnr = runif(100,101,105) 

dataFrame =data.frame(date=date, Price=Price, Postnr=Postnr) 


d <- ggplot(dataFrame, aes(date, Price)) 
d + geom_point() 
d + stat_summary_bin(aes(y = Postnr), fun.y = "mean", geom = "point")