[英]R aggregate data.frame having dates and hours in one column misformatted
我有一個如下數據框:
kWh Equipment date
1 1.53 aquecedor01 2015-01-01 00:00:00
2 5.29 aquecedor01 2015-01-01 01:00:00
3 5.73 aquecedor01 2015-01-01 02:00:00
但是,當我通過Equipment變量匯總數據以找到kWh的最大值時,date列的格式錯誤如下:
Equipment kWh date
1 aquecedor01 6.5 1433023200
2 aquecedor02 6.5 1433023200
3 exaustor 6.5 1433023200
我已經為此苦苦掙扎了一段時間,而我所發現的大多數東西只能獨立於日期或小時運行。 就我而言,由於我是在Shiny應用程序中執行繪圖,因此一次完成所有操作會更容易。
我想在條形圖中繪制每個設備的所有最大值,然后在條形上寫下該時間。 這是我這樣做的代碼:
ggplotly(ggplot(data=aggregate(
. ~ Equipment,
data = dt.hourly[(as.character(input$dateRange[1]) <= dt.hourly$date) &
(as.character(input$dateRange[2]) > dt.hourly$date) &
(dt.hourly$Equipment %in% input$equipments),], max),
aes(x=Equipment, y=kWh)) +
geom_bar(position = 'dodge', stat='identity') +
geom_text(aes(label=date),
position = position_stack(vjust = 0.5),
angle = 90,
size=2) +
xlab("Date") +
ylab("Consumption (kWh)") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
))
除了那個angle=90
被忽略,我不知道為什么。 這就是我得到的:
提前。
作為可重現的示例:
library(plotly)
set.seed(1)
dt <- data.frame(
kWh = sample(10:100, 10, replace = TRUE)/100,
Equipment = sample(c("heater", "furnace", "AC"), 10, replace = TRUE),
date = sample(as.POSIXct(c("2015-01-14 17:00:00", "2015-01-21 20:00:00", "2015-01-21 22:00:00", "2015-02-21 20:00:00", "2015-01-22 14:00:00", "2015-02-14 17:00:00", "2015-02-21 20:00:00", "2015-02-21 22:00:00", "2015-03-21 20:00:00", "2015-03-22 14:00:00" )), 10, replace = TRUE)
)
對於繪圖:
ggplotly(ggplot(data=aggregate(
. ~ Equipment,
data = dt[("2015-01-12" <= dt$date) &
("2015-02-22" > dt$date) &
(dt$Equipment %in% c("AC", "furnace")),], max),
aes(x=Equipment, y=kWh)) +
geom_bar(position = 'dodge', stat='identity') +
geom_text(aes(label=date),
position = position_stack(vjust = 0.5),
angle = 90,
size=2) +
xlab("Date") +
ylab("Consumption (kWh)") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)))
dput
輸出為:
structure(list(kWh = c(0.34, 0.43, 0.62, 0.92, 0.28, 0.91, 0.95,
0.7, 0.67, 0.15), Equipment = structure(c(3L, 3L, 1L, 2L, 1L,
2L, 1L, 1L, 2L, 1L), .Label = c("AC", "furnace", "heater"), class = "factor"),
date = structure(c(1427032800, 1421877600, 1424548800, 1421870400,
1421877600, 1424548800, 1421254800, 1424548800, 1426968000,
1424548800), class = c("POSIXct", "POSIXt"), tzone = "")), class = "data.frame", row.names = c(NA,
-10L))
由於您的目標是注釋發生最大kWh的日期,因此您要在匯總中省略日期 。 因此,請考慮使用添加了相同長度列的ave
計算分組的max_kWh (內聯聚合)。 然后子集您的數據幀,其中kWh == max_kWh
。
dt$max_kWh <- with(dt, ave(kWh, Equipment, FUN=max))
agg_dt <- subset(dt, kWh == max_kWh)
ggplot(data=agg_dt, aes(x=Equipment, y=kWh)) +
geom_bar(position = 'dodge', stat='identity') +
geom_text(aes(label=date),
position = position_stack(vjust = 0.5),
angle = 0,
size = 2) +
xlab("Equipment") +
ylab("Consumption (kWh)") +
theme(axis.text.x = element_text(angle = 0, hjust = 1))
對於讀取輸入值的Shiny集成,請使用transform
添加max_kWh列,然后將結果包裝在subset
:
agg_dt <- subset(
transform(dt.hourly[(as.character(input$dateRange[1]) <= dt.hourly$date) &
(as.character(input$dateRange[2]) > dt.hourly$date) &
(dt.hourly$Equipment %in% input$equipments),],
max_kWh = ave(kWh, Equipment, FUN=max),
kWh == max_kWh
)
ggplotly(ggplot(data=agg_dt, aes(x=Equipment, y=kWh)) +
geom_bar(position = 'dodge', stat='identity') +
geom_text(aes(label=date),
position = position_stack(vjust = 0.5),
angle = 0,
size = 2) +
xlab("Date") +
ylab("Consumption (kWh)") +
theme(axis.text.x = element_text(angle = 0, hjust = 1))
))
您可以在繪制數據之前根據需要過濾數據:
library(tidyverse)
dt_sum <- dt %>%
# First filter according to your input
filter(Equipment %in% c("AC", "furnace") & ("2015-01-12" <= date) & ("2015-02-22" > date)) %>%
group_by(Equipment) %>% # Group the data by Equipment
top_n(1, kWh) %>% # Take the maximum kWh value per Equipment
top_n(1, date) # Take the maximum date if there are several with the same max kWh value
dt_sum
# A tibble: 2 x 3
# Groups: Equipment [2]
# kWh Equipment date
# <dbl> <fct> <dttm>
# 1 0.92 furnace 2015-01-21 20:00:00
# 2 0.95 AC 2015-01-14 17:00:00
p <- ggplot(dt_sum, aes(x = Equipment, y = kWh)) +
geom_bar(position = 'dodge', stat = 'identity') +
geom_text(aes(label = date), position = position_stack(vjust = 0.5),
angle = 90, size = 2) +
xlab("Date") +
ylab("Consumption (kWh)") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
p
角度問題歸因於ggplotly
(如您所見,在ggplot
不會忽略angle = 90
)。
ggplotly(p)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.