No error message getting when trying to add legend

No error message getting when trying to add a legend however not getting the legend in plot.

ggplot(Rent1, aes(Date, Rent))+
geom_point(size=3, shape=16)+
geom_smooth(method = "lm", formula = y~poly(x,1), se = F) +
geom_smooth(method = "lm", formula = y~poly(x,3), se = F, color = "red") +
theme_economist() +
theme_bw()+
coord_cartesian(ylim = c(0, 60)) +
stat_regline_equation(label.y=55) +
stat_cor(aes(label=..rr.label..), label.y=50)+
labs(title = "Market Condition", x = "Sign Date", y = "Rent Per SF")+
theme(plot.title = element_text(hjust = 0.5))+
legend(0, 50, legend=c("Linier Trend", "3rd Order Polynomial Trend"), col = c("blue", "red"), lty=1:2, cex=0.8, box.lty=2, box.lwd=2, box.col="green")

Rent1

  No `Lease \r\nCenter \r\nAddress` Date          GLA  Rent YearBuilt Population MedianI…¹ Housi…² Total…³


1 1 Kohl's 2008-01-01 75000 25 1977 278238 69974. 105743 7.40e 9
2 2 Lowe's 2009-07-01 107000 24 2010 2263730 78283. 955038 7.48e10
3 3 Burlington Coat Factory 2010-05-01 87410 8.01 1986 1752655 97846. 623456 6.10e10
4 4 Nordstrom Rack 2010-08-01 35000 18.6 2009 1893161 93244. 650745 6.07e10
5 5 Target 2011-04-01 99677 23 1999 2220210 76616. 935856 7.17e10
6 6 Safeway 2011-10-01 65000 32.5 2012 1756357 101087. 637714 6.45e10
7 7 Dick's Sporting Goods 2013-01-01 84060 28.0 1968 1545600 81051. 667701 5.41e10
8 8 Costco 2013-03-01 147721 21 1998 2879507 59267. 988139 5.86e10
9 9 Food 4 Less 2013-09-01 80000 24.5 1992 5050412 55164. 1875950 1.03e11
10 10 Ross Dress for Less 2013-10-01 160088 40.3 2006 1039775 57151. 329405 1.88e10
11 11 Equinox 2014-01-01 114010 41.6 2001 2220210 76616. 935856 7.17e10

Hi, can you paste the output of dput(head(Rent1)) instead of just pasting the data?

dput(head(Rent1))
structure(list(No = c(1, 2, 3, 4, 5, 6), Lease Center Address = c("Kohl's",
"Lowe's", "Burlington Coat Factory", "Nordstrom Rack", "Target",
"Safeway"), Date = structure(c(13879, 14426, 14730, 14822, 15065,
15248), class = "Date"), GLA = c(75000, 107000, 87410, 35000,
99677, 65000), Rent = c(25, 24, 8.01, 18.6, 23, 32.5), YearBuilt = c(1977,
2010, 1986, 2009, 1999, 2012), Population = c(278238, 2263730,
1752655, 1893161, 2220210, 1756357), MedianIncome = c(69973.5756787683,
78282.594862194, 97845.7982503978, 93244.0574111211, 76615.968202373,
101087.410075677), HousingUnits = c(105743, 955038, 623456, 650745,
935856, 637714), TotalIncome = c(7399215813, 74762852832, 61002549994,
60678104140, 71701513538, 64464856629)), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"))

1 Like

This is base R syntax, you can't mix base R with ggplot2 syntax. If you want to get a legend on ggplot2 you need to map an aesthetic, for a manual legend, you can map it to string values and set the color on a manual scale. Take a look at this example made with part of your code:

library(ggplot2)

# Sample data
Rent1 <- structure(list(No = c(1, 2, 3, 4, 5, 6), `Lease Center Address` = c("Kohl's",
                                                                           "Lowe's", "Burlington Coat Factory", "Nordstrom Rack", "Target",
                                                                           "Safeway"), Date = structure(c(13879, 14426, 14730, 14822, 15065,
                                                                                                          15248), class = "Date"), GLA = c(75000, 107000, 87410, 35000,
                                                                                                                                           99677, 65000), Rent = c(25, 24, 8.01, 18.6, 23, 32.5), YearBuilt = c(1977,
                                                                                                                                                                                                                2010, 1986, 2009, 1999, 2012), Population = c(278238, 2263730,
                                                                                                                                                                                                                                                              1752655, 1893161, 2220210, 1756357), MedianIncome = c(69973.5756787683,
                                                                                                                                                                                                                                                                                                                    78282.594862194, 97845.7982503978, 93244.0574111211, 76615.968202373,
                                                                                                                                                                                                                                                                                                                    101087.410075677), HousingUnits = c(105743, 955038, 623456, 650745,
                                                                                                                                                                                                                                                                                                                                                        935856, 637714), TotalIncome = c(7399215813, 74762852832, 61002549994,
                                                                                                                                                                                                                                                                                                                                                                                         60678104140, 71701513538, 64464856629)), row.names = c(NA, -6L
                                                                                                                                                                                                                                                                                                                                                                                         ), class = c("tbl_df", "tbl", "data.frame"))

ggplot(Rent1, aes(Date, Rent))+
    geom_point(size=3, shape=16)+
    geom_smooth(aes(color = "Linier Trend"), method = "lm", formula = y~poly(x,1), se = F) +
    geom_smooth(aes(color = "3rd Order Polynomial Trend"), method = "lm", formula = y~poly(x,3), se = F) +
    # theme_economist() +
    theme_bw()+
    coord_cartesian(ylim = c(0, 60)) +
    scale_color_manual(values = c("Linier Trend" = "blue", "3rd Order Polynomial Trend" = "red")) +
    # stat_regline_equation(label.y=55) +
    # stat_cor(aes(label=..rr.label..), label.y=50)+
    labs(title = "Market Condition", x = "Sign Date", y = "Rent Per SF") +
    theme(plot.title = element_text(hjust = 0.5))

Created on 2023-01-29 with reprex v2.0.2

3 Likes

Thank you andresrcs. this really helps me a lot.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.