-1

I am doing the map of the world showing profit and loss from different countries. I already plotted the map, I prepared and joined data, Im at the final step.

My dataset is 'data_profit', 'Total_profit' are values Im using (negative and positive ones)- and it's used to fill the map with colors, depending on the value. Rest of the code is map plotting.

ditch_the_axes <- theme(
  axis.text = element_blank(),
  axis.line = element_blank(),
  axis.ticks = element_blank(),
  panel.border = element_blank(),
  panel.grid = element_blank(),
  axis.title = element_blank()
)

terra<-ggplot(data_profit, aes( x = long, y = lat, group = group )) +
  geom_polygon(aes(x = long, y = lat,fill = Total_profit),color = "black")+
  coord_fixed(1.3) +
  theme_bw() +
  ditch_the_axes+
  scale_fill_gradient2( low="black", high="red", space ="Lab" )
data_profit <-
structure(list(long = c(-69.8991241455078, -69.8957061767578, 
-69.9421920776367, -70.004150390625, -70.0661163330078, -70.0508804321289
), lat = c(12.4520015716553, 12.4229984283447, 12.4385251998901, 
12.50048828125, 12.5469722747803, 12.5970697402954), group = c(1, 
1, 1, 1, 1, 1), order = 1:6, region = c("Aruba", "Aruba", "Aruba", 
"Aruba", "Aruba", "Aruba"), Total_profit = c(0, 0, 0, 0, 0, 0
)), row.names = c(NA, 6L), class = "data.frame")

and this is the output map: enter image description here

So, the thing is the final map doesn't show the negative values (which should be in shade of black and grey). I checked whether 'Total_profit' values are numeric (with is.finite and is.numeric). Do you have any idea what to change in the code?

tjebo
  • 21,977
  • 7
  • 58
  • 94
Marta New
  • 13
  • 2
  • 1
    Marta New, welcome to SO! You are receiving down-votes because your question is neither reproducible (we do not have a sample of your data) nor can we see the problem (no image!). Please make this question *reproducible*, including sample data (preferably something we can copy **easily** with `dput(head(x))`), and the plot showing the problem *using the sample data you give us*. This means you need to find a sampling of your data that is specific enough to show the problem but not so big that it jams the web page with bloat. Suggested reading: https://stackoverflow.com/questions/5963269 – r2evans Jul 10 '19 at 15:17
  • Thanks Marta New for adding the data - however, this does not reproduce the example! Before posting a question and code, I would always recommend to run it in an empty session - you will see the result when running your code on the given sample data – tjebo Jul 11 '19 at 09:31

1 Answers1

0

Your question is unfortunately not reproducible, so I created some different sample data. I also suggest using "world map" and geom_map instead of geom_polygon (see below). I assume the problem in your plot is a combination of missing values and the lack of correct limits in scale_..._gradient. Using scale_...gradientn comes with another problem - the asymmetric midpoint. (see here)

library(tidyverse)
library(scales) #load this for the mid-point problem and using scales::rescale (see below)

WorldData <- map_data('world') #easier way to draw countries than using geom_polygon

# In the next steps I am merging your profit data with the world map. I am creating a different sample data frame 
data_profit <- data.frame(region = sample(WorldData$region,6), Total_profit = c(-5, -3, 0, 3, 5, 10))
map_profit <- dplyr::left_join(WorldData, data_profit, by ='region')
#> Warning: Column `region` joining character vector and factor, coercing into
#> character vector

# This plot is the easier one - using scale_fill_gradient(...). No mid-point problem. Note I am specifying the NA color using na.value.
# I am also specifying the limits using limits = ... I am not hard coding this, giving more flexibility for future graphs.
ggplot() +
geom_map(data = map_profit, map = WorldData,
         aes(x = long, y = lat, map_id = region, fill = Total_profit, na.rm = FALSE)) +
  scale_fill_gradient(low="black", high="red", na.value = 'blue', 
                      limits = c(min(map_profit$Total_profit), max(map_profit$Total_profit)))
#> Warning: Ignoring unknown aesthetics: x, y, na.rm


# The next plot comes with the problem of an asymmetric mid-point 
# I therefore rescale the values using rescale
ggplot() +
  geom_map(data = map_profit, map = WorldData,
           aes(x = long, y = lat, map_id = region, fill = Total_profit, na.rm = FALSE)) +
  scale_fill_gradientn(colors = c("black", 'white', "red"), na.value = 'blue', 
                       values = rescale(c(min(map_profit$Total_profit, na.rm = TRUE),0, max(map_profit$Total_profit,na.rm = TRUE))),
                       limits = c(min(map_profit$Total_profit), max(map_profit$Total_profit)))
#> Warning: Ignoring unknown aesthetics: x, y, na.rm

Created on 2019-07-11 by the reprex package (v0.3.0)

tjebo
  • 21,977
  • 7
  • 58
  • 94