Coder Social home page Coder Social logo

Comments (11)

crazycapivara avatar crazycapivara commented on June 17, 2024

Hi,

data must NOT be JSON. Sorry, I have to update the docs showing how to use R data objects.

You just need to pass the column names for lat and lng via get_position R function, e. g.

scatterplot:

deckgl(latitude = -24.655, longitude = 176.9, zoom = 4) %>%
   add_scatterplot_layer(
      data = quakes,
      getPosition = get_position("lat", "long"),
      getRadius = JS("d => d.mag * 100"),
      radiusScale = 60,
      getColor = JS("d => [Math.sqrt(d.depth) * 20, 130, 10]")
   )

icon:

deckgl(latitude = -24.655, longitude = 176.9, zoom = 4) %>%
   add_icon_layer(
      data = quakes,
      getPosition = get_position("lat", "long"),
      getSize = get_property("mag"),
      getColor = JS("d => [Math.sqrt(d.depth) * 20, 130, 10]")
   )

from deckgl.

crazycapivara avatar crazycapivara commented on June 17, 2024

Another example using data from the deckgl package

sample_data <- deckgl::bart_stations

sample_data$radius <- sqrt(sample_data$exits)

properties <- list(
  getPosition = get_position("lat", "lng"),
  getRadius = get_property("radius"),
  radiusScale = 6,
  getColor = c(255, 140, 20)
)

deck <- deckgl(zoom = 10.5, pitch = 35) %>%
  add_scatterplot_layer(data = sample_data, properties = properties) %>%
  add_mapbox_basemap()

if (interactive()) deck

from deckgl.

crazycapivara avatar crazycapivara commented on June 17, 2024

see also road-safety-example

from deckgl.

 avatar commented on June 17, 2024

It really helped
Many thanks @crazycapivara

from deckgl.

 avatar commented on June 17, 2024

Another example using data from the deckgl package

sample_data <- deckgl::bart_stations

sample_data$radius <- sqrt(sample_data$exits)

properties <- list(
  getPosition = get_position("lat", "lng"),
  getRadius = get_property("radius"),
  radiusScale = 6,
  getColor = c(255, 140, 20)
)

deck <- deckgl(zoom = 10.5, pitch = 35) %>%
  add_scatterplot_layer(data = sample_data, properties = properties) %>%
  add_mapbox_basemap()

if (interactive()) deck

hi @crazycapivara
Some other questions:

head(a)
lng lat
1 116.71148 39.434895
2 116.71148 39.434895
3 116.71148 39.434895
4 116.71144 39.435100
5 116.71144 39.435060
6 116.71140 39.435090

How to get path, Is get_position still valid?

when I try that ,I get nothing
code as follow:
properties <- list(
pickable = TRUE,
widthScale = 20,
widthMinPixels = 2,
getPosition = get_position("lat", "lng"),
getColor = c(255, 140, 20),
getWidth = 5

,getTooltip = get_property("pd")

)

deck <- deckgl(latitude = 38.38687967,longitude = 114.42064498,pitch = 25, zoom = 5.5) %>%
add_path_layer(data = a, properties = properties) %>%
add_mapbox_basemap()

if (interactive()) deck

from deckgl.

 avatar commented on June 17, 2024

properties <- list(
pickable = TRUE,
widthScale = 20,
widthMinPixels = 2,
getPosition = get_position("lat", "lng"),
getColor = c(255, 140, 20),
getWidth = 5
)

deck <- deckgl(latitude = 38.38687967,longitude = 114.42064498,pitch = 25, zoom = 5.5) %>%
add_path_layer(data = a, properties = properties) %>%
add_mapbox_basemap()

if (interactive()) deck

from deckgl.

crazycapivara avatar crazycapivara commented on June 17, 2024

Hi @liygzting,
you need to use the right data format as specified in deckgl-path-layer-api-reference:

// Data format:
[
      {
        path: [[-122.4, 37.7], [-122.5, 37.8], [-122.6, 37.85]],
        name: 'Richmond - Millbrae',
        color: [255, 0, 0]
      },
      ...
]

Furthermore, you need to use the getPath property as shown in the docs.

So, you need a list column in your data.frame containing the coords of the path(es).

Here is an example using the bart_stations data set:

coords <- deckgl::bart_stations[, c("lng", "lat")] %>%
  as.matrix()

data <- tibble::tibble(
  tooltip = "Bart Stations",
  path = list(coords)
)

# Check, if your data format is correct
jsonlite::toJSON(data) %>% print()

properties <- list(
  pickable = TRUE,
  widthScale = 20,
  widthMinPixels = 2,
  getPath = get_property("path"),
  getColor = c(250, 140, 20),
  getWidth = 5,
  getTooltip = get_property("tooltip")
)

deckgl(zoom = 9, pickingRadius = 20) %>%
  add_path_layer(data = data, properties = properties) %>%
  add_icon_layer(
    data = bart_stations,
    getPosition = get_position("lat", "lng"),
    getColor = c(10, 140, 50),
    getTooltip = get_property("name")
  )

from deckgl.

crazycapivara avatar crazycapivara commented on June 17, 2024

Alternatively, you can create the data using a list of lists:

coords <- deckgl::bart_stations[, c("lng", "lat")] %>%
  as.matrix()

data <- list(
  list(
    tooltip = "Bart Stations",
    path = coords
  )
)

from deckgl.

 avatar commented on June 17, 2024

thanks @crazycapivara ,
for path , scatter,icon or grid etc, I change data to tibble not dataframe so that i can plot most of layer.
Does that mean you can just use this?
Or is there anything else to notice?

from deckgl.

crazycapivara avatar crazycapivara commented on June 17, 2024

Nope, you can use a data.frame aswell. A tibble is just a variant and often more comfortable to handle. That's why I prefer it, e. g. when using list columns. Here is how to create a data.frame with list column which works fine:

# coords same as above
df <- data.frame(
  tooltip = "Bart Stations",
  path = I(list(coords))
)

For all layers expecting point data it's fine to have a data.frame with lat and lng columns.

It is planned to support sf objects, see #81.

from deckgl.

 avatar commented on June 17, 2024

thanks @crazycapivara
It really makes sense
Since you told me this good method, I decided to start using tibble too.

from deckgl.

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.