eliocamp / ggnewscale

Multiple Fill, Color and Other Scales in `ggplot2`
https://eliocamp.github.io/ggnewscale/
GNU General Public License v3.0
406 stars 18 forks source link

Does not display two scales #61

Closed laura-martinez00 closed 4 months ago

laura-martinez00 commented 11 months ago

Hello, I am trying to create a gradient colour map with two scales using the following code but the second colour would not be displayed on the map:

setwd("~/Desktop/Master/Data visualization/Data visualization final")

referendum <- read_csv("EU-referendum-result-data (2).csv")

uk_shp <- read_sf("uk_constituencies_2016.shp")

referendum <- referendum |> rename(area_code = Area_Code)

brexit_data <- left_join(uk_shp, referendum, by = "area_code")

West Tyrone

brexit_data$Pct_Turnout[18] <- 61.8 brexit_data$Remain[18] <- 26765 brexit_data$Leave[18] <- 13274 brexit_data$Pct_Remain[18] <- 66.8 brexit_data$Pct_Leave[18] <- 33.2

Upper Bann

brexit_data$Pct_Turnout[17] <- 63.8 brexit_data$Remain[17] <- 24550 brexit_data$Leave[17] <- 27262 brexit_data$Pct_Remain[17] <- 47.4 brexit_data$Pct_Leave[17] <- 52.6

Strangford

brexit_data$Pct_Turnout[16] <- 64.5 brexit_data$Remain[16] <- 18727 brexit_data$Leave[16] <- 23383 brexit_data$Pct_Remain[16] <- 44.5 brexit_data$Pct_Leave[16] <- 55.5

South Down

brexit_data$Pct_Turnout[15] <- 62.4 brexit_data$Remain[15] <- 32076 brexit_data$Leave[15] <- 15625 brexit_data$Pct_Remain[15] <- 67.2 brexit_data$Pct_Leave[15] <- 32.8

South Antrim

brexit_data$Pct_Turnout[14] <- 63.4 brexit_data$Remain[14] <- 21498 brexit_data$Leave[14] <- 22055 brexit_data$Pct_Remain[14] <- 49.4 brexit_data$Pct_Leave[14] <- 50.6

North Down

brexit_data$Pct_Turnout[13] <- 67.7 brexit_data$Remain[13] <- 23131 brexit_data$Leave[13] <- 21046 brexit_data$Pct_Remain[13] <- 52.4 brexit_data$Pct_Leave[13] <- 47.6

North Antrim

brexit_data$Pct_Turnout[12] <- 64.9 brexit_data$Remain[12] <- 18782 brexit_data$Leave[12] <- 30938 brexit_data$Pct_Remain[12] <- 37.8 brexit_data$Pct_Leave[12] <- 62.2

Newry and Armagh

brexit_data$Pct_Turnout[11] <- 63.7 brexit_data$Remain[11] <- 31963 brexit_data$Leave[11] <- 18659 brexit_data$Pct_Remain[11] <- 62.9 brexit_data$Pct_Leave[11] <- 37.1

Mid Ulster

brexit_data$Pct_Turnout[10] <- 61.7 brexit_data$Remain[10] <- 25612 brexit_data$Leave[10] <- 16799 brexit_data$Pct_Remain[10] <- 60.4 brexit_data$Pct_Leave[10] <- 39.6

Lagan Valley

brexit_data$Pct_Turnout[9] <- 66.6 brexit_data$Remain[9] <- 22710 brexit_data$Leave[9] <- 25704 brexit_data$Pct_Remain[9] <- 46.9 brexit_data$Pct_Leave[9] <- 53.1

Foyle

brexit_data$Pct_Turnout[8] <- 57.4 brexit_data$Remain[8] <- 32064 brexit_data$Leave[8] <- 8905 brexit_data$Pct_Remain[8] <- 78.3 brexit_data$Pct_Leave[8] <- 21.7

Fermanagh and South Tyrone

brexit_data$Pct_Turnout[7] <- 67.9 brexit_data$Remain[7] <- 28200 brexit_data$Leave[7] <- 19958 brexit_data$Pct_Remain[7] <- 58.6 brexit_data$Pct_Leave[7] <- 41.4

East Londonderry

brexit_data$Pct_Turnout[6] <- 69.9 brexit_data$Remain[6] <- 21098 brexit_data$Leave[6] <- 19455 brexit_data$Pct_Remain[6] <- 52.0 brexit_data$Pct_Leave[6] <- 48.0

East Antrim

brexit_data$Pct_Turnout[5] <- 65.2 brexit_data$Remain[5] <- 18616 brexit_data$Leave[5] <- 22929 brexit_data$Pct_Remain[5] <- 44.8 brexit_data$Pct_Leave[5] <- 55.2

Belfast West

brexit_data$Pct_Turnout[4] <- 48.9 brexit_data$Remain[4] <- 23099 brexit_data$Leave[4] <- 8092 brexit_data$Pct_Remain[4] <- 74.1 brexit_data$Pct_Leave[4] <- 25.9

Belfast South

brexit_data$Pct_Turnout[3] <- 67.6 brexit_data$Remain[3] <- 30960 brexit_data$Leave[3] <- 13596 brexit_data$Pct_Remain[3] <- 69.5 brexit_data$Pct_Leave[3] <- 30.5

Belfast North

brexit_data$Pct_Turnout[2] <- 57.5 brexit_data$Remain[2] <- 20128 brexit_data$Leave[2] <- 19844 brexit_data$Pct_Remain[2] <- 50.4 brexit_data$Pct_Leave[2] <- 49.6

Belfast East

brexit_data$Pct_Turnout[1] <- 66.5 brexit_data$Remain[1] <- 20728 brexit_data$Leave[1] <- 21918 brexit_data$Pct_Remain[1] <- 48.6 brexit_data$Pct_Leave[1] <- 51.4

brexit_data$Region_Code <- NULL brexit_data$Region <- NULL brexit_data$id <- NULL brexit_data$Area <- NULL

Reproject data to WGS84

brexit_data <- st_transform(brexit_data, 4326)

Customise variables

brexit_data$c_Pct_Leave <- ifelse(brexit_data$Pct_Leave > 50,brexit_data$Pct_Leave, NA ) brexit_data$c_Pct_Remain <- ifelse(brexit_data$Pct_Remain > 50,brexit_data$Pct_Remain, NA )

ggplot() + geom_sf(data = brexit_data, aes(fill = c_Pct_Remain), colour = "white", lwd = 0.1) + scale_fill_gradientn( colors = c("#FF9966", "#FF0000", "#990033"), breaks = c(55, 60, 100), labels = c("50-55%", "55-60%", ">60%"), limits = c(50, 100), name = "Percentage Remain" ) + new_scale_fill() + geom_sf(data = brexit_data, aes(fill = c_Pct_Leave), colour = "white", lwd = 0.1) + scale_fill_gradientn( colors = c("#00FFFF", "#0099CC", "#0000FF"), breaks = c(55, 60, 100), labels = c( "50-55%", "55-60%", ">60%"), limits = c(0, 100), name = "Percentage Leave" ) + theme_minimal()

gg_brexit

EU-referendum-result-data (2).csv

eliocamp commented 7 months ago

Hi, I'm sorry that I totally missed this issue. Unfortunately I cannot run the example because I don't have the uk_constituencies_2016.shp. Is it possible for you to share that dataset? Also, can you provide the output of the code to see what the issue is?

eliocamp commented 4 months ago

I'm closing the issue due to not having a reproducible example. Feel free to comment with a reproducible example and I'll gladly open it up again.