However, I would like to customize the legend labels in order to add the units of each variable, or something like this: Temperature (
), Rel. humidity (%) and Precipitation (mm). Also, I would like to remove the title "variable" and to move the legend to somewhere inside the plot area.
Any ideias on how to accomplish that? Here is the (reproducible) code I wrote to plot the data:
library(ggplot2)
require(RCurl)
# Read data
weather <- read.csv(textConnection(link),header=F, sep=',', stringsAsFactors = FALSE,
as.is=TRUE)
names(weather) <- c('Year', 'Julian', 'Hour', 'Temperature', 'Net Radiation', 'Precipitation', 'Humidity', 'Wind')
# Combine the first three columns into a character vector
date_info <- with(weather, paste(Year, Julian, Hour))
# Parse that character vector
weather$date <- strptime(date_info, "%Y %j %H")
# Data is hourly, but I need daily averages (or accumulation, for precip)
# First, create factor for days
DF2 <- data.frame(weather, Day = as.Date(format(weather$date)))
#Then, create daily averages and accumulation
new <- aggregate(cbind(Humidity, Temperature, Wind) ~ Day, DF2, mean)
tmp <- aggregate(cbind(Precipitation) ~ Day, DF2, sum)
new$Precipitation <- tmp$Precipitation
# Remove wind, not desired right now
new <- subset(new, select = -c(Wind))
# Melt data by date
data <- melt(new, id='Day')
# Plot variables
ggplot(data=data, aes(x=Day, y=value, group=variable, linetype=variable)) +
geom_line(size=0.7) +
scale_linetype_manual(values = c('twodash','solid','longdash')) +
theme_bw() +
labs(x = "date", y = "value") +
scale_x_date(breaks = "1 month", labels = date_format("%b-%Y"),
limits = as.Date(c('2012-01-01','2013-01-01'))) +
theme(axis.text.x = element_text(angle=45, hjust = 1, vjust = 1))
Thiago.