如何手动添加图例到ggplot对象 [英] How to manually add a legend to a ggplot object

查看:92
本文介绍了如何手动添加图例到ggplot对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据框:

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.

预先感谢!

推荐答案

您可以通过使用融化(在重塑包装中)轻松实现此目的。

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

这篇关于如何手动添加图例到ggplot对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆