r堆积条总值

时间:2016-06-24 09:54:26

标签: r plot stacked

我想创建一个关于从房屋(房屋中的设备)收集的数据的图(EDA)。但是我被困了..

数据看起来像这样:

df$device    df$date       df$time    df$value
boiler       2015-01-13    12:15      0.0009
boiler       2015-01-13    12:30      0.0007
boiler       2015-01-13    12:45      0.0005
boiler       2015-01-13    13:00      0.0010
TV           2015-01-13    12:15      0.0009
TV           2015-01-13    12:30      0.0007
TV           2015-01-13    12:45      0.0005
TV           2015-01-13    13:00      0.0010
boiler       2015-01-14    12:15      0.0009
boiler       2015-01-14    12:30      0.0007
boiler       2015-01-14    12:45      0.0005
boiler       2015-01-14    13:00      0.0010
TV           2015-01-14    12:15      0.0009
TV           2015-01-14    12:30      0.0007
TV           2015-01-14    12:45      0.0005
TV           2015-01-14    13:00      0.0010

数据是在8个月(1月至9月)内测量的。我想制作一个情节,包括:     Y轴=测量值     X轴=几个月     Plots =所有设备

我尝试过:

df$monthnumber <- month(df$Date)
test <- table(df$Device, df$monthnumber)

barplot(counts1,col=rainbow(7), xlim = c(1,15),
        legend = c(rownames(counts1)), bty = "L") 

导致下一张图片的原因是什么:

结果:enter image description here 但是,这是错误的。因为它包含设备测量的频率。我不知道如何为每个应用程序每月添加TOTAL VALUE(总和)。

我试过了:

test$value <- aggregate (df$Measurevalue, by = list(genergy$Device), sum)

但是给出了一个错误:

  

$&lt; - 。data.frame( tmp ,&#34; value&#34;,value = list(Group.1 = c)&#34; Boiler&#34; ,:
    替换有7行,数据有285896

我该如何解决这个问题? - 堆叠条形图,每个设备的总价值为(y= energy),(X= months

1 个答案:

答案 0 :(得分:0)

因此,您可以使用dplyrggplot

的组合
library("dplyr")
library("ggplot2")

df <- data.frame(device = c("boiler","boiler","boiler","boiler","TV","TV","TV","TV","boiler","boiler","boiler","boiler","TV","TV","TV","TV"), 
                     date = c("2015-01-13","2015-01-13","2015-01-13","2015-01-13","2015-01-13","2015-01-13","2015-01-13","2015-01-13","2015-01-14","2015-01-14","2015-01-14","2015-01-14","2015-01-14","2015-01-14","2015-01-14","2015-01-14"),
                     time = c("12:15","12:30","12:45","13:00","12:15","12:30","12:45","13:00","12:15","12:30","12:45","13:00","12:15","12:30","12:45","13:00"),
                     value = c(0.009,0.007,0.005,0.001,0.009,0.007,0.005,0.001,0.009,0.007,0.005,0.001,0.009,0.007,0.005,0.001)
                     )
    df <- mutate(df,device = as.factor(device),
                 date = as.Date.character(date,format = "%Y-%m-%d")) %>%
      group_by(device) %>%
        mutate(sum(value))

结果是每天按设备汇总 enter image description here

正如您在评论中提到的那样,您希望每月进行一次 - 根据月份对表格进行分组

df<- group_by(df,m=as.factor(month(date)),device) %>%
summarise(s = sum(value))
ggplot(df,aes(x=m,y=s,fill=device)) + geom_bar(stat="identity")

由于我没有足够的月度数据,图表如下所示。 enter image description here

更新:2016年6月27日 在输入数据后,运行以下命令。然后尝试根据月份汇总数据。

    df <- mutate(df,device = as.factor(device),
                  date = as.POSIXct((strftime(date,format = "%Y-%m-%d")))) %>%
         group_by(device)
相关问题