Как изменить цвет в функции для ежемесячных графиков? - PullRequest
1 голос
/ 13 марта 2020

У меня есть пространственно-временные данные о мигрирующих видах, и я хочу сделать GIF, показывающий их движение в течение года на ежемесячной основе. Я использую этот код для создания изображения для каждого месяца:

eb  <- #put the dput() data provided below here
cols_m <- setNames( c("blue1", "cyan2", "green3", "greenyellow", "yellow1", "orange1", "orangered", "red3", "mediumvioletred", "darkviolet", "mediumpurple4", "darkblue"), levels(eb$MONTH2))

#create the base map####
library(ggplot2)
library(rnaturalearth)
library(rnaturalearthdata)

world <- ne_countries(scale = "medium", returnclass = "sf")

gg  <- ggplot(data = world) +
  geom_sf(color = "white", fill = "gray45", lwd=0.2) + 
  coord_sf(xlim=c(60,170), ylim=c(-40, 55), expand = FALSE) +
  theme(panel.background = element_rect(fill="gray10"), 
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(),
        axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank())

#function for pictures each month PER SPECIES####
spec_map_fu <- function(MONTH){
  gg + geom_point(data=eb[eb$MONTH==MONTH,], aes(x=LONGITUDE, y=LATITUDE, color=as.factor(MONTH)), size=0.5, alpha=0.5, show.legend = FALSE) +
    scale_color_manual(values=cols_m) +
    theme(strip.text.x = element_text(size=12, color="white", face="bold"), 
          strip.background = element_rect(fill="gray10"), plot.background = element_rect(fill = "gray10"), panel.background = element_rect(fill = "gray10")) +
    annotate("text", x = 60, y=-40, label = "ship", color="white") +
    facet_grid(. ~ COMMON.NAME)
  print(paste0("saving plot", MONTH))
  ggsave(filename = sprintf("%02d.png", MONTH), dpi=100)
}


#save a plot for each month
library(purrr)

seq(from=1, to=12, by=1) %>% 
  map_df(spec_map_fu)

Мне бы хотелось, чтобы цвет точек менялся в зависимости от того, какой месяц нанесен, как указано в cols_m, но только первый цвет используется для всех двенадцати участков. Как я могу сделать так, чтобы в конце у меня была картинка для каждого месяца, используя разные цвета, которые я установил?

Бонусный вопрос: Как я могу добавить соответствующее название месяца к графику над названиями видов ?

Пример данных, сделанных с помощью dput():

structure(list(OBSERVATION.DATE = structure(c(6262, 10252, 10484, 
9300, 6882, 10058, 8285, 10798, 7811, 7757, 3935, 3902, 10772, 
10713, 8610, 3312, 10688, 8474, 8233, 6859, 3940, 358, 10820, 
10853, 10953, 10928, 954, 8866, 7947, 9438, 10944, 5868, 10237, 
6736, 6883, 6339, 9631, 9432, 6259, 6198, 9796, 4025, 10544, 
6891, 7418, 8401, 3935, 9494, 6957, 6945, 16920, 17054, 17153, 
16872, 16864, 16965, 17117, 16964, 16932, 16955, 17141, 16849, 
16866, 16941, 17034, 16947, 16955, 16873, 16819, 16915, 16845, 
17160, 16860, 16838, 16884, 16802, 16930, 16927, 17085, 16863, 
17081, 17088, 16837, 17094, 16848, 16846, 16989, 16844, 16905, 
17085, 17019, 16845, 17107, 17085, 17147, 17027, 16940, 17147, 
16925, 17035, 16131, 16432, 16419, 16417, 16324, 16073, 16370, 
16107, 16360, 16072, 16071, 16081, 16363, 16383, 16074, 16398, 
16076, 16402, 16372, 16425, 16427, 16088, 16130, 16396, 16095, 
16123, 16366, 16413, 16404, 16426, 16400, 16417, 16112, 16425, 
16169, 16353, 16405, 16385, 16371, 16389, 16415, 16279, 16367, 
16428, 16396, 16409, 16370, 16083, 16360, 16398), class = "Date"), 
    COMMON.NAME = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Dollarbird", "Asian Koel", 
    "Pacific Koel"), class = "factor"), LATITUDE = c(14.4382942, 
    3.33714, -13.4336, 3.0960945, -28.0172, 35.514, -27.84, 27.57326, 
    12.174918, -27.2494, -13.08333, 22.2725429, -5.2080576, 16.1003246, 
    14.4385453, -28.30869, -4.25, 1.8610871, -5.6261643, -10.6938, 
    15.1443814, -26.64167, -6.41667, 24.937827, -16.8992644, 
    -26.9277329, 37.6159991, 24.9692525, -31.08333, -35.1655993, 
    -12.659014, -12.506973, -33.5661555, 24.928, -16.9118569, 
    25.2122382, 39.1379995, -31.4300769, 11.4232423, -33.6175927, 
    -35.27722, -20.91382, -34.07, 0.5944456, 0.5944456, 4.933333, 
    -26.09074, 10.2752706, -27.5198, -9.44261, 11.0587881, 19.2352947, 
    11.6762876, 19.3297939, 9.7904882, 20.2113159, 8.6405841, 
    12.7362145, 26.5199162, 13.1189651, 15.4226027, 11.0587881, 
    10.2152602, 13.1251482, 12.8805882, 11.9878325, 13.1189651, 
    12.1085027, 15.43169, 12.8536944, 11.6875401, 13.9818607, 
    12.31273, 18.2887021, 13.0689753, 9.6814038, 27.87943, 11.7453606, 
    10.3042383, 6.2932783, 15.2983633, 15.3988398, 8.165719, 
    12.959246, 9.903407, 26.1519993, 2.2266681, 12.8536944, 29.3880771, 
    9.640774, 5.2010485, 1.400603, 13.0375891, 10.3042383, 11.5076967, 
    10.7973073, 10.9953306, 10.0585898, 13.7313789, 11.9901151, 
    -23.3992842, -34.5972676, -34.9534931, -38.175, -16.2493413, 
    -32.4802254, -16.8613089, -24.85333, -24.7623623, -32.75, 
    -31.2619444, -32.8625742, -12.8470278, -16.2493413, -31.4447925, 
    -16.2493413, -24.85333, -35.3404744, -33.6932, -34.5620963, 
    -16.2493413, -19.45604, -33.8727352, -12.4250111, -27.2764784, 
    -27.5430528, -17.177295, -19.50085, -27.4535203, -30.3394444, 
    -32.8137904, -15.5586556, -32.4335206, -24.465538, -32.8918537, 
    -27.6758, -27.3158952, -31.429997, -27.0929816, -33.8434, 
    -21.052047, -6.0413889, -26.8403078, -36.3977778, -21.1406687, 
    -26.60616, -30.67551, -33.1253, -27.8983513, -37.5561965), 
    LONGITUDE = c(101.3721952, 101.2438202, 132.4168, 101.6582751, 
    153.1647, 126.597, 153.34972, 84.49705, 108.698558, 152.8755, 
    131.08333, 114.175884, 145.8102894, 99.1078377, 101.37218, 
    152.94382, 142.75, 103.8928771, 150.5884557, 142.53373, 120.5873379, 
    153.075, 146.75, 121.530921, 145.7476759, 152.7585411, 126.9629002, 
    112.9985046, 152.75, 149.162364, 143.3473778, 130.949564, 
    150.9257498, 113.017, 145.7544564, 121.6983715, 118.8351202, 
    152.8838931, 107.4278355, 150.4653168, 149.17544, 149.03417, 
    150.77078, 101.4464866, 101.4464866, 100.466667, 150.82348, 
    77.1605229, 152.9261, 147.37347, 76.9488433, 73.0760503, 
    78.1634384, 72.8137638, 76.3045665, 79.4070699, 77.642887, 
    77.5716734, 80.2325555, 80.2503882, 73.8916826, 76.9488433, 
    76.2530458, 80.2417676, 79.7130203, 77.1439254, 80.2503882, 
    78.1528252, 75.03567, 77.5094716, 78.1803417, 74.5694332, 
    76.62383, 74.7760435, 77.5780463, 76.5553361, 98.344495, 
    75.5512126, 79.8313083, 81.2749672, 74.0043783, 73.8649249, 
    98.294177, 74.86168, 78.072681, 91.66327, 102.1520142, 77.5094716, 
    79.5111144, 76.422109, 100.4255533, 103.817997, 79.9839449, 
    79.8313083, 92.7023792, 76.8178439, 76.7392384, 76.3870288, 
    100.5417091, 77.0560348, 150.4887995, 150.895586, 150.7628059, 
    144.325, 145.3168123, 152.3597717, 145.7523966, 152.33619, 
    152.412352, 152.25, 151.2033333, 151.6852283, 132.8171956, 
    145.3168123, 152.468133, 145.3168123, 152.33619, 149.1091812, 
    151.28675, 150.8729643, 145.3168123, 147.06543, 151.1764133, 
    132.9647424, 153.0380112, 152.8611366, 145.45824, 147.29715, 
    153.1010067, 150.46, 151.811211, 144.4457102, 152.5267982, 
    150.92601, 151.7181417, 153.04223, 153.062259, 152.609824, 
    153.163619, 151.07068, 149.1907874, 141.2125, 152.8790474, 
    150.0416667, 148.6324668, 153.01294, 150.52443, 150.2812, 
    153.1111991, 149.7560281), MONTH = c(2, 1, 9, 6, 11, 7, 9, 
    7, 5, 3, 10, 9, 6, 5, 7, 1, 4, 3, 7, 10, 10, 12, 8, 9, 12, 
    12, 8, 4, 10, 11, 12, 1, 1, 6, 11, 5, 5, 10, 2, 12, 10, 1, 
    11, 11, 4, 1, 10, 12, 1, 1, 4, 9, 12, 3, 3, 6, 11, 6, 5, 
    6, 12, 2, 3, 5, 8, 5, 6, 3, 1, 4, 2, 12, 2, 2, 3, 1, 5, 5, 
    10, 3, 10, 10, 2, 10, 2, 2, 7, 2, 4, 10, 8, 2, 11, 10, 12, 
    8, 5, 12, 5, 8, 3, 12, 12, 12, 9, 1, 10, 2, 10, 1, 1, 1, 
    10, 11, 1, 11, 1, 11, 10, 12, 12, 1, 3, 11, 1, 2, 10, 12, 
    11, 12, 11, 12, 2, 12, 4, 10, 12, 11, 10, 11, 12, 7, 10, 
    12, 11, 12, 10, 1, 10, 11), MONTH2 = c("February", "January", 
    "September", "June", "November", "July", "September", "July", 
    "May", "March", "October", "September", "June", "May", "July", 
    "January", "April", "March", "July", "October", "October", 
    "December", "August", "September", "December", "December", 
    "August", "April", "October", "November", "December", "January", 
    "January", "June", "November", "May", "May", "October", "February", 
    "December", "October", "January", "November", "November", 
    "April", "January", "October", "December", "January", "January", 
    "April", "September", "December", "March", "March", "June", 
    "November", "June", "May", "June", "December", "February", 
    "March", "May", "August", "May", "June", "March", "January", 
    "April", "February", "December", "February", "February", 
    "March", "January", "May", "May", "October", "March", "October", 
    "October", "February", "October", "February", "February", 
    "July", "February", "April", "October", "August", "February", 
    "November", "October", "December", "August", "May", "December", 
    "May", "August", "March", "December", "December", "December", 
    "September", "January", "October", "February", "October", 
    "January", "January", "January", "October", "November", "January", 
    "November", "January", "November", "October", "December", 
    "December", "January", "March", "November", "January", "February", 
    "October", "December", "November", "December", "November", 
    "December", "February", "December", "April", "October", "December", 
    "November", "October", "November", "December", "July", "October", 
    "December", "November", "December", "October", "January", 
    "October", "November")), row.names = c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 
45L, 46L, 47L, 48L, 49L, 50L, 128126L, 128127L, 128128L, 128129L, 
128130L, 128131L, 128132L, 128133L, 128134L, 128135L, 128136L, 
128137L, 128138L, 128139L, 128140L, 128141L, 128142L, 128143L, 
128144L, 128145L, 128146L, 128147L, 128149L, 128150L, 128151L, 
128152L, 128153L, 128154L, 128155L, 128156L, 128157L, 128158L, 
128159L, 128160L, 128161L, 128162L, 128163L, 128164L, 128165L, 
128166L, 128167L, 128168L, 128169L, 128170L, 128171L, 128172L, 
128173L, 128174L, 128175L, 128176L, 353361L, 353362L, 353363L, 
353364L, 353365L, 353366L, 353367L, 353368L, 353369L, 353370L, 
353371L, 353372L, 353373L, 353374L, 353375L, 353376L, 353377L, 
353378L, 353379L, 353380L, 353381L, 353382L, 353383L, 353384L, 
353385L, 353386L, 353387L, 353388L, 353389L, 353390L, 353391L, 
353392L, 353393L, 353394L, 353395L, 353396L, 353397L, 353398L, 
353399L, 353400L, 353401L, 353402L, 353403L, 353404L, 353405L, 
353406L, 353407L, 353408L, 353409L, 353410L), class = "data.frame")

1 Ответ

1 голос
/ 21 марта 2020

Вам нужно изменить свой scale_color_manual, чтобы взять соответствующий цвет из вашего cols_m за каждый месяц: scale_color_manual(values=cols_m[MONTH]). Вы также можете использовать ggtitle вместе с month.abb вектором, чтобы добавить название месяца к своим графикам. Поскольку у вас темный фон, я бы также изменил цвет текста заголовка в theme.

Следующие работы для вас:

#function for pictures each month PER SPECIES####
spec_map_fu <- function(MONTH){

  gg + geom_point(data=eb[eb$MONTH==MONTH,], 
                        aes(x=LONGITUDE, y=LATITUDE, color=as.factor(MONTH)), 
                        size=0.5, alpha=0.5, show.legend = FALSE) +
    scale_color_manual(values=cols_m[MONTH]) +
    theme(strip.text.x = element_text(size=12, color="white", face="bold"), 
          strip.background = element_rect(fill="gray10"), 
          plot.background = element_rect(fill = "gray10"), 
          panel.background = element_rect(fill = "gray10"),
          plot.title = element_text(color="White", size=14, face="bold.italic")) +
    annotate("text", x = 60, y=-40, label = "ship", color="white") +
    facet_grid(. ~ COMMON.NAME) +
    ggtitle(month.abb[MONTH])

  print(paste0("saving plot", MONTH))
  ggsave(filename = sprintf("%02d.png", MONTH), dpi=100)

}

Пример февраля с заголовком и точками в cyan2 (см., например, юг Индии в средней панели).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...