为什么在我使用R中的Rayshader的plot_gg()函数时,我的图的背景是黑色的?

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

Why is the background of my plot black when I use plot_gg() from Rayshader in R

问题

我试图使用Rayshader包在3D中表示某个区域的一些点数据。它正确绘制了一切,只是背景完全是黑色的。如何修复这个问题?以下是我的代码:

base_map <- ggplot(data = df.shp, mapping = aes(x = long, y = lat, group = group)) + 
  geom_polygon(color = "#8a8a8a", fill = "#8a8a8a") +
  coord_quickmap() +
  theme_void()

growing_stock = base_map +
  geom_point(data = temp1, aes(x=longitude, y=latitude, color=volume_per_ha, group=time),size=2) + 
  scale_colour_gradient(name = 'Average growing stock [m3/ha]', 
                        limits=range(temp1$volume_per_ha), 
                        low="#FCB9B2", high="#B23A48")  
growing_stock

plot_gg(growing_stock, width=5, height=5, multicore = TRUE, scale = 300)

这是绘图的样子。

英文:

I am trying to represent some point data for a region in 3D using the Rayshader package. It plots everything correctly, except that the background is totally black. How to fix this? Below is my code:

base_map &lt;- ggplot(data = df.shp, mapping = aes(x = long, y = lat, group = group)) + 
  geom_polygon(color = &quot;#8a8a8a&quot;, fill = &quot;#8a8a8a&quot;) +
  coord_quickmap() +
  theme_void()

growing_stock = base_map +
  geom_point(data = temp1, aes(x=longitude, y=latitude, color=volume_per_ha, group=time),size=2) + 
  scale_colour_gradient(name = &#39;Average growing stock [m3/ha]&#39;, 
                        limits=range(temp1$volume_per_ha), 
                        low=&quot;#FCB9B2&quot;, high=&quot;#B23A48&quot;)  
growing_stock

plot_gg(growing_stock, width=5, height=5, multicore = TRUE, scale = 300)

Here is how the plot looks like

为什么在我使用R中的Rayshader的plot_gg()函数时,我的图的背景是黑色的?

答案1

得分: 1

你有几种选项来解决这个问题。删除 theme_void,或者用其他主题之一替换它可以改变背景颜色,但会向图表中添加一些你可能不想要的 ggplot2 组件。解决问题的一种简单方法就是使用 theme() 明确更改背景颜色。

base_map <- ggplot(data = df.shp, mapping = aes(x = long, y = lat, group = group)) +
  geom_polygon(color = "#8a8a8a", fill = "#8a8a8a") +
  coord_quickmap() +
  theme_void() +
  theme(
    plot.background = element_rect(
      fill = "white",
      colour = "white"
    )
  )

在这个示例中,我将背景颜色更改为 "white"。你可以选择更改为其他颜色。

英文:

You have several options to fix this. Removing theme_void, or replacing it with one of other themes can change the background color, but will add some other ggplot2 components to the graph that you may not want. One easy way to fix the problem is just to explicitly change the background color using theme().

base_map &lt;- ggplot(data = df.shp, mapping = aes(x = long, y = lat, group = group)) +
  geom_polygon(color = &quot;#8a8a8a&quot;, fill = &quot;#8a8a8a&quot;) +
  coord_quickmap() +
  theme_void() +
  theme(
    plot.background = element_rect(
      fill = &quot;white&quot;,
      colour = &quot;white&quot;
    )
  )

I changed background color to "white" in this example. You may want to change to another color.

huangapple
  • 本文由 发表于 2023年2月14日 19:50:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447425.html
匿名

发表评论

匿名网友

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

确定