Giter VIP home page Giter VIP logo

Comments (7)

kdm9 avatar kdm9 commented on September 15, 2024 1

The only further thought I have is "oops, I forgot to upload this" :)

I don't have time right now to make this a nice function, but for those who are searching, here's my plotting code. Hopefully, I'll soon have time and remember to turn this into a function within the conStruct package, but in the mean time, here goes:

#' # Construct-related plots for the paper

library(tidyverse)
library(ggspatial)
library(ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(ggforce)
library(foreach)
theme_set(theme_bw())

#' # Fancy piemaps
#'
#' This looted from https://stackoverflow.com/questions/51398344/r-pie-charts-distorted-when-adding-to-projected-map-using-ggplot


world = map_data("world", resolution=0)
basem = ggplot(data=world, aes(x=long, y=lat, group=group)) + 
  geom_polygon(color = "grey", fill="white") + 
  coord_quickmap(xlim = c(-12, 55), ylim = c(34, 60)) +
  ylab("Latitude") + 
  xlab("Longitude") + 
  theme(
    panel.background = element_rect(fill = "lightsteelblue2"),
    panel.grid = element_blank(), 
    legend.position = "top")

basem


pie.list = csvmap_data %>% 
  tidyr::nest(c(pop, proportion)) %>%
  # make a pie chart from each row, & convert to grob
  mutate(pie.grob = purrr::map(data,
                               function(d) ggplotGrob(ggplot(d, aes(x = 1, y = proportion, fill = pop)) +
                                                      geom_col(show.legend = FALSE) +
                                                      scale_fill_manual(values=gautam.pal.k3) +
                                                      coord_polar(theta = "y") +
                                                      theme_void()))) %>%
  # convert each grob to an annotation_custom layer.
  rowwise() %>%
  mutate(radius = 0.9) %>%
  mutate(subgrob = list(annotation_custom(grob = pie.grob,
                                          xmin = longitude - radius, xmax = longitude + radius,
                                          ymin = latitude  - radius, ymax = latitude + radius)))

basem + 
    pie.list %>%
    filter(mdl=="sp", K==3) %>%
    pull(subgrob)

basem + 
    pie.list %>%
    filter(mdl=="sp", K==2) %>%
    pull(subgrob)

from construct.

petrelharp avatar petrelharp commented on September 15, 2024

Hi, @kdm9 - glad it's being helpful! And gee - it looks to me like the distortion in the first map is coming from being squished horizontally, not from the map projection - here's a zoom in on a northern and a southern pie plot in exactly the same rectangle:
Screenshot from 2021-08-31 07-37-48
Screenshot from 2021-08-31 07-37-42

This can happen if you plot to a window in R and then save it out as a pdf to a different aspect ratio image - how are you saving it to a file?

from construct.

kdm9 avatar kdm9 commented on September 15, 2024

Hi Peter,

Thanks for your help! Yes, it's odd isn't it.

I'm using ggsave, with an a4-compatible width and height (6" by 7"). It is a bit suspicious, but I don't think the save aspect ratio is the direct cause. See for example below, which is what happens when you use an export size of 6" by 20" wide. The plot isn't stretched, it seems to keep its own native aspect ratio (nb the large white margins on each side). Additionally, if one scales points to the equator to the poles and then plot, one can see a trend of squishedness from the equator to the poles.

k2-piemap-big

from construct.

petrelharp avatar petrelharp commented on September 15, 2024

Ah, ok, you're right. How about this?
https://stackoverflow.com/questions/51398344/r-pie-charts-distorted-when-adding-to-projected-map-using-ggplot

from construct.

kdm9 avatar kdm9 commented on September 15, 2024

Ah, fantastic! Many thanks. I googled extensively, but looks like I missed that SO question/answer. I'd be happy to write a small section for the construct vignette with example code once it's working if that's of interest, since this is something others probably could use too.

from construct.

gbradburd avatar gbradburd commented on September 15, 2024

That'd be great Kevin - thanks!

from construct.

gbradburd avatar gbradburd commented on September 15, 2024

Hey Kevin - I'm going to close this issue, but feel free to reopen if you have further thoughts/problems with this!

from construct.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.