Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually add a legend to a ggplot object

Tags:

r

ggplot2

I have this data frame:

structure(list(month_num = 1:24, founded_month = c(4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 1L, 2L, 3L), founded_year = c(2008L, 2008L, 2008L, 
2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 
2010L, 2010L, 2010L), count = c(270L, 222L, 256L, 250L, 277L, 
268L, 246L, 214L, 167L, 408L, 201L, 225L, 203L, 220L, 230L, 225L, 
177L, 207L, 166L, 135L, 116L, 122L, 69L, 42L), month_abb = c("Apr", 
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", 
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
"Nov", "Dec", "Jan", "Feb", "Mar"), short_year = c("08", "08", 
"08", "08", "08", "08", "08", "08", "08", "09", "09", "09", "09", 
"09", "09", "09", "09", "09", "09", "09", "09", "10", "10", "10"
), proj = c(282, 246, 292, 298, 337, 340, 330, 310, 275, 528, 
333, 369, 359, 388, 410, 417, 381, 423, 394, 375, 368, 386, 345, 
330), label = c("Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
"Nov", "Dec", "Jan\n09", "Feb", "Mar", "Apr", "May", "Jun", "Jul", 
"Aug", "Sep", "Oct", "Nov", "Dec", "Jan\n10", "Feb", "Mar")), .Names = c("month_num", 
"founded_month", "founded_year", "count", "month_abb", "short_year", 
"proj", "label"), row.names = c(NA, -24L), class = "data.frame")

and i've got all of this done (I know the code's a bit ugly looking, pointers appreciated):

p <- ggplot(m_summary2, aes(x = month_num, y = count))
p + 
geom_line(colour = rgb(0/255, 172/255, 0/255)) + geom_point(colour = rgb(0/255, 172/255,          
    0/255)) + 
geom_line(aes(x = m_summary2$month_num, y = m_summary2$proj), 
    colour = rgb(18/255, 111/255, 150/255)) + 
geom_point(aes(x = m_summary2$month_num, y = m_summary2$proj), colour = rgb(18/255,   
    111/255, 150/255)) +     
scale_x_continuous("Month", breaks = m_summary2$month_num, labels = m_summary2$label) + 
scale_y_continuous("# Startups Founded") + 
opts(title = paste("# Startups Founded:", m_summary2$month_abb[1], 
    m_summary2$short_year[1], "-", m_summary2$month_abb[nrow(m_summary2)],  
    m_summary2$short_year[nrow(m_summary2)]))

Now I would like to add a legend to clarify that the blue line is a projection and the green line is the current data. I would like to make the changes without altering the dataframe if possible.

Thanks in advance!

like image 926
Dan Avatar asked Apr 05 '10 23:04

Dan


People also ask

How do I add a legend to Ggplot?

Adding a legend If you want to add a legend to a ggplot2 chart you will need to pass a categorical (or numerical) variable to color , fill , shape or alpha inside aes . Depending on which argument you use to pass the data and your specific case the output will be different.

Why isn't my legend showing up in Ggplot?

You need to put the color parameter inside the aesthetics. This will result in the mapping of colors for the legend. After that you can manually scale the color to get any color you desire.

How do I change the legend value in ggplot2?

You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))

How do I specify legend titles in ggplot2?

Another way to change legend titles is to use guides() function in ggplot2. Here, guides() function can take two legend titles as arguments. We use guide_legend() to specify the new title we want one for size and other for color.


1 Answers

You can easily achieve this by using melt (in the reshape package). Here is the code you add after you define the data frame.

id1 = c("month_num","founded_month", "founded_year","month_abb","short_year","label");   
m_summary3 = melt(m_summary2, id = id1);
p = ggplot(m_summary3, aes(x = month_num, y = value, group = variable, colour = variable));
c1 = rgb(0/255, 172/255, 0/255);
c2 = rgb(18/255, 111/255, 150/255);
x_scale = scale_x_continuous("Month", breaks = m_summary2$month_num, labels = m_summary2$label);
y_scale = scale_y_continuous("# Startups Founded")

p + geom_line() + scale_colour_manual(values = c(c1,c2)) + x_scale + y_scale;

Ramnath

like image 119
Ramnath Avatar answered Sep 17 '22 23:09

Ramnath