Created
August 29, 2023 17:24
-
-
Save tukachev/ed2d6150a2a2571a6db9114b5b89bac0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(readr) | |
library(ggtext) | |
battery_log <- read_csv("battery_log.csv", | |
col_names = FALSE, | |
locale = locale(encoding = "ASCII")) | |
# View(battery_log) | |
battery_log <- battery_log %>% rename( | |
"date" = X1, | |
"status" = X2, | |
"capacity" = X3, | |
"capacity_value" = X4, | |
"rate" = X5, | |
"voltage" = X6, | |
"event_type" = X7 | |
) %>% | |
filter(status %in% c("Discharging", "Critical, Discharging")) %>% | |
mutate( | |
capacity_value = as.numeric(gsub(" ", "", capacity_value)), | |
voltage = as.numeric(gsub(" ", "", voltage)), | |
date = as.POSIXct(date, format = "%d.%m.%Y %H:%M:%S") | |
) | |
battery_log <- readRDS("battery_log.Rds") | |
# saveRDS(battery_log, "battery_log.Rds") | |
battery_log <- battery_log %>% | |
mutate( | |
minutes_work = 1, | |
new_battery = ifelse(date > as.Date("2023-07-30"), | |
"new battery", "old battery"), | |
capacity_numeric = parse_number(capacity), | |
cycle_change = ifelse( | |
capacity_numeric <= 100.0 & | |
lag(capacity_numeric, default = -1) != 100.0 | |
& | |
capacity_numeric > lag(capacity_numeric, | |
default = -1), | |
1, | |
0 | |
) | |
) %>% | |
arrange(date) %>% | |
mutate(ID = cumsum(cycle_change)) %>% | |
group_by(ID) %>% | |
mutate(minutes_work = cumsum(minutes_work), count = n()) %>% | |
select( | |
ID, | |
new_battery, | |
date, | |
capacity, | |
capacity_numeric, | |
capacity_value, | |
rate, | |
voltage, | |
event_type, | |
minutes_work, | |
count | |
) %>% | |
filter(count > 30) %>% | |
arrange(ID, date) | |
#график | |
battery_log %>% | |
ggplot() + | |
geom_step( | |
aes( | |
minutes_work, | |
capacity_numeric, | |
group = ID, | |
colour = new_battery | |
), | |
alpha = 0.75, | |
show.legend = FALSE, | |
linewidth = 0.5 | |
) + | |
scale_color_manual(values = c("#C3362E", "#797979")) + | |
scale_y_continuous( | |
breaks = seq(0, 100, 10), | |
labels = paste0(seq(0, 100, 10), "%"), | |
expand = c(0.01, 0) | |
) + | |
scale_x_continuous(breaks = seq(0, 420, 60), expand = c(0.01, 0)) + | |
labs( | |
title = | |
glue::glue( | |
"**Время работы моего ноутбука Xiaomi Air 12.5 <br><b style='color:#797979;'>**до**</b> и <b style='color:#C3362E;'>**после**</b> замены аккумулятора**" | |
), | |
subtitle = glue::glue( | |
"На основе данных поминутного мониторинга оставшегося <br><span style='color:#000000;'>% заряда аккумулятора</span> для {length(unique(battery_log$ID))} цикла разряда" | |
), | |
x = "Время работы, минут", | |
# y = "Емкость аккумулятора, %", | |
caption = "Источник данных: BatteryInfoView v.1.25\nВизуализация: Юрий Тукачев, август 2023 @weekly_charts" | |
) + | |
theme( | |
text = element_text(family = "PT Sans", size = 18), | |
plot.title.position = "plot", | |
plot.caption.position = "plot", | |
plot.margin = margin(25, 25, 10, 25), | |
plot.title = element_markdown(), | |
plot.subtitle = element_markdown(color = "gray60", | |
margin = margin(15, 0, 10, 0)), | |
plot.caption = element_text( | |
color = "gray60", | |
size = 12, | |
margin = margin(t = 15) | |
), | |
axis.title.y = element_blank(), | |
panel.background = element_rect(fill = "white", | |
colour = "white"), | |
panel.grid.major = element_line(colour = "white"), | |
panel.grid.minor = element_line(colour = "white") | |
) + | |
coord_cartesian(clip = "off") | |
ggsave( | |
"battery_log.png", | |
scale = 1.5, | |
dpi = 300, | |
width = 5, | |
height = 5 | |
) | |
# check | |
battery_summary <- battery_log %>% group_by(ID) %>% | |
summarise( | |
max_cap = max(capacity_numeric), | |
min_cap = min(capacity_numeric), | |
mins_max = max(mins), | |
max_cap_value = max(capacity_value), | |
n = n() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment