如何手动将图例添加到 ggplot 对象
我有这个数据框:
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")
并且我已经完成了所有这些(我知道代码看起来有点难看,指针赞赏):
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)]))
现在我想添加一个图例来澄清蓝线是投影而绿线是当前数据。如果可能的话,我想在不改变数据框的情况下进行更改。
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是手动添加图例的另一种方法。这允许您选择属于每个图例名称的颜色并可以用作模板。这是明确的传说。
这是定义颜色变量的更好方法。您在可视化之前整理数据。这就是隐含的传说。
This is another way to add a legend manually. This allows you to pick what color belongs to each legend name and can be used as a template. This is the explicit legend.
This is a better way to define color variables. You tidy your data before the visualization. This is the implicit legend.
您可以通过使用melt(在reshape包中)轻松实现这一点。这是定义数据框后添加的代码。
拉姆纳特
You can easily achieve this by using melt (in the reshape package). Here is the code you add after you define the data frame.
Ramnath
这是一种手动注释绘图的方法。我假设您将打印的绘图保存为 p2.因此,您需要将此代码添加到您已有的代码中。
让我知道这是否有效!
Here is a way to manually annotate your plot. I have assumed that you save the plot that you have printed as p2. So you need to add this code to what you already have.
Let me know if this works!