Create and show a legend with ggplot2 with 3 datasets

John Orr Source

I have been trying to get a legend to show up on a graph that I have generated. Here's the plot:

Graph with average drought year diversions, average drought year treatment plant production, and storage decremented by production and incremented by diversions

Here are the 3 data frames I am using:

data frames for graph

data frames for graph

Code that generated the graph:

#Plot the data
dplot <- ggplot(data = dftotal3, aes(x=dateseq, y=droughtavg))
dplot <- dplot + geom_histogram(stat = "Identity", fill = "cyan", binwidth = 0.1)
dplot <- dplot + scale_x_discrete(limits = dftotal3$dateseq, labels = c(format(dateseq, "%b")))
dplot <- dplot + scale_y_log10(name = "Acre-Feet", breaks = c(0,100, 500, 1000, 2000, 6000, 15000, 30000))
dplot <- dplot + ggtitle("Senior Rights and a Three Year Drought") + theme(plot.title = element_text(face = "bold", color = "Blue"))
dplot <- dplot + xlab("Month")
dplot <- dplot + theme(axis.text.x=element_text(angle=60, hjust=1))
dplot <- dplot + theme(axis.text.x = element_text(face="bold", color="Black", size=5))
dplot <- dplot + geom_line(data = dfstor1, aes(x = dateseq, y = Beg_Stor), color = "Blue", size = 1)
dplot <- dplot + geom_line(data = dfprod3, aes(x = dateseq, y = droughtavg), color = "Magenta", size = 1)

print(dplot) 

I would like the legend (or annotation) to describe the histogram, and plotted lines.

I have used Google to search many different strings, e.g. "Legend does not display in ggplot2" or "creating a legend in ggplot2".

# Results from dput(dfprod3):
dfprod3 <- structure(list(Month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 
9, 10, 11, 12), droughtavg = c(1124.50109, 1022.97005, 1261.632464, 
1447.198, 1734.61844872, 2909.60478, 3649.6774, 2935.5954, 2815.02115, 
1338.52, 1152.99683, 1186.73306, 1124.50109, 1022.97005, 1261.632464, 
1447.198, 1734.61844872, 2909.60478, 3649.6774, 2935.5954, 2815.02115, 
1338.52, 1152.99683, 1186.73306, 1124.50109, 1022.97005, 1261.632464, 
1447.198, 1734.61844872, 2909.60478, 3649.6774, 2935.5954, 2815.02115, 
1338.52, 1152.99683, 1186.73306), monthname = c("Jan", "Feb", 
"Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", 
"Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", 
"Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", 
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), dateseq = structure(c(17167, 
17198, 17226, 17257, 17287, 17318, 17348, 17379, 17410, 17440, 
17471, 17501, 17532, 17563, 17591, 17622, 17652, 17683, 17713, 
17744, 17775, 17805, 17836, 17866, 17897, 17928, 17956, 17987, 
18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231), class = "Date")), row.names = c(NA, 
-36L), class = c("tbl_df", "tbl", "data.frame"))

# Results from dput(dftotal3)
dftotal3 <- structure(list(Month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 
9, 10, 11, 12), UCC02 = c(15.18, 0, 6.91, 189.92, 768.61, 1245.08, 
493.15, 231.98, 101.65, 100.39, 33.88, 22.88, 15.18, 0, 6.91, 
189.92, 768.61, 1245.08, 493.15, 231.98, 101.65, 100.39, 33.88, 
22.88, 15.18, 0, 6.91, 189.92, 768.61, 1245.08, 493.15, 231.98, 
101.65, 100.39, 33.88, 22.88), UCC06 = c(87.05, 97.59, 104.29, 
237.14, 1570.98, 4206.98, 2023.17, 462.99, 205.94, 273.42, 200.93, 
325.52, 87.05, 97.59, 104.29, 237.14, 1570.98, 4206.98, 2023.17, 
462.99, 205.94, 273.42, 200.93, 325.52, 87.05, 97.59, 104.29, 
237.14, 1570.98, 4206.98, 2023.17, 462.99, 205.94, 273.42, 200.93, 
325.52), UCC12 = c(140.87, 55.64, 60.13, 557.55, 1446.62, 1408.16, 
586.09, 353.8, 433.97, 402.79, 174.74, 125.17, 140.87, 55.64, 
60.13, 557.55, 1446.62, 1408.16, 586.09, 353.8, 433.97, 402.79, 
174.74, 125.17, 140.87, 55.64, 60.13, 557.55, 1446.62, 1408.16, 
586.09, 353.8, 433.97, 402.79, 174.74, 125.17), LCC02 = c(0, 
0, 0, 128.33, 427.3, 396.28, 146.89, 156.75, 217.06, 132.69, 
0, 0, 0, 0, 0, 128.33, 427.3, 396.28, 146.89, 156.75, 217.06, 
132.69, 0, 0, 0, 0, 0, 128.33, 427.3, 396.28, 146.89, 156.75, 
217.06, 132.69, 0, 0), LCC06 = c(0, 0, 0, 84.87, 493.63, 737.65, 
1159.48, 538.1, 536.95, 406.93, 0, 0, 0, 0, 0, 84.87, 493.63, 
737.65, 1159.48, 538.1, 536.95, 406.93, 0, 0, 0, 0, 0, 84.87, 
493.63, 737.65, 1159.48, 538.1, 536.95, 406.93, 0, 0), LCC12 = c(0, 
0, 0, 387.54, 414.07, 968.4, 916.79, 648.59, 600.29, 162.44, 
0, 0, 0, 0, 0, 387.54, 414.07, 968.4, 916.79, 648.59, 600.29, 
162.44, 0, 0, 0, 0, 0, 387.54, 414.07, 968.4, 916.79, 648.59, 
600.29, 162.44, 0, 0), SP02 = c(0, 0, 0, 631.16, 1464.07, 720.04, 
721.73, 516.13, 383.83, 129.24, 869.88, 0, 0, 0, 0, 631.16, 1464.07, 
720.04, 721.73, 516.13, 383.83, 129.24, 869.88, 0, 0, 0, 0, 631.16, 
1464.07, 720.04, 721.73, 516.13, 383.83, 129.24, 869.88, 0), 
    SP06 = c(0, 0, 0, 1109.974375, 398.64, 466.3, 1656.958, 1374.8625, 
    2820.772325, 2014.94468, 0, 0, 0, 0, 0, 1109.974375, 398.64, 
    466.3, 1656.958, 1374.8625, 2820.772325, 2014.94468, 0, 0, 
    0, 0, 0, 1109.974375, 398.64, 466.3, 1656.958, 1374.8625, 
    2820.772325, 2014.94468, 0, 0), SP12 = c(0, 0, 55.6875, 1628.65029166667, 
    1576.2956, 303.5, 937.62935, 204.64, 1013.6474375, 1968.83755222208, 
    0, 0, 0, 0, 55.6875, 1628.65029166667, 1576.2956, 303.5, 
    937.62935, 204.64, 1013.6474375, 1968.83755222208, 0, 0, 
    0, 0, 55.6875, 1628.65029166667, 1576.2956, 303.5, 937.62935, 
    204.64, 1013.6474375, 1968.83755222208, 0, 0), droughtavg = c(81.0333333333333, 
    51.0766666666667, 75.6725, 1651.71155555556, 2853.4052, 3484.13, 
    2880.62911666667, 1495.9475, 2104.70325416667, 1863.89407740736, 
    426.476666666667, 157.856666666667, 81.0333333333333, 51.0766666666667, 
    75.6725, 1651.71155555556, 2853.4052, 3484.13, 2880.62911666667, 
    1495.9475, 2104.70325416667, 1863.89407740736, 426.476666666667, 
    157.856666666667, 81.0333333333333, 51.0766666666667, 75.6725, 
    1651.71155555556, 2853.4052, 3484.13, 2880.62911666667, 1495.9475, 
    2104.70325416667, 1863.89407740736, 426.476666666667, 157.856666666667
    ), monthname = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", 
    "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 
    "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", 
    "Oct", "Nov", "Dec"), dateseq = structure(c(17167, 17198, 
    17226, 17257, 17287, 17318, 17348, 17379, 17410, 17440, 17471, 
    17501, 17532, 17563, 17591, 17622, 17652, 17683, 17713, 17744, 
    17775, 17805, 17836, 17866, 17897, 17928, 17956, 17987, 18017, 
    18048, 18078, 18109, 18140, 18170, 18201, 18231), class = "Date")), row.names = c(NA, 
-36L), class = "data.frame")

# Results from > dput(dfstor1)
dfstor1 <- structure(list(Month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 
29, 30, 31, 32, 33, 34, 35, 36), Beg_Stor = c(32239, 31195.5323, 
30223.63897, 29037.67947, 30156.46667, 30730.99167, 29961.94379, 
27230.98879, 25429.04119, 26059.35879, 25332.83846, 24303.67216, 
23260.20449, 22216.73679, 21244.84346, 20058.88396, 21177.67116, 
21752.19616, 20983.14828, 18252.19328, 16450.24568, 17080.56328, 
16354.04295, 15324.87665, 14281.40898, 13237.94128, 12266.04795, 
11080.08845, 12198.87565, 12773.40065, 12004.35277, 9273.39777, 
7471.45017, 8101.76777, 7375.24744, 6346.08114), dateseq = structure(c(17167, 
17198, 17226, 17257, 17287, 17318, 17348, 17379, 17410, 17440, 
17471, 17501, 17532, 17563, 17591, 17622, 17652, 17683, 17713, 
17744, 17775, 17805, 17836, 17866, 17897, 17928, 17956, 17987, 
18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231), class = "Date")), row.names = c(NA, 
-36L), class = c("tbl_df", "tbl", "data.frame"))

Legend should show the color of the line or histogram. cyan = diversions, magenta = production, blue = storage.

rggplot2annotations

Answers

answered 1 week ago MrFlick #1

If you want things in a legend, they need to be in an aesthetic mapping. It's easiest to place a label value in the aes() call and then set the values with the appropriate scale function

ggplot(data = dftotal3, aes(x=dateseq, y=droughtavg)) + 
  geom_col(aes(fill = "Diversions")) + 
  scale_x_discrete(limits = dftotal3$dateseq, labels = c(format(dftotal3$dateseq, "%b"))) + 
  scale_y_log10(name = "Acre-Feet", breaks = c(0,100, 500, 1000, 2000, 6000, 15000, 30000)) + 
  ggtitle("Senior Rights and a Three Year Drought") + theme(plot.title = element_text(face = "bold", color = "Blue")) + 
  xlab("Month") + 
  theme(axis.text.x=element_text(angle=60, hjust=1)) + 
  theme(axis.text.x = element_text(face="bold", color="Black", size=5)) + 
  geom_line(data = dfstor1, aes(x = dateseq, y = Beg_Stor, color="Storage"), size = 1) + 
  geom_line(data = dfprod3, aes(x = dateseq, y = droughtavg, color="Production"), size = 1) +
  scale_fill_manual(values=c("Diversions"="cyan")) + 
  scale_color_manual(values=c("Storage"="blue", "Production"="magenta"))

enter image description here

comments powered by Disqus