The data is:
d.fit.spatial.1914 <- structure(list(country = c("Albania", "Austria", "Belgium", "Netherlands",
"Poland", "Portugal", "Denmark", "Germany", "Greece", "Spain",
"Bulgaria", "Switzerland", "Romania", "Serbia", "Czech", "Hungary",
"Croatia", "Slovenia", "Slovakia", "Sweden", "Italy", "Turkey",
"Ireland", "Lithuania", "Luxembourg", "Latvia", "Montenegro",
"Macedonia", "United Kingdom", "France"), year = c(1914, 1914,
1914, 1914, 1914, 1914, 1914, 1914, 1914, 1914, 1914, 1914, 1914,
1914, 1914, 1914, 1914, 1914, 1914, 1914, 1914, 1914, 1914, 1914,
1914, 1914, 1914, 1914, 1914, 1914), fit.0.025quant = c(0.434655240136129,
0.284655241371776, 0.720674142241487, 0.384655241369517, 0.18465524138084,
0.83465486750421, 0.932497202050029, 0.584655240989832, 0.984655241367537,
0.784655241373554, 0.0345030032830154, 0.984655241376334, 0.284655225269347,
0.684655241365249, 0.284655241372936, 0.284655241362455, 0.284655241377793,
0.284655241376081, 0.284655241371874, 0.78249311930548, 0.434655241367893,
0.434655240132238, 0.884655241344018, 0.184655241378209, 0.834655241370662,
0.184655241370411, 0.434655240134927, 0.434655240126972, 0.884655241345161,
0.884655241368697), fit.mean = c(0.450091896365781, 0.300091897258075,
0.802571964680874, 0.40009189725807, 0.200091897258009, 0.850091616757218,
0.948236426452339, 0.600091896986149, 1.00009189725808, 0.800091897258069,
0.0500915893805267, 1.00009189725808, 0.300091885435822, 0.700091897258069,
0.300091897258072, 0.300091897258073, 0.300091897258071, 0.300091897258071,
0.300091897258073, 0.798233429108366, 0.45009189725807, 0.450091896365781,
0.90009189725807, 0.200091897258009, 0.850091897258064, 0.200091897258008,
0.450091896365779, 0.450091896365786, 0.900091897258078, 0.900091897258063
), fit.0.975quant = c(0.465555777310772, 0.31555577856971, 0.884464486582753,
0.415555778571938, 0.215555778560413, 0.86555540195537, 0.963452930309468,
0.615555778192601, 1.015555778574, 0.81555577856788, 0.0657080548000513,
1.0155557785652, 0.315555762328393, 0.715555778576285, 0.315555778568526,
0.315555778579083, 0.315555778563714, 0.315555778565382, 0.315555778569618,
0.813449285348024, 0.465555778573548, 0.465555777314699, 0.915555778597406,
0.215555778563069, 0.865555778570851, 0.215555778570873, 0.465555777311962,
0.465555777319966, 0.915555778596324, 0.915555778572741)), row.names = 3421:3450, class = "data.frame")
I want to plot it as:
get_eurostat_geospatial(resolution = 10,
nuts_level = 0,
year = 2016)
SHP_0 <- get_eurostat_geospatial(resolution = 10,
nuts_level = 0,
year = 2016)
SHP_0 <- SHP_0[-c(13, 17, 25, 26, 28, 29, 36), ]
SHP_0 %>% ggplot() + geom_sf() + scale_x_continuous(limits = c(-8, 26)) + scale_y_continuous(limits = c(35, 57))
SHP_0$fit.1914 <- d.fit.spatial.1914$fit.mean
SHP_0 %>% ggplot(aes(fill = fit.1914)) + geom_sf() + scale_fill_continuous(
name = "fitted values") + scale_x_continuous(limits = c(-8, 26)) + scale_y_continuous(limits = c(35, 57)) + theme_void()
Which is gonna produce the map as:
My question is the following:
fit.mean variable which gets plotted in the map is a continuous variable stretching on a 21-scale: 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.
I want to manually fix every specific value of fit.mean to a specific color. Make something like this: "0" = "black", '0.05' = "light black", "0.1" = "gray", and so on, up to "1"?
Do you think it is possible to add this information to the code and then plot the map?
QUESTION UPDATE:
The data is:
d.fit.spatial.1805 <- structure(list(country = c("Albania", "Austria", "Belgium", "Netherlands",
"Poland", "Portugal", "Denmark", "Germany", "Greece", "Spain",
"Bulgaria", "Switzerland", "Romania", "Serbia", "Czech", "Hungary",
"Croatia", "Slovenia", "Slovakia", "Sweden", "Italy", "Turkey",
"Ireland", "Lithuania", "Luxembourg", "Latvia", "Montenegro",
"Macedonia", "United Kingdom", "France"), year = c(1805, 1805,
1805, 1805, 1805, 1805, 1805, 1805, 1805, 1805, 1805, 1805, 1805,
1805, 1805, 1805, 1805, 1805, 1805, 1805, 1805, 1805, 1805, 1805,
1805, 1805, 1805, 1805, 1805, 1805), fit.0.025quant = c(-0.016166293819701,
-0.0581544272902701, -0.148534056205924, -0.075655597471402,
-0.0161662938184579, -0.0161050665610848, -0.0161662938171779,
-0.0165435242509537, -0.154547828396413, -0.0161655664527505,
-0.0161662938171813, 0.083833706183544, -0.0161662938183122,
-0.288330122979802, -0.0581544272911485, -0.0581544272905173,
-0.0581544272899457, -0.0581544272914977, -0.0581544272903846,
-0.0161659131434106, -0.27565559747508, -0.0161662938195299,
0.383833706156319, -0.0161662938186588, -0.0756555974709727,
-0.0161662938251493, -0.0161662938216207, -0.0161662938178196,
0.383833706172539, 0.0838337061800731), fit.mean = c(7.19524193427133e-07,
1.16390204029805e-05, 0.259698900787099, 0.178117305167472, 7.19524193854208e-07,
4.75057466539724e-05, 7.19524193333879e-07, -4.67561111578645e-05,
0.221081854734691, 1.23018298593547e-06, 7.1952419392722e-07,
0.100000719525193, 7.19524194115432e-07, 0.112003560708191, 1.1639020403944e-05,
1.16390204042395e-05, 1.16390204042116e-05, 1.16390204045057e-05,
1.16390204041033e-05, 9.73471048775933e-07, -0.021882694836441,
7.19524193784702e-07, 0.400000719524191, 7.19524193865506e-07,
0.178117305167472, 7.19524193336303e-07, 7.19524193615361e-07,
7.19524193505524e-07, 0.400000719524195, 0.100000719525192),
fit.0.975quant = c(0.0161672403229479, 0.0581780465522874,
0.667987216093969, 0.431920940052196, 0.0161672403217052,
0.0162307112686135, 0.0161672403204262, 0.0164293246765442,
0.596752979869224, 0.0161679742980705, 0.0161672403204283,
0.116167240323243, 0.016167240321559, 0.512390899959434,
0.0581780465531677, 0.0581780465525372, 0.0581780465519659,
0.0581780465535179, 0.0581780465524048, 0.0161676206102999,
0.231920940048006, 0.0161672403227774, 0.416167240346926,
0.0161672403219055, 0.431920940051759, 0.0161672403283977,
0.0161672403248681, 0.0161672403210674, 0.416167240330704,
0.116167240326714)), row.names = 151:180, class = "data.frame")
The code to plot a map is:
get_eurostat_geospatial(resolution = 10,
nuts_level = 0,
year = 2016)
SHP_0 <- get_eurostat_geospatial(resolution = 10,
nuts_level = 0,
year = 2016)
SHP_0 <- SHP_0[-c(13, 17, 25, 26, 28, 29, 36), ]
SHP_0 %>% ggplot() + geom_sf() + scale_x_continuous(limits = c(-8, 26)) + scale_y_continuous(limits = c(35, 57))
SHP_0$fit.1805 <- d.fit.spatial.1805$fit.mean
my_colours <- c("#440154FF", "#481467FF", "#482576FF", "#463480FF",
"#414487FF", "#3B528BFF", "#35608DFF", "#2F6C8EFF",
"#2A788EFF", "#25848EFF", "#21908CFF", "#1E9C89FF",
"#22A884FF", "#2FB47CFF", "#43BF71FF", "#5DC863FF",
"#7AD151FF", "#9AD93CFF", "#BBDF27FF", "#DEE318FF",
"#FDE725FF")
SHP_0 %>%mutate(fit.1805 = factor(round(fit.1805, 2), levels = seq(0, 1, 0.05))) %>% ggplot(aes(fill = fit.1805)) + geom_sf() + scale_x_continuous(limits = c(-8, 26)) + scale_y_continuous(limits = c(35, 57)) + scale_fill_manual(values = my_colours, drop = FALSE) + theme_void()
Which produces the map:
The question is the following:
Why the map plots NA for some countries? If it is because some of them like Italy has a negative value, then Germany, for example, also has a negative value, but it got a proper color in the map.
How can I assign proper colors instead of NAs in the map?
It sounds like you could use a binned scale:
SHP_0 %>%
ggplot(aes(fill = fit.1914)) +
geom_sf() +
scale_fill_viridis_b(name = "fitted values", breaks = seq(0, 1, 0.05)) +
scale_x_continuous(limits = c(-8, 26)) +
scale_y_continuous(limits = c(35, 57)) +
theme_void() +
theme(legend.key.height = unit(2, 'cm'))
If you really want to have a specified color for each level, you could do something like:
my_colours <- c("#F8766D", "#EB8335", "#DA8F00", "#C49A00",
"#A9A400", "#86AC00", "#53B400", "#00BA38",
"#00BE6D", "#00C094", "#00C0B5", "#00BDD2",
"#00B6EB", "#00ABFD", "#619CFF", "#A58AFF",
"#D078FF", "#EC69EF", "#FB61D7", "#FF63B9",
"#FF6B96")
SHP_0 %>%
mutate(fit.1914 = factor(round(fit.1914, 2), levels = seq(0, 1, 0.05))) %>%
ggplot(aes(fill = fit.1914)) +
geom_sf() +
scale_x_continuous(limits = c(-8, 26)) +
scale_y_continuous(limits = c(35, 57)) +
scale_fill_manual(values = my_colours, drop = FALSE) +
theme_void()
Just replace the hex codes in my_colours
with either colour names or your own hex codes.