ggplotly使多边形的边界变为透明。有解决方法吗?

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

ggplotly makes the border of a polygon transparent. Work around?

问题

我正在使用ggplotly制作散点图,以便用户可以悬停在观察结果上并查看标签等信息。我在上面绘制了一个多边形以突出显示特定区域。我将它放在ggplot的开头,并用白色填充多边形,然后在ggplot语句的末尾再次绘制它,以使边界位于点的上方,并使用透明填充。我在ggplot中完成所有这些操作,然后转换为ggplotly。ggplotly使多边形的边界变为透明,我失去了应该位于底部的白色内部。有没有解决方法?为什么会这样?

这是代码:

rm(list = ls())
vertices <- data.frame(x = c(-1, 1, 1, -1, -1), y = c(-1, -1, 1, 1, -1))
df <- data.frame(x = rnorm(1000), y = rnorm(1000), ticker = paste0(1:1000), warren = (runif(1000) < .3))

plot1 <- ggplot(df, aes(x = x, y = y)) +
  theme_minimal(base_size = 14) +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.background = element_rect(fill = "grey95"), panel.grid = element_line(color = "grey85")) +
  geom_polygon(data = vertices, aes(x, y), fill = "white", color = "lightblue") +
  suppressWarnings(geom_point(size = 2, aes(col = warren, shape = warren, text = ticker))) +
  scale_color_manual(values = c(rgb(1, 0, 0, .2), rgb(0, 0, 1))) +
  geom_polygon(data = vertices, aes(x, y), fill = rgb(0, 0, 0, 0), color = "lightblue", linewidth = 3)

plot1

g <- ggplotly(plot1)

g

这是ggplot图:
ggplotly使多边形的边界变为透明。有解决方法吗?

这是ggplotly图:
ggplotly使多边形的边界变为透明。有解决方法吗?

英文:

I am making a scatter plot using ggplotly so that one can hover over the observation and see the label and so forth. I am plotting a polygon on top of it to highlight a specific area. I put it at the start of the ggplot to and fill the polygon with white and I do it again with at the end of the ggplot statements so that the boundary sits on top of the points and use a transparent fill. I do all this in ggplot and then convert to ggplotly. ggplotly makes the boundary of the polygon transparent and I lose the white interior that is supposed to be on the bottom. Is there a work around? Why is it doing this?

Here is the code:

rm(list = ls())
vertices &lt;- data.frame(x = c(-1, 1, 1, -1, -1), y = c(-1, -1, 1, 1, -1))
df &lt;- data.frame(x = rnorm(1000), y = rnorm(1000), ticker = paste0(1:1000), warren = (runif(1000) &lt; .3))

plot1 &lt;- ggplot(df, aes(x = x, y = y)) +
  theme_minimal(base_size = 14) +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(panel.background = element_rect(fill = &quot;grey95&quot;), panel.grid = element_line(color = &quot;grey85&quot;)) +
  geom_polygon(data = vertices, aes(x, y), fill = &quot;white&quot;, color = &quot;lightblue&quot;) +
  suppressWarnings(geom_point(size = 2, aes(col = warren, shape = warren, text = ticker))) +
  scale_color_manual(values = c(rgb(1, 0, 0, .2), rgb(0, 0, 1))) +
  geom_polygon(data = vertices, aes(x, y), fill = rgb(0, 0, 0, 0), color = &quot;lightblue&quot;, linewidth = 3)

plot1

g &lt;- ggplotly(plot1)

g

Here is the ggplot:
ggplotly使多边形的边界变为透明。有解决方法吗?

Here is the ggplotly:
ggplotly使多边形的边界变为透明。有解决方法吗?

答案1

得分: 1

代码部分不需要翻译。以下是翻译的内容:

看起来绘图机制之间的转换删除了其中一个图层(多边形图层)。ggplot 中的大小与 Plotly 中不同,因此您的标记变得更大了。

首先,我们将添加缺失的图层到您的图中,然后调整大小。最后,因为我只是要复制多边形图层,我们需要在底部图层中添加白色填充。

如果您有任何问题,请告诉我。

英文:

It looks like the conversion between plotting mechanisms dropped one of your layers (a polygon layer). The sizing in ggplot is different than in Plotly, so your markers got a lot bigger, as well.

First, we're going to add that missing layer back to your plot, then adjust the sizes. Lastly, because I'm just going to duplicate the polygon layer, we'll need to add the white fill to the bottom layer.

Let me know if you have any questions.

g$x$data &lt;- append(g$x$data, g$x$data[1]) # duplicate the first trace
g                                         # inspect what you expect

lapply(1:length(g$x$data),                # update traces based on type
       function(j) {
         if(g$x$data[[j]]$mode == &quot;markers&quot;) {  
           g$x$data[[j]]$marker$size &lt;&lt;- 5      # reduce size
         } else {
           g$x$data[[j]]$line$width &lt;&lt;- 8       # reduce width
         }
       })
g$x$data[[1]]$fillcolor &lt;- &quot;white&quot;        # change the bottom trace only
g                                         # inspect what you expect

ggplotly使多边形的边界变为透明。有解决方法吗?

huangapple
  • 本文由 发表于 2023年6月8日 23:20:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433387.html
匿名

发表评论

匿名网友

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

确定