Set this flag to TRUE when you want to write out plots.
write.plot.enabled = F
library(data.table)
library(dplyr)
library(ggplot2)
Read AAVSO data from file
raw <- read.csv("AAVSO R Mon.csv", as.is=TRUE)
Transform raw data
r.mon <- raw %>%
mutate(jd=JD-2400000, mag=as.numeric(Magnitude), band=as.factor(Band)) %>%
select(jd, mag, band) %>%
filter(band == "V" | band == "Vis.") %>%
filter(!is.na(mag))
## Warning in eval(substitute(expr), envir, enclos): NAs introduced by
## coercion
Plot AAVSO light curve
# Limit plot to the last 25 years.
xlim <- c(max(r.mon$jd)-25*365,max(r.mon$jd))
# Limit plot of brightness.
ylim <- c(9.5,14.5)
p <- ggplot(data=r.mon) +
geom_point(aes(jd,mag,color=band), alpha=0.5) +
scale_color_manual(name="Band", values=c("green","grey50")) +
scale_y_continuous(trans="reverse") +
labs(title="R Mon AAVSO Light Curve", x="Julian Date - 2400000", y="Brightness [mag]") +
coord_cartesian(ylim=ylim, xlim=xlim) +
theme_bw()
p
if(write.plot.enabled) write.plot(p)
write.plot <- function(p, name=NULL, width=1024, height=576, font.size=16) {
title <- ifelse(!is.null(name), name, p$labels$title)
if (is.null(title)) stop("Neither name nor title given")
filename <- paste0(title, ".png")
png(filename,width, height)
print(p + theme_bw(base_size=font.size))
dev.off()
}