1 year ago

#246944

test-img

Francesca Barazzetta

Scatterpie pie colour changes according to value

I'm using the scatterpie package to produce piecharts on a map. However, the 3 pie segments E, S, AC will have different colours depending on their values at each location. I'm not familiar with scatterpie but I cannot seem to find the best way to perform this operation. I used the cut function within the fill part of the scatterpie code then used scale_fill_manual underneath to assign the colours based on this example link. Anyway here is a copy of what Id like to do. I hope someone can give insight.

library(ggplot2)
library(scatterpie)

world_map <- map_data("world")
ggplot(world_map, aes(x = long, y = lat, group = group)) +
  geom_polygon(fill="lightgray", colour = "white")

# Med Contries
med.countries <- c("Portugal", "Spain", "Albania", "Algeria", "Bosnia and Herzegovina", "Croatia", "Cyprus", "Egypt", "France", "Greece", "Israel","Italy",
                       "Lebanon","Libya", "Malta", "Monaco", "Montenegro","Morocco", "Slovenia", "Spain", "Syria", "Tunisia","Turkey")

map_data = data.frame(MPA=c("A","B","C","E","F","G"), long=c(3.1, 9.1, 20.5, 13.7, 9.7, 5.2), lat=c(42.19, 44.3, 37.4,44.90, 40.85, 43.12), E= c(0.2,0.4,0.3,0.6,0.1,0.7), S=c(0.3, 0.28, 0.5,0.3,0.3,0.15), AC=c(0.5,0.32, 0.2, 0.1, 0.6, 0.15))

med.maps <- map_data("world", region = med.countries)

colours_group = c("olivedrab4", "#0073C2FF", "#EFC000FF",  "darkorange2",  "#CD534CFF") 

ggplot(map_data, aes(x = long, y = lat))+
  geom_polygon(data = med.maps, aes(group = group),fill="lightgray", colour = "white")+
    geom_scatterpie(data = map_data, aes(x= long, y= lat, group=MPA, fill= cut(c(map_data$E, map_data$S, map_data$AC), c(0,0.2,0.4,0.6,0.8))), 
                  cols = c("E", "S", "AC"), pie_scale = 2) + 
  scale_fill_manual("vunrisk", values = colours_group)+
 
  scale_y_continuous(limits=c(35,48))+
  scale_x_continuous(limits=c(0,25))+
                  coord_equal() +
  labs(title = "Piechart") +
  theme_void()+
  theme(legend.position = "right")

r

ggplot2

colors

categorical-data

scatterpie

0 Answers

Your Answer

Accepted video resources