(R)在qplot中的行,每个x的平均值为y
问题描述:
我想在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")
答
研究员伊恩后让我在正确的道路,我终于找到了我一直在寻找的:
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")
答
这是你正在寻找,@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")
那么你尝试到底是什么?另外请注意,不鼓励使用'qplot'。最好使用适当的'ggplot()'函数。此外,以[可重现格式]分享您的数据更好(http://*.com/questions/5963269/how-to-make-a-great-r-reproducible-example),所以我们可以复制/粘贴它进入R来测试可能的解决方案。 – MrFlick
正如暗示让你走上正确的道路,看看帮助(stat_summary) –
谢谢Ian Fellows,这让我走上了正确的道路 – Atius