Skip to content

Instantly share code, notes, and snippets.

@nmcglincy
Last active December 17, 2015 07:40
Show Gist options
  • Save nmcglincy/52406b56f9ff054f9ca3 to your computer and use it in GitHub Desktop.
Save nmcglincy/52406b56f9ff054f9ca3 to your computer and use it in GitHub Desktop.
making graphs from my calories counting gsheet
suppressMessages(library(dplyr))
suppressMessages(library(ggplot2))
suppressMessages(library(ggthemes))
suppressMessages(library(lubridate))
data = read.csv(file = "days-of-zhorik - Sheet1.csv",
header = TRUE,
stringsAsFactors = FALSE)
dat = data %>%
group_by(date) %>%
summarise(tot = sum(cal_tot))
dat$date = ymd(dat$date)
pdf("tot-cal-per-day.pdf",
height = 7,
width = 11)
with(dat, plot(date,
tot,
type = "h",
ylim = c(1200, 3200),
ylab = "Total est. calories",
xlab = "Date"))
with(dat, abline(h = 2000, col = "red", lty = 2))
with(dat, abline(h = 1800, col = "green", lty = 2))
dev.off()
dat.meal = data %>%
group_by(date, meal) %>%
summarise(tot = sum(cal_tot))
dat.meal$date = ymd(dat.meal$date)
dat.meal$meal = factor(dat.meal$meal, levels = c("breakfast", "lunch", "dinner", "snack"))
ggplot(dat.meal,
aes(x = date,
y = tot,
fill = meal,
order = meal)) +
geom_hline(yintercept = c(2000, 1800),
size = 0.25,
colour = c("red", "green")) +
geom_bar(stat = "identity",
colour = "black") +
xlab("Date") +
ylab("Total estimated calories") +
scale_fill_brewer(type = "qual",
palette = 5) +
theme(panel.border = element_rect(fill = NA, colour = "black"),
panel.background = element_rect(fill = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_text(vjust = 0, size = 16),
axis.title.y = element_text(vjust = 1, size = 16),
axis.text.x = element_text(size=16, angle = 90, vjust = 0.5, hjust = 0),
axis.text.y = element_text(size=16),
plot.title = element_text(size = 20),
legend.text = element_text(size = 16),
legend.title = element_text(size = 18),
strip.text.x = element_text(size = 16),
strip.text.y = element_text(size = 16))
ggsave("zhorik2.pdf",
width = 10,
height = 7,
units = "in")
ggplot(dat.meal,
aes(x = date,
y = tot)) +
geom_bar(stat = "identity",
colour = "black") +
facet_wrap(~ meal) +
xlab("Date") +
ylab("Total estimated calories") +
scale_fill_brewer(type = "qual",
palette = 5) +
theme(panel.border = element_rect(fill = NA, colour = "black"),
panel.background = element_rect(fill = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_text(vjust = 0, size = 16),
axis.title.y = element_text(vjust = 1, size = 16),
axis.text.x = element_text(size=16, angle = 90, vjust = 0.5, hjust = 0),
axis.text.y = element_text(size=16),
plot.title = element_text(size = 20),
legend.text = element_text(size = 16),
legend.title = element_text(size = 18),
strip.text.x = element_text(size = 16),
strip.text.y = element_text(size = 16))
ggsave("zhorik3.pdf",
width = 7,
height = 7,
units = "in")
system("open *pdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment