Code Monkey home page Code Monkey logo

Comments (7)

oswaldosantos avatar oswaldosantos commented on August 18, 2024

Post a reproducible example please

from ggsn.

mattbk avatar mattbk commented on August 18, 2024

It looks like a mismatch between this package and coord_sf() from ggplot2.

library(ggplot2)

x = c(1000:2000)
y = c(100:200)

data <- data.frame( x = rep( x, each=1 ),
                  y = rep( y,  length(x)),
                  z = rnorm( x ))

xlim.set<-range(data$x)
ylim.set<-range(data$y)

ggplot() +
  geom_tile(data=data,
            aes_string(x="x",
                       y="y",
                       fill="z")) +
  ggsn::scalebar(location="bottomleft",
                 dist = .2, #km
                 x.min = xlim.set[1],
                 x.max = xlim.set[2],
                 y.min = ylim.set[1],
                 y.max = ylim.set[2],
                 dd2km = F,
                 dist_unit = "m",
                 st.bottom = F) +
  coord_sf(datum = NA, #remove graticule per https://github.com/tidyverse/ggplot2/issues/2071
           xlim=xlim.set,
           ylim=ylim.set)

With coord_sf():

image

Without coord_sf():

image

from ggsn.

oswaldosantos avatar oswaldosantos commented on August 18, 2024

Try with higher values for st.dist and height.

from ggsn.

mattbk avatar mattbk commented on August 18, 2024

So I should rename this issue "work with coord_sf()" then.

from ggsn.

oswaldosantos avatar oswaldosantos commented on August 18, 2024

If there is a problem with scalebar() and coord_sf(), yes. However, you should provide a reproducible example and feedback for comments. The code you previously provided throw errors (Error in roundup(diff(xlim.set)/10) : could not find function "roundup" and Error in FUN(X[[i]], ...) : object 'value' not found) and does not has the code for the plots. Furthermore, you did not give any feedback about my comment, it did not work? Even after fixing the errors and writing the missing code for the plots, I got the expected behavior using height = .5 and st.dist = .7.
Please provide a reproducible example a reply to comments to try to help you.

from ggsn.

mattbk avatar mattbk commented on August 18, 2024

I've updated the example to make it reproducible.

Manually adjusting height and st.dist will not work for my use case because I am generating multiple maps at once that potentially cover different extents and may have different amounts of aspect ratio modifications with coord_sf().

from ggsn.

oswaldosantos avatar oswaldosantos commented on August 18, 2024

Compare the plot using coord_sf() with and without scalebar(). I do not see conflicts between the functions. scalebar() adds a scale bar with and height equal to a fraction hegiht of the y axis, and the text is away of the scale bar by a fraction st.dist of the y axis. If you generate multiple maps with different extents, height and st.dist will be a proportion relative to the each one of the particular extents (the proportion will be the same by the aboslute scale bar height and text distant will vary according to the extent).

ggplot() +
  geom_tile(data=data,
            aes_string(x="x",
                       y="y",
                       fill="z")) +
  coord_sf(datum = NA,
           xlim=xlim.set,
           ylim=ylim.set)

f1

ggplot() +
  geom_tile(data=data,
            aes_string(x="x",
                       y="y",
                       fill="z")) +
  ggsn::scalebar(location="bottomleft",
                 dist = .2, #km
                 x.min = xlim.set[1],
                 x.max = xlim.set[2],
                 y.min = ylim.set[1],
                 y.max = ylim.set[2],
                 dd2km = F,
                 dist_unit = "m",
                 st.bottom = F,
                 st.dist = .12,
                 height = .1) +
  coord_sf(datum = NA,
           xlim=xlim.set,
           ylim=ylim.set)

f2

from ggsn.

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.