如何在图上添加图例?

时间:2019-04-10 14:24:28

标签: r ggplot2 legend

我有一个想要添加图例的图形:

enter image description here

到目前为止,我已经能够添加一个默认使用其自身颜色的图例,或将其更改为我不想要的渐变填充的图例。

我在做什么错,我该如何解决?

地块代码:

ggplot(df) +
  geom_line(aes(date, actual, group = 1),
            colour = "forestgreen") +
  geom_line(aes(date, plan, group = 1),
            colour = "red") +
  geom_smooth(aes(date, difference),
              method = "auto",
              se = FALSE,
              linetype = "dashed") +
  scale_x_date(date_breaks = "4 days" , date_labels = "%d-%b-%y") +
  facet_grid(sector ~ ., scales = "free")

dput

structure(list(date = structure(c(17959, 17959, 17959, 17959, 
17959, 17960, 17960, 17960, 17960, 17960, 17961, 17961, 17961, 
17961, 17961, 17962, 17962, 17962, 17962, 17962, 17963, 17963, 
17963, 17963, 17963, 17964, 17964, 17964, 17964, 17964, 17965, 
17965, 17965, 17965, 17965, 17966, 17966, 17966, 17966, 17966, 
17967, 17967, 17967, 17967, 17967, 17968, 17968, 17968, 17968, 
17968, 17969, 17969, 17969, 17969, 17969, 17970, 17970, 17970, 
17970, 17970, 17971, 17971, 17971, 17971, 17971, 17972, 17972, 
17972, 17972, 17972, 17973, 17973, 17973, 17973, 17973, 17974, 
17974, 17974, 17974, 17974, 17975, 17975, 17975, 17975, 17975, 
17976, 17976, 17976, 17976, 17976, 17977, 17977, 17977, 17977, 
17977, 17978, 17978, 17978, 17978, 17978, 17979, 17979, 17979, 
17979, 17979, 17980, 17980, 17980, 17980, 17980, 17981, 17981, 
17981, 17981, 17981, 17982, 17982, 17982, 17982, 17982, 17983, 
17983, 17983, 17983, 17983, 17984, 17984, 17984, 17984, 17984, 
17985, 17985, 17985, 17985, 17985, 17986, 17986, 17986, 17986, 
17986, 17987, 17987, 17987, 17987, 17987, 17988, 17988, 17988, 
17988, 17988, 17989, 17989, 17989, 17989, 17989, 17990, 17990, 
17990, 17990, 17990, 17991, 17991, 17991, 17991, 17991, 17992, 
17992, 17992, 17992, 17992, 17993, 17993, 17993, 17993, 17993
), class = "Date"), sector = structure(c(1L, 2L, 3L, 4L, 5L, 
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("ADVANCE CLINICIAN", 
"CALL HANDLERS", "CLINICIANS", "GP", "PHARMACIST"), class = "factor"), 
    actual = c(16L, 232L, 104L, 40L, 12L, 23L, 231L, 90L, 47L, 
    12L, 34L, 245L, 109L, 61L, 0L, 32L, 226L, 99L, 50L, 0L, 25L, 
    247L, 103L, 58L, 7L, 84L, 362L, 89L, 57L, 8L, 50L, 333L, 
    86L, 44L, 11L, 14L, 276L, 71L, 68L, 7L, 24L, 263L, 93L, 62L, 
    12L, 42L, 241L, 92L, 42L, 13L, 42L, 242L, 106L, 60L, 8L, 
    50L, 262L, 101L, 58L, 8L, 38L, 340L, 80L, 42L, 0L, 32L, 312L, 
    73L, 39L, 8L, 14L, 219L, 81L, 54L, 15L, 19L, 239L, 100L, 
    50L, 20L, 15L, 245L, 104L, 58L, 13L, 38L, 233L, 90L, 57L, 
    8L, 50L, 236L, 94L, 41L, 7L, 78L, 370L, 106L, 61L, 8L, 34L, 
    328L, 91L, 45L, 8L, 16L, 247L, 54L, 57L, 20L, 19L, 263L, 
    93L, 43L, 15L, 26L, 231L, 88L, 58L, 13L, 50L, 234L, 87L, 
    61L, 8L, 68L, 245L, 113L, 50L, 15L, 68L, 352L, 89L, 38L, 
    8L, 22L, 288L, 98L, 18L, 11L, 13L, 201L, 34L, 40L, 8L, 12L, 
    208L, 41L, 45L, 8L, 5L, 215L, 48L, 45L, 0L, 53L, 210L, 45L, 
    55L, 0L, 20L, 220L, 71L, 23L, 8L, 12L, 349L, 80L, 54L, 8L, 
    2L, 287L, 52L, 35L, 0L), plan = c(43L, 291L, 28L, 32L, 11L, 
    37L, 262L, 25L, 32L, 11L, 39L, 260L, 22L, 32L, 11L, 34L, 
    266L, 90L, 32L, 11L, 39L, 269L, 93L, 32L, 11L, 66L, 422L, 
    152L, 61L, 14L, 54L, 373L, 133L, 53L, 14L, 43L, 291L, 106L, 
    32L, 11L, 37L, 262L, 90L, 32L, 11L, 39L, 260L, 94L, 32L, 
    11L, 34L, 266L, 90L, 32L, 11L, 39L, 269L, 93L, 32L, 11L, 
    66L, 422L, 152L, 61L, 14L, 54L, 373L, 133L, 53L, 14L, 43L, 
    291L, 106L, 32L, 11L, 37L, 262L, 90L, 32L, 11L, 39L, 260L, 
    94L, 32L, 11L, 34L, 266L, 90L, 32L, 11L, 39L, 269L, 93L, 
    32L, 11L, 66L, 422L, 152L, 61L, 14L, 54L, 373L, 133L, 53L, 
    14L, 43L, 291L, 106L, 32L, 11L, 37L, 262L, 90L, 32L, 11L, 
    39L, 260L, 94L, 32L, 11L, 34L, 266L, 90L, 32L, 11L, 39L, 
    269L, 93L, 32L, 11L, 66L, 422L, 152L, 61L, 14L, 54L, 373L, 
    133L, 53L, 14L, 43L, 283L, 73L, 32L, 11L, 37L, 283L, 99L, 
    32L, 11L, 39L, 264L, 88L, 32L, 11L, 34L, 260L, 90L, 32L, 
    11L, 39L, 275L, 98L, 32L, 11L, 66L, 424L, 156L, 61L, 14L, 
    54L, 360L, 130L, 53L, 14L), difference = c(-27L, -59L, 76L, 
    8L, 1L, -14L, -31L, 65L, 15L, 1L, -5L, -15L, 87L, 29L, -11L, 
    -2L, -40L, 9L, 18L, -11L, -14L, -22L, 10L, 26L, -4L, 18L, 
    -60L, -63L, -4L, -6L, -4L, -40L, -47L, -9L, -3L, -29L, -15L, 
    -35L, 36L, -4L, -13L, 1L, 3L, 30L, 1L, 3L, -19L, -2L, 10L, 
    2L, 8L, -24L, 16L, 28L, -3L, 11L, -7L, 8L, 26L, -3L, -28L, 
    -82L, -72L, -19L, -14L, -22L, -61L, -60L, -14L, -6L, -29L, 
    -72L, -25L, 22L, 4L, -18L, -23L, 10L, 18L, 9L, -24L, -15L, 
    10L, 26L, 2L, 4L, -33L, 0L, 25L, -3L, 11L, -33L, 1L, 9L, 
    -4L, 12L, -52L, -46L, 0L, -6L, -20L, -45L, -42L, -8L, -6L, 
    -27L, -44L, -52L, 25L, 9L, -18L, 1L, 3L, 11L, 4L, -13L, -29L, 
    -6L, 26L, 2L, 16L, -32L, -3L, 29L, -3L, 29L, -24L, 20L, 18L, 
    4L, 2L, -70L, -63L, -23L, -6L, -32L, -85L, -35L, -35L, -3L, 
    -30L, -82L, -39L, 8L, -3L, -25L, -75L, -58L, 13L, -3L, -34L, 
    -49L, -40L, 13L, -11L, 19L, -50L, -45L, 23L, -11L, -19L, 
    -55L, -27L, -9L, -3L, -54L, -75L, -76L, -7L, -6L, -52L, -73L, 
    -78L, -18L, -14L)), row.names = c(NA, -175L), class = c("tbl_df", 
"tbl", "data.frame"))

1 个答案:

答案 0 :(得分:1)

function encrypt(plainText, keyBase64, ivBase64) { var key = Buffer.from(keyBase64, 'base64'); var iv = Buffer.from(ivBase64, 'base64'); var cipher = crypto.createCipheriv(getAlgorithm(keyBase64), key, iv); let cip = cipher.update(plainText, 'utf8', 'base64') cip += cipher.final('base64'); return cip; }; function decrypt (messagebase64, keyBase64, ivBase64) { var key = Buffer.from(keyBase64, 'base64'); var iv = Buffer.from(ivBase64, 'base64'); var decipher = crypto.createDecipheriv(getAlgorithm(keyBase64), key, iv); let dec = decipher.update(messagebase64, 'base64'); dec += decipher.final(); return dec; } 自变量放在colour中,然后在其中写下标签。您可以使用aes重命名标题,并使用+labs(colour="Legend Title")

处理颜色
scale_fill_manual(values)

希望这会有所帮助!