ho questo frame di dati:Come aggiungere manualmente una leggenda a un oggetto 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")
e ho tutto questo fatto (conosco il codice è un po 'brutto da vedere, puntatori apprezzato):
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)]))
Ora vorrei aggiungere una legenda per chiarire che la linea blu è una proiezione e la linea verde sono i dati correnti. Vorrei apportare le modifiche senza alterare il dataframe se possibile.
Grazie in anticipo!
così che funzionerà, ma speravo di poterlo fare senza alterare il dataframe. Modificherò la domanda per riflettere questo. – Dan
bene, è possibile definire m_summary3 come un frame di dati temporaneo solo per disegnare la figura in questo modo. motivi per cui non vorresti avere una cornice dati temporanea? se non vuoi creare m_summary3, passa semplicemente data = melt (m_summary2, id = id1) al tuo comando ggplot e questo dovrebbe occuparsene – Ramnath
yeah lo capisco, e potrei continuamente rimodellare i dataframes, non c'è davvero nulla di sbagliato con questo approccio. Speravo solo che ci fosse un modo per farlo in ggplot. – Dan