使用R中的Rayshader为3D地图的高度添加颜色。

huangapple go评论58阅读模式
英文:

Add color on height 3D map with Rayshader in R

问题

我想使用rayshader制作3D地图。我看过这个网站https://www.tylermw.com/3d-ggplots-with-rayshader/,并尝试使用我的数据运行代码。在这个网站上,高度图是黑色的,但在我的输出中不是。有没有办法获得像那样的结果?谢谢。。

我使用了这段代码。

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('面积', 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)

这是我的结果快照。
使用R中的Rayshader为3D地图的高度添加颜色。

这是我的期望。
使用R中的Rayshader为3D地图的高度添加颜色。

英文:

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 &lt;- sf::st_read(&#39;map/map.shp&#39;, quiet = TRUE)
gg = ggplot(map) +
   geom_sf(aes(fill =AREA),linewidth=0.7,colour=&#39;black&#39;,inherit.aes=FALSE) +
    scale_fill_viridis(&#39;Area&#39;,na.value = &#39;white&#39;)+
   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.
使用R中的Rayshader为3D地图的高度添加颜色。

This is my expect.
使用R中的Rayshader为3D地图的高度添加颜色。

答案1

得分: 3

显然,你正在使用新版本的 ggplot2 3.4.0。我认为你的问题不是关于添加颜色,而更可能与新版本的 ggplot2 有关,特别是新的 linewidth()。你的颜色仍然是正确的,但更细。这已经在文档中记录了 https://ggplot2.tidyverse.org/news/index.html

我也遇到了一些关于 ggplot2 3.4.0 和 rayshader 的奇怪行为。如下图所示,我的带有 ggplot2 3.4.0 的 3D 图变得难以辨认。

使用 ggplot 3.4.0 的 rayshader

使用R中的Rayshader为3D地图的高度添加颜色。

到目前为止,我唯一的解决方案是切换回 ggplot2 3.3.6。

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

你可能想尝试一下,至少作为一个临时解决方案。

使用 ggplot 3.3.6 的 rayshader

使用R中的Rayshader为3D地图的高度添加颜色。

英文:

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

使用R中的Rayshader为3D地图的高度添加颜色。

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

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

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

rayshader with ggplot 3.3.6

使用R中的Rayshader为3D地图的高度添加颜色。

答案2

得分: 0

你确定你正在使用当前版本的软件包吗?如果是的话,可能是一些棘手的环境问题(我在Linux上运行R)。

当我运行你的代码时,我得到了这个结果 - 我将边框颜色从黑色改为红色以夸张效果;除此之外,这是你的代码,它似乎表现正常。

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")
英文:

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 &lt;- st_read(system.file(&quot;shape/nc.shp&quot;, package=&quot;sf&quot;)) # to make reproducible

gg &lt;- ggplot(map) +
  geom_sf(aes(fill = AREA),
          linewidth=0.7,
          colour=&#39;red&#39;, # changed from black to red to exaggerate...
          inherit.aes=FALSE) +
  viridis::scale_fill_viridis(&#39;Area&#39;,na.value = &#39;white&#39;)+
  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 = &quot;nc.png&quot;)

使用R中的Rayshader为3D地图的高度添加颜色。

huangapple
  • 本文由 发表于 2023年2月8日 10:02:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定