Customize legend of multiple geom_line's

2,856 views
Skip to first unread message

Thiago V. dos Santos

unread,
Feb 12, 2014, 12:59:05 PM2/12/14
to ggp...@googlegroups.com
Hi everyone,

I am working with hourly meteorological data and I need to plot a time series of aggregated daily values. I wrote a code that makes it, but there are some detalis that need to be changed. Specifically, I would like to be able to manually change some details of the legend.

In the way I have implemented it, the legend shows the names of the columns of the data.frame I am using, for example: Temperature, Humidity and Precipitation. 

However, I would like to customize the legend labels in order to add the units of each variable, or something like this: Temperature (°C), 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))


Best regards,
Thiago.

Vivek

unread,
Feb 12, 2014, 1:36:55 PM2/12/14
to ggp...@googlegroups.com

Details on positioning and labeling of legends (and more) can be got from Winston Chang's cookbook: http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/

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'),
                        breaks=c("Humidity","Temperature","Precipitation"),
                        labels=c("Rel. humidity (%)","Temperature (°C)", "Precipitation (mm)")) +
  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))+theme(legend.title=element_blank())+theme(legend.position=c(.6, .85))
  
Vivek

Thiago V. dos Santos

unread,
Feb 12, 2014, 2:44:56 PM2/12/14
to ggp...@googlegroups.com
Hi Vivek,

Thanks a lot, it was exactly what I was looking for.

Best,
Thiago.
Reply all
Reply to author
Forward
0 new messages