Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
R code for the Telegram bot powered by n8n
# Make a dumbbell plot of the current and min/max temperatures in some EU capitals
# 2022-04-18 by Ed Parsadanyan
library(ggplot2)
library(ggthemes)
library(magrittr)
library(dplyr)
library(tidyr)
library(lubridate)
# arguments that were passed via command line
args = commandArgs(trailingOnly=TRUE)
print(args)
print(sessionInfo())
# Load csv file with city names and temperature
rawdat <- read.csv(file=args[1])
rawdat <- setNames(rawdat, c("CityName","TempCur","TempMin","TempMax")) %>%
mutate(CityName = factor(as.character(CityName),
levels = rev(as.character(CityName)),
labels = rev(as.character(CityName)))) %>%
gather(key = "Temp", value = "value", -CityName, -TempCur)
# Calculate X-axis limits, so that major ticks are always at the both ends of the axis
roundto <- 5
roundmin<- roundto * floor(min(rawdat$value)/roundto) # - (roundto*0.5)
roundmax<- roundto * floor(max(rawdat$value)/roundto) + roundto
print(paste0(roundmin," ",roundmax))
# Create dumbbell plot, save png file
plot_ <- ggplot(data=rawdat, aes(x= value, y= CityName)) +
geom_line(aes(group = CityName), colour="gray50") +
geom_point(aes(color=Temp), size=4) +
geom_point(aes(x=TempCur), size=1.5, colour="gray20") +
theme_clean(base_size = 12) + scale_x_continuous(breaks = seq(roundmin, roundmax, by = roundto)) +
theme(
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(colour = "gray", linetype = "dotted"),
panel.grid.minor.x = element_line(colour = "gray", linetype = "dotted"),
axis.line.y = element_line(colour = "black", size = 0.5, linetype = "solid"),
axis.line.x = element_line(colour = "black", size = 0.5, linetype = "solid"),
legend.position="none"
) + expand_limits(x=c(roundmin,roundmax)) +
labs(title="Current and min/max air temperature for the last 24 hours",
x="Air temperature (C)", y="",
caption=paste0("Data: OpenWeatherMap. Plot generated via ",paste0(R.version$language," version ",R.version$major,".",R.version$minor)," on ",now()))
ggsave(args[2],plot_,units="cm",dpi=150,width=20,height=10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment