niloy-biswas / Population-Density-Map

Explore population density patterns in this repository featuring a captivating map generated with the Rayshader package in R.
Apache License 2.0
18 stars 5 forks source link

Error: object 'subset_colors' not found - at Step 8 of your example code #1

Open ericmelse opened 5 months ago

ericmelse commented 5 months ago

Dear Niley,

With great interest I am reading your Medium post How to Create a 3D Population Density Map in R and exercise your code example as to create the Bangladesh tilted map.

My issue now is that at Step 8, when I run your code:

# Step 8: Define Color Palette
# Select a color palette from the MetBrewer library and customize it for your map.
color <- MetBrewer::met.brewer(name="Benedictus", direction = -1)
tx <- grDevices::colorRampPalette(subset_colors, bias = 4.5)(256)
swatchplot(tx)
swatchplot(subset_colors)

I get the following error message immediately after the line tx ...: Error: object 'subset_colors' not found I admit that I am not a savvy R user and only can guess that something has first to be declared as to get the object subset_colors in memory so that the tx object can be created.

Time permitting, I appreciate your helping hand as to solve this Error.

niloy-biswas commented 5 months ago

I believe just using color instead of subset_colors would solve your problem.

tx <- grDevices::colorRampPalette(color, bias = 4.5)(256) swatchplot(tx) swatchplot(color)

ericmelse commented 5 months ago

Dear Niley,

Indeed, changing subset_colors into colors solved the issue and I am now able to run the example code. Another minor suggestion, I have edited the code of Step 1 and Step 2 to accommodate the installation of all required packages for those users who start with a basic R configuration (as to be able to run your code example), like:

# Step 1: Install Required Packages
install.packages("tidyverse", dependencies=TRUE)
install.packages("ggplot2", dependencies=TRUE)
install.packages("sf", dependencies=TRUE)
install.packages("tmap", dependencies=TRUE)
install.packages("mapview", dependencies=TRUE)
install.packages("stars", dependencies=TRUE)
install.packages("rayshader", dependencies=TRUE)
install.packages("rayrender", dependencies=TRUE)
install.packages("MetBrewer", dependencies=TRUE)
install.packages("colorspace", dependencies=TRUE)
install.packages("extrafont", dependencies=TRUE)
install.packages("magick", dependencies=TRUE)

# Step 2: Load Packages and Set Options
# Load the required libraries and set the RGL options:
options(rgl.useNULL = FALSE)

require(tidyverse)
require(ggplot2)
require(sf)
require(tmap)
require(mapview)
require(stars)
require(rayshader)
require(rayrender)
require(MetBrewer)
require(colorspace)
require(extrafont)
require(magick)

I proceed to Step 9 and render the 3D representation of the population density using Rayshader. After some rotation, the result is like: Code result rgl_rglwidget() Next, I proceed to Step 10 and save to the file Dhaka_Benedictus_4.png without changing anything in your code. But, the file that is saved is an 1x1 pixel image: Code_result_Dhaka_Benedictus_4 Do you have an explanation why the image is not saved correctly and a possible solution to get it to work as it should?

Further news on this issue Unaware that I was that R was still running, the following is reported by R:

Warning messages:
1: In render_highquality(filename = outfile, interactive = F, lightdirection = 55,  :
  When `sample_method = "sobol_blue"`, `samples` must be less than or equal to 256. Setting `sample_method` to `"sobol"`.
2: In UseMethod("depth") :
  no applicable method for 'depth' applied to an object of class "NULL"
3: In UseMethod("depth") :
  no applicable method for 'depth' applied to an object of class "NULL"

So, I try the export again with samples set to 256. And, this time the code does complete running without any error prompt. However, the result image (png file) is not what we expect it to be: Dhaka_Benedictus_4 Do you have a suggestion what I now should do with the code?

  render_highquality(
    filename = outfile,
    interactive = F,
    lightdirection = 55, #Degree
    lightaltitude = c(30, 80),
    lightcolor = c("white", "white"),  # Set both lights to white
    lightintensity = c(600, 100),
    width = 1400,
    height = 1580,
    samples = 256
  )
niloy-biswas commented 5 months ago

Did you wait until the rendering was completed?

The rendering time actually varies from device to device. In my case, my MacBook M1 takes around 15-20 minutes to complete a single rendering and save the actual picture. Before that, the file is saved as a 1x1 pixel image.

ericmelse commented 5 months ago

Yes, Niloy, I learned as much. Your code, after using your suggestion, does complete and after the saving of the above result image (png file), in the RStudio console your timers are reported:

2024-06-03 10:16:40.77362 
8.30475066900253 

So, my assumption now is that 'something' is either wrong or missing that leads to the rather black 3D image of my previous post instead of the color image in the Rayshader window. Any suggestion you can think of is appreciated.

niloy-biswas commented 5 months ago

Sorry Man, Couldn't help without being able to reproduce the error actually.

ericmelse commented 5 months ago

Dear Niloy, thank you for taking care. Yes, of course, when you cannot replicate my problem then it is rather difficult to solve it. Next, I ran your Github example file SriLanka-Population-Density.R and like above, the plot is just fine displayed in the 3D plot viewer, like: Code result Sri_Lanka_BuPu2 But, sadly, the image is saved just as black, like: Sri_Lanka_BuPu2 Something disagrees between the code and deep down in the package that is used as no errors are reported. At this point I can only let it go.

jsonkao commented 3 months ago

Hi @ericmelse , I was getting a black iamge on all the scripts as well. After rendering the XQuartz plot with plot_3d, I could run render_highquality("filename.png") successfully. Seemingly only after running that first render_highquality without any arguments, I slowly added arguments about light and width, and subsequent calls have worked. Will edit this answer if I find a better reason/cause/solution.

It possibly has to do with removing the interactive = F option.