1

I want to make 3D map with rayshader. I have seen this web https://www.tylermw.com/3d-ggplots-with-rayshader/ and try the code with my data. In this web, height map has black color but not on my output. Are there any way to get result like that? Thanks..

I use this code.

map <- sf::st_read('map/map.shp', quiet = TRUE)
gg = ggplot(map) +
   geom_sf(aes(fill =AREA),linewidth=0.7,colour='black',inherit.aes=FALSE) +
    scale_fill_viridis('Area',na.value = 'white')+
   theme(axis.line = element_blank(),axis.title = element_blank(),
         axis.ticks = element_blank(), axis.text = element_blank())
plot_gg(gg, multicore = TRUE, width = 6 ,height=2.7, fov = 70,offset_edges = TRUE)

This is snapshot my result. my output

This is my expect. my expect

Eriza
  • 25
  • 4

2 Answers2

3

Obviously, you are using new version of ggplot2 3.4.0. I think your issue is not about adding color but more likely to do with newer version of ggplot2, specifically new linewidth(). Your color is still correct as black but thinner. This has been documented https://ggplot2.tidyverse.org/news/index.html.

I have also experienced some strange behaviors with ggplot2 3.4.0 and rayshader. As you can see in the following graph, my 3D graph with ggplot2 3.4.0 became unrecognizable.

rayshader with ggplot 3.4.0

enter image description here

The only solution I have so far is to switch back to ggplot2 3.3.6.

library(remotes)
install_version("ggplot2", version = "3.3.6", repos = "http://cran.us.r-project.org")

You may want to try it, as least as a temporary solution.

rayshader with ggplot 3.3.6

enter image description here

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27
  • aaaaa that's what I mean, I wonder why the results are different.. Thanks sir, i'll try it. – Eriza Feb 08 '23 at 10:54
0

Are you absolutely positively certain you are using the current versions of packages? If yes then it could be some tricky environmental issue (I run my R on Linux).

When I run your code I get this - I changed the edges color from black to red to exaggerate the effect; otherwise it is your code, and it seems to behave itself.

library(sf)
library(ggplot2)
library(rayshader)

map <- st_read(system.file("shape/nc.shp", package="sf")) # to make reproducible

gg <- ggplot(map) +
  geom_sf(aes(fill = AREA),
          linewidth=0.7,
          colour='red', # changed from black to red to exaggerate...
          inherit.aes=FALSE) +
  viridis::scale_fill_viridis('Area',na.value = 'white')+
  theme(axis.line = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank(), 
        axis.text = element_blank())

plot_gg(gg,
        multicore = TRUE, 
        width = 6,
        height=2.7, 
        fov = 70,
        offset_edges = TRUE)

render_snapshot(filename = "nc.png")

enter image description here

Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44
  • Yes, i use the current versions of packages. map <- st_read(system.file("map/map.shp", package="sf")) this code is error. Error: `dsn` must point to a source, not an empty string. – Eriza Feb 08 '23 at 08:43
  • since I did not have your `map.shp` file I used the one that ships with the `{sf}` package - you need to copy/paste the full line `map <- st_read(system.file("shape/nc.shp", package="sf"))`, the shape/nc.shp part is important – Jindra Lacko Feb 08 '23 at 08:47
  • i want to apply that code to my map. should i save my folder in system.file? – Eriza Feb 08 '23 at 08:52
  • just ignore "my" assignment to the map object and use yours then; it does not matter as long as the object is a sf data frame named `map` and has a field named `AREA` – Jindra Lacko Feb 08 '23 at 08:59
  • hmm how to make sure i use the current version of package? because when i try with same code i have different results. – Eriza Feb 08 '23 at 09:20