What I want is to remove diffmean
legend produced by geom_segment()
and scale_color_gradient2()
and add legend for both dotted line and solid line (geom_line()
). I have tried:
ggplot2::geom_segment(aes(xend = date, yend = cummeanpcp, color = diffmean), linewidth = 1.2,
na.rm = TRUE) +
ggplot2::scale_color_gradient2(high = "#2c7bb6", mid = "white", low = "#d7191c") +
ggplot2::geom_line(aes(color = "Cumsumpcp"), linewidth = 0.85, lineend = "round", na.rm = TRUE) +
ggplot2::geom_line(aes(y = cummeanpcp, color = "Cummeanpcp"), linetype = "longdash", na.rm = TRUE) +
ggplot2::scale_color_manual(values = c("Cummeanpcp" = "black",
"Cumsumpcp" = "black")) +
ggthemes::theme_hc(base_size = 15)
But I get:
Scale for colour is already present.
Adding another scale for colour, which will replace the existing scale.
Error: Continuous value supplied to discrete scale
Data:
structure(list(day = c("01", "02", "03", "04", "05", "06", "07",
"08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
"30", "31", "01", "02", "03", "04", "05", "06", "07", "08", "09",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26", "27", "28", "29", "01", "02",
"03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31", "01", "02", "03", "04",
"05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26",
"27", "28", "29", "30", "01", "02", "03", "04", "05", "06", "07",
"08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
"30", "31", "01", "02", "03", "04", "05", "06", "07", "08", "09",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "01",
"02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
"24", "25", "26", "27", "28", "29", "30", "31", "01", "02", "03",
"04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
"26", "27", "28", "29", "30", "31", "01", "02", "03", "04", "05",
"06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16",
"17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27",
"28", "29", "30", "01", "02", "03", "04", "05", "06", "07", "08",
"09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
"31", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
"22", "23", "24", "25", "26", "27", "28", "29", "30", "01", "02",
"03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31"), month = c("01", "01",
"01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01",
"01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01",
"01", "01", "01", "01", "01", "01", "01", "02", "02", "02", "02",
"02", "02", "02", "02", "02", "02", "02", "02", "02", "02", "02",
"02", "02", "02", "02", "02", "02", "02", "02", "02", "02", "02",
"02", "02", "02", "03", "03", "03", "03", "03", "03", "03", "03",
"03", "03", "03", "03", "03", "03", "03", "03", "03", "03", "03",
"03", "03", "03", "03", "03", "03", "03", "03", "03", "03", "03",
"03", "04", "04", "04", "04", "04", "04", "04", "04", "04", "04",
"04", "04", "04", "04", "04", "04", "04", "04", "04", "04", "04",
"04", "04", "04", "04", "04", "04", "04", "04", "04", "05", "05",
"05", "05", "05", "05", "05", "05", "05", "05", "05", "05", "05",
"05", "05", "05", "05", "05", "05", "05", "05", "05", "05", "05",
"05", "05", "05", "05", "05", "05", "05", "06", "06", "06", "06",
"06", "06", "06", "06", "06", "06", "06", "06", "06", "06", "06",
"06", "06", "06", "06", "06", "06", "06", "06", "06", "06", "06",
"06", "06", "06", "06", "07", "07", "07", "07", "07", "07", "07",
"07", "07", "07", "07", "07", "07", "07", "07", "07", "07", "07",
"07", "07", "07", "07", "07", "07", "07", "07", "07", "07", "07",
"07", "07", "08", "08", "08", "08", "08", "08", "08", "08", "08",
"08", "08", "08", "08", "08", "08", "08", "08", "08", "08", "08",
"08", "08", "08", "08", "08", "08", "08", "08", "08", "08", "08",
"09", "09", "09", "09", "09", "09", "09", "09", "09", "09", "09",
"09", "09", "09", "09", "09", "09", "09", "09", "09", "09", "09",
"09", "09", "09", "09", "09", "09", "09", "09", "10", "10", "10",
"10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10",
"10", "10", "10", "10", "10", "10", "10", "10", "10", "10", "10",
"10", "10", "10", "10", "10", "10", "11", "11", "11", "11", "11",
"11", "11", "11", "11", "11", "11", "11", "11", "11", "11", "11",
"11", "11", "11", "11", "11", "11", "11", "11", "11", "11", "11",
"11", "11", "11", "12", "12", "12", "12", "12", "12", "12", "12",
"12", "12", "12", "12", "12", "12", "12", "12", "12", "12", "12",
"12", "12", "12", "12", "12", "12", "12", "12", "12", "12", "12",
"12"), cummeanpcp = c(0.9, 2.4, 3.6, 4.6, 6.1, 7.6, 9.3, 10.7,
12.6, 13.3, 14.5, 15.5, 16.9, 17.4, 18.1, 19.2, 20.2, 21, 21.9,
23, 24.2, 25.2, 26.5, 26.6, 27.2, 28.7, 29.8, 30.6, 31.7, 31.9,
32.8, 34.1, 35.9, 36.7, 37.2, 37.9, 38.7, 39.8, 41.5, 42.3, 43.8,
44.7, 46.5, 47.2, 47.9, 49.4, 51.7, 52.4, 54.4, 55.3, 55.9, 57,
58.5, 59.4, 61, 63.7, 64.4, 66.1, 67.2, 67.2, 68, 68.5, 69, 69.9,
71.4, 72.4, 73.2, 73.3, 73.4, 73.6, 74.3, 75.6, 76.8, 77.6, 78.3,
78.6, 79.5, 79.9, 80.5, 81.5, 81.7, 82.2, 82.9, 83.6, 84, 84.9,
86.2, 88.2, 90, 91.2, 92.3, 93.5, 95.9, 97.6, 99, 100.6, 102.1,
104, 105.5, 106.3, 107.4, 108.7, 110.1, 111.4, 113.6, 115.2,
115.7, 117.8, 119.9, 121.5, 122.3, 123.7, 126.2, 127.4, 128.5,
130.8, 131.9, 133.8, 135.9, 137, 137.5, 139.3, 141, 141.6, 142.5,
143.9, 145, 146.6, 149.2, 150.7, 153.3, 154.9, 155.8, 157.9,
158.8, 160.7, 162.3, 164.9, 165.3, 166.4, 167.2, 168.5, 171.6,
174.1, 178.7, 181.5, 182.7, 183.4, 185, 186.2, 186.9, 188, 189.4,
190.1, 190.6, 192.3, 193.3, 193.8, 194.3, 196.5, 197.9, 198,
198.2, 198.6, 199.5, 200.3, 201.1, 202.3, 203.1, 203.3, 203.5,
203.6, 203.7, 204.3, 205.1, 205.9, 206.8, 207.3, 207.5, 207.6,
207.9, 209, 209.3, 210.5, 211.9, 212.1, 212.1, 212.4, 212.7,
213.1, 213.1, 213.6, 213.7, 214.1, 214.5, 214.7, 214.9, 215.8,
215.8, 216, 216, 216.7, 217.4, 217.5, 217.8, 217.8, 219.3, 219.5,
219.9, 220.2, 220.6, 220.7, 220.7, 220.9, 221, 221.6, 221.7,
222.1, 222.2, 222.4, 222.5, 223.3, 223.4, 223.4, 223.4, 223.4,
223.5, 224, 224, 225.2, 225.3, 226, 226.1, 226.2, 226.7, 226.7,
227, 227.2, 227.3, 228, 228.1, 228.3, 229.2, 230.2, 230.5, 230.5,
230.7, 230.8, 231.1, 231.3, 232.5, 233.5, 235, 235.5, 236.6,
236.6, 236.7, 236.8, 236.8, 237.6, 238.2, 239, 239.9, 240.7,
242.6, 243.3, 243.9, 244.8, 247, 247.8, 248.4, 250.1, 251.3,
252.6, 254.2, 255.8, 256.6, 257.3, 258.3, 258.9, 259.8, 261.6,
264.8, 267, 270.5, 272.4, 273.4, 275.7, 276.4, 280, 282, 285.4,
288.3, 290.9, 292.8, 295.9, 297.1, 298.3, 301.3, 302.9, 306.5,
308.4, 309.4, 311.1, 312.1, 312.5, 314.3, 317.9, 321.1, 323.9,
326.7, 331.3, 334.3, 334.9, 336.1, 338.3, 340.7, 344.4, 347.4,
350.6, 351.6, 352.2, 352.8, 354.3, 356.3, 358.1, 361, 362, 363.3,
364.6, 365.5, 366.1, 366.6, 368, 369.8, 370.8, 372.2, 372.9,
374.2, 376.2, 377.9, 380.2, 381.3, 383.2, 383.7, 385.1, 386.6,
388.2, 388.9, 390, 391.2, 393.2, 394.8, 395.1, 396.7, 399.1,
402.1, 404.3, 405.3, 407.5, 409.2, 411.3, 413.1, 416.1, 418.5,
420.9), cumsumpcp = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 6.4, 10.6,
10.6, 10.6, 10.7, 10.7, 10.7, 10.7, 10.7, 11.1, 11.3, 11.3, 11.6,
11.6, 11.6, 11.6, 11.6, 11.6, 11.7, 11.7, 11.7, 11.7, 11.7, 11.7,
11.7, 11.7, 11.7, 11.7, 11.7, 11.7, 11.7, 12.8, 12.8, 12.8, 12.8,
12.8, 12.8, 12.8, 12.8, 12.8, 12.8, 12.8, 12.8, 12.8, 12.8, 12.8,
12.8, 12.8, 12.8, 13.2, 13.3, 13.3, 13.3, NA, 13.3, 13.3, 13.3,
13.3, 17.5, 18, 19.8, 34.1, 35.2, 35.3, 35.3, 35.3, 35.3, 35.3,
35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3,
35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3,
35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3, 35.3,
35.3, 35.3, 35.3, 35.3, 42.6, 43.1, 43.1, 43.1, 43.1, 43.1, 43.1,
43.1, 43.6, 43.6, 43.6, 43.6, 43.6, 43.6, 43.6, 43.6, 43.6, 43.6,
43.6, 43.6, 43.6, 43.6, 43.6, 45.1, 45.1, 45.1, 45.1, 45.1, 45.1,
45.1, 46, 46.7, 51.5, 51.5, 70.3, 76.6, 87, 94.4, 101.6, 102.2,
102.2, 110.5, 111.5, 111.7, 111.7, 116.2, 119.1, 128.9, 132.2,
132.2, 132.2, 132.2, 134, 134.1, 134.1, 134.1, 134.1, 134.1,
136.7, 137.2, 137.2, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7,
144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7,
144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7,
144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7,
144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7,
144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7,
144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7,
144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7, 144.7,
144.7, 144.7, 144.7, 144.7, 175.9, 242.4, 251.5, 257.7, 257.7,
257.7, 260.5, 264.1, 271.7, 271.7, 271.8, 271.8, 290, 303.3,
303.6, 309.9, 309.9, 310, 310, 310.7, 310.7, 310.7, 310.7, 310.7,
310.7, 310.7, 310.7, 310.7, 310.7, 310.7, 310.7, 310.7, 310.7,
310.7, 310.7, 310.7, 310.7, 310.7, 310.7, 310.7, 310.7, 313.1,
313.1, 316, 316.8, 319.2, 326, 433.8, 433.8, 433.8, 475, 477.2,
479, 479, 482.9, 482.9, 484.9, 491, 491.3, 491.4, 492.8, 510.7,
511.6, 513.2, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), diffmean = c(-0.8,
-2.3, -3.5, -4.5, -6, -7.5, -2.9, -0.0999999999999996, -2, -2.7,
-3.8, -4.8, -6.2, -6.7, -7.4, -8.1, -8.9, -9.7, -10.3, -11.4,
-12.6, -13.6, -14.9, -15, -15.5, -17, -18.1, -18.9, -20, -20.2,
-21.1, -22.4, -24.2, -25, -25.5, -26.2, -27, -27, -28.7, -29.5,
-31, -31.9, -33.7, -34.4, -35.1, -36.6, -38.9, -39.6, -41.6,
-42.5, -43.1, -44.2, -45.7, -46.6, -48.2, -50.5, -51.1, -52.8,
-53.9, NA, -54.7, -55.2, -55.7, -56.6, -53.9, -54.4, -53.4, -39.2,
-38.2, -38.3, -39, -40.3, -41.5, -42.3, -43, -43.3, -44.2, -44.6,
-45.2, -46.2, -46.4, -46.9, -47.6, -48.3, -48.7, -49.6, -50.9,
-52.9, -54.7, -55.9, -57, -58.2, -60.6, -62.3, -63.7, -65.3,
-66.8, -68.7, -70.2, -71, -72.1, -73.4, -74.8, -76.1, -78.3,
-79.9, -80.4, -82.5, -84.6, -86.2, -87, -81.1, -83.1, -84.3,
-85.4, -87.7, -88.8, -90.7, -92.8, -93.4, -93.9, -95.7, -97.4,
-98, -98.9, -100.3, -101.4, -103, -105.6, -107.1, -109.7, -111.3,
-112.2, -114.3, -113.7, -115.6, -117.2, -119.8, -120.2, -121.3,
-122.1, -122.5, -124.9, -122.6, -127.2, -111.2, -106.1, -96.4,
-90.6, -84.6, -84.7, -85.8, -78.9, -78.6, -78.9, -80.6, -77.1,
-74.7, -65.4, -64.3, -65.7, -65.8, -66, -64.6, -65.4, -66.2,
-67, -68.2, -69, -66.6, -66.3, -66.4, -59, -59.6, -60.4, -61.2,
-62.1, -62.6, -62.8, -62.9, -63.2, -64.3, -64.6, -65.8, -67.2,
-67.4, -67.4, -67.7, -68, -68.4, -68.4, -68.9, -69, -69.4, -69.8,
-70, -70.2, -71.1, -71.1, -71.3, -71.3, -72, -72.7, -72.8, -73.1,
-73.1, -74.6, -74.8, -75.2, -75.5, -75.9, -76, -76, -76.2, -76.3,
-76.9, -77, -77.4, -77.5, -77.7, -77.8, -78.6, -78.7, -78.7,
-78.7, -78.7, -78.8, -79.3, -79.3, -80.5, -80.6, -81.3, -81.4,
-81.5, -82, -82, -82.3, -82.5, -82.6, -83.3, -83.4, -83.6, -84.5,
-85.5, -85.8, -54.6, 11.7, 20.7, 26.6, 26.4, 25.2, 27, 29.1,
36.2, 35.1, 35.2, 35.1, 53.2, 66.5, 66, 71.7, 70.9, 70.1, 69.3,
68.1, 67.4, 66.8, 65.9, 63.7, 62.9, 62.3, 60.6, 59.4, 58.1, 56.5,
54.9, 54.1, 53.4, 52.4, 51.8, 50.9, 49.1, 45.9, 43.7, 40.2, 38.3,
39.7, 37.4, 39.6, 36.8, 37.2, 40.6, 145.5, 142.9, 141, 179.1,
180.1, 180.7, 177.7, 180, 176.4, 176.5, 181.6, 180.2, 179.3,
180.3, 196.4, 193.7, 192.1, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
), date = structure(c(19358, 19359, 19360, 19361, 19362, 19363,
19364, 19365, 19366, 19367, 19368, 19369, 19370, 19371, 19372,
19373, 19374, 19375, 19376, 19377, 19378, 19379, 19380, 19381,
19382, 19383, 19384, 19385, 19386, 19387, 19388, 19389, 19390,
19391, 19392, 19393, 19394, 19395, 19396, 19397, 19398, 19399,
19400, 19401, 19402, 19403, 19404, 19405, 19406, 19407, 19408,
19409, 19410, 19411, 19412, 19413, 19414, 19415, 19416, NA, 19417,
19418, 19419, 19420, 19421, 19422, 19423, 19424, 19425, 19426,
19427, 19428, 19429, 19430, 19431, 19432, 19433, 19434, 19435,
19436, 19437, 19438, 19439, 19440, 19441, 19442, 19443, 19444,
19445, 19446, 19447, 19448, 19449, 19450, 19451, 19452, 19453,
19454, 19455, 19456, 19457, 19458, 19459, 19460, 19461, 19462,
19463, 19464, 19465, 19466, 19467, 19468, 19469, 19470, 19471,
19472, 19473, 19474, 19475, 19476, 19477, 19478, 19479, 19480,
19481, 19482, 19483, 19484, 19485, 19486, 19487, 19488, 19489,
19490, 19491, 19492, 19493, 19494, 19495, 19496, 19497, 19498,
19499, 19500, 19501, 19502, 19503, 19504, 19505, 19506, 19507,
19508, 19509, 19510, 19511, 19512, 19513, 19514, 19515, 19516,
19517, 19518, 19519, 19520, 19521, 19522, 19523, 19524, 19525,
19526, 19527, 19528, 19529, 19530, 19531, 19532, 19533, 19534,
19535, 19536, 19537, 19538, 19539, 19540, 19541, 19542, 19543,
19544, 19545, 19546, 19547, 19548, 19549, 19550, 19551, 19552,
19553, 19554, 19555, 19556, 19557, 19558, 19559, 19560, 19561,
19562, 19563, 19564, 19565, 19566, 19567, 19568, 19569, 19570,
19571, 19572, 19573, 19574, 19575, 19576, 19577, 19578, 19579,
19580, 19581, 19582, 19583, 19584, 19585, 19586, 19587, 19588,
19589, 19590, 19591, 19592, 19593, 19594, 19595, 19596, 19597,
19598, 19599, 19600, 19601, 19602, 19603, 19604, 19605, 19606,
19607, 19608, 19609, 19610, 19611, 19612, 19613, 19614, 19615,
19616, 19617, 19618, 19619, 19620, 19621, 19622, 19623, 19624,
19625, 19626, 19627, 19628, 19629, 19630, 19631, 19632, 19633,
19634, 19635, 19636, 19637, 19638, 19639, 19640, 19641, 19642,
19643, 19644, 19645, 19646, 19647, 19648, 19649, 19650, 19651,
19652, 19653, 19654, 19655, 19656, 19657, 19658, 19659, 19660,
19661, 19662, 19663, 19664, 19665, 19666, 19667, 19668, 19669,
19670, 19671, 19672, 19673, 19674, 19675, 19676, 19677, 19678,
19679, 19680, 19681, 19682, 19683, 19684, 19685, 19686, 19687,
19688, 19689, 19690, 19691, 19692, 19693, 19694, 19695, 19696,
19697, 19698, 19699, 19700, 19701, 19702, 19703, 19704, 19705,
19706, 19707, 19708, 19709, 19710, 19711, 19712, 19713, 19714,
19715, 19716, 19717, 19718, 19719, 19720, 19721, 19722), class = "Date")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -366L))
My code so far:
ggplot2::ggplot(data = plot_data, aes(x = date, y = cumsumpcp)) +
ggplot2::geom_segment(aes(xend = date, yend = cummeanpcp, color = diffmean), linewidth = 1.2,
na.rm = TRUE) +
ggplot2::scale_color_gradient2(high = "#2c7bb6", mid = "white", low = "#d7191c") +
ggplot2::geom_line(linewidth = 0.85, lineend = "round", na.rm = TRUE) +
ggplot2::geom_line(aes(y = cummeanpcp), linetype = "longdash", na.rm = TRUE) +
ggthemes::theme_hc(base_size = 15)
Code output:
This is achievable by:
guide = guide_none()
in the scale_color_gradient2
calllinetype = "Cumsumpcp"
and linetype = "Cummeanpcp"
in the two geom_line
sscale_linetype_manual
library(ggplot2)
ggplot(data = plot_data, aes(x = date, y = cumsumpcp)) +
geom_segment(aes(xend = date, yend = cummeanpcp, color = diffmean), linewidth = 1.2,
na.rm = TRUE) +
scale_color_gradient2(high = "#2c7bb6", mid = "white", low = "#d7191c", guide = guide_none()) +
geom_line(aes(linetype = "Cumsumpcp"),
linewidth = 0.85,
lineend = "round",
na.rm = TRUE,
show.legend = FALSE) +
geom_line(aes(y = cummeanpcp, linetype = "Cummeanpcp"), na.rm = TRUE) +
ggthemes::theme_hc(base_size = 15) +
scale_linetype_manual(values = c("Cummeanpcp" = "dashed",
"Cumsumpcp" = "solid")) +
guides(linetype = guide_legend(override.aes = list(
linewidth = c(0.5, 1),
lineend = c("square", "round")
)))