为什么 scale_edge_width_identity(guide = "legend") 不起作用?
Why doesn't scale_edge_width_identity(guide = "legend") work?
我正在使用 ggraph 绘制两个分别包含不同大小和宽度的节点和边的网络。
我想使用 scale_size_identity()
和 scale_edge_width_identity()
来保持节点和边的绝对大小。
我想显示图例,我对节点使用 scale_size_identity(legend = "guide")
,这按预期工作,但对于边缘 scale_edge_width_identity(legend = "guide")
,我收到以下错误:
Error: Continuous value supplied to discrete scale
如果我不向 scale_edge_width_identity()
提供 legend = "guide"
,一切正常。我缩放的变量是数字。如果我使用 scale_edge_alpha_identity(legend = "guide")
,也会出现此问题 为什么会这样,还有什么方法可以显示图例吗?感谢任何建议。
更新:
它适用于下面@teunbrand 的建议。但是,我注意到用于缩放节点大小的 scale_size_continuous()
不适用于这种方法。使用 scale_radius()
代替:
# building on example by @teunbrand
library(ggraph)
#> Loading required package: ggplot2
require(tidygraph)
#> Loading required package: tidygraph
#>
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#>
#> filter
library(patchwork)
gr <- create_notable("bull")
layout <- create_layout(gr, layout = "igraph", algorithm = "kk")
edge_guide <- guide_legend()
# Edges are plotted using the same scale but nodes are not when scale_size_continuous() is used.
# Notice the differently sized nodes between the legend guides:
ggraph(layout) +
geom_node_point(aes(size = seq(1, 5, length.out = 5))) +
geom_edge_link(aes(edge_width = seq(1, 5, length.out = 5))) +
scale_size_continuous(range = c(1, 5), breaks = c(1, 3, 5)) +
scale_edge_width_continuous(range = c(1, 5), breaks = c(1, 2, 3)) |
ggraph(layout) +
geom_node_point(aes(size = seq(1, 10, length.out = 5))) +
geom_edge_link(aes(edge_width = seq(1, 10, length.out = 5))) +
scale_size_continuous(range = c(1, 10), breaks = c(1, 3, 5)) +
scale_edge_width_continuous(range = c(1, 10), breaks = c(1, 2, 3))
plotOutput1
# However, using scale_radius() instead does the job:
ggraph(layout) +
geom_node_point(aes(size = seq(1, 5, length.out = 5))) +
geom_edge_link(aes(edge_width = seq(1, 5, length.out = 5))) +
scale_radius(range = c(1, 5), breaks = c(1, 3, 5)) +
scale_edge_width_continuous(range = c(1, 5), breaks = c(1, 2, 3)) |
ggraph(layout) +
geom_node_point(aes(size = seq(1, 10, length.out = 5))) +
geom_edge_link(aes(edge_width = seq(1, 10, length.out = 5))) +
scale_radius(range = c(1, 10), breaks = c(1, 3, 5)) +
scale_edge_width_continuous(range = c(1, 10), breaks = c(1, 2, 3))
Created on 2020-12-04 by the reprex package (v0.3.0)
plotOutput2
我感到困惑,这可能是一个错误,但这里有解决方法。解决方法基本上是使用 _continuous()
版本的比例尺通过将范围设置为数据的确切范围来尽可能接近地模仿 _identity()
版本。
library(ggraph)
#> Loading required package: ggplot2
require(tidygraph)
#> Loading required package: tidygraph
#>
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#>
#> filter
gr <- create_notable('bull')
layout <- create_layout(gr, layout = 'igraph', algorithm = 'kk')
edge_guide <- guide_legend()
ggraph(layout) +
geom_node_point() +
geom_edge_link(aes(edge_width = 1:5)) +
scale_edge_width_continuous(range = c(1, 5))
ggraph(layout) +
geom_node_point() +
geom_edge_link(aes(edge_alpha = seq(0.1, 1, length.out = 5))) +
scale_edge_alpha_continuous(range = c(0.1, 1))
由 reprex package (v0.3.0)
于 2020-12-03 创建
我正在使用 ggraph 绘制两个分别包含不同大小和宽度的节点和边的网络。
我想使用 scale_size_identity()
和 scale_edge_width_identity()
来保持节点和边的绝对大小。
我想显示图例,我对节点使用 scale_size_identity(legend = "guide")
,这按预期工作,但对于边缘 scale_edge_width_identity(legend = "guide")
,我收到以下错误:
Error: Continuous value supplied to discrete scale
如果我不向 scale_edge_width_identity()
提供 legend = "guide"
,一切正常。我缩放的变量是数字。如果我使用 scale_edge_alpha_identity(legend = "guide")
,也会出现此问题 为什么会这样,还有什么方法可以显示图例吗?感谢任何建议。
更新:
它适用于下面@teunbrand 的建议。但是,我注意到用于缩放节点大小的 scale_size_continuous()
不适用于这种方法。使用 scale_radius()
代替:
# building on example by @teunbrand
library(ggraph)
#> Loading required package: ggplot2
require(tidygraph)
#> Loading required package: tidygraph
#>
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#>
#> filter
library(patchwork)
gr <- create_notable("bull")
layout <- create_layout(gr, layout = "igraph", algorithm = "kk")
edge_guide <- guide_legend()
# Edges are plotted using the same scale but nodes are not when scale_size_continuous() is used.
# Notice the differently sized nodes between the legend guides:
ggraph(layout) +
geom_node_point(aes(size = seq(1, 5, length.out = 5))) +
geom_edge_link(aes(edge_width = seq(1, 5, length.out = 5))) +
scale_size_continuous(range = c(1, 5), breaks = c(1, 3, 5)) +
scale_edge_width_continuous(range = c(1, 5), breaks = c(1, 2, 3)) |
ggraph(layout) +
geom_node_point(aes(size = seq(1, 10, length.out = 5))) +
geom_edge_link(aes(edge_width = seq(1, 10, length.out = 5))) +
scale_size_continuous(range = c(1, 10), breaks = c(1, 3, 5)) +
scale_edge_width_continuous(range = c(1, 10), breaks = c(1, 2, 3))
plotOutput1
# However, using scale_radius() instead does the job:
ggraph(layout) +
geom_node_point(aes(size = seq(1, 5, length.out = 5))) +
geom_edge_link(aes(edge_width = seq(1, 5, length.out = 5))) +
scale_radius(range = c(1, 5), breaks = c(1, 3, 5)) +
scale_edge_width_continuous(range = c(1, 5), breaks = c(1, 2, 3)) |
ggraph(layout) +
geom_node_point(aes(size = seq(1, 10, length.out = 5))) +
geom_edge_link(aes(edge_width = seq(1, 10, length.out = 5))) +
scale_radius(range = c(1, 10), breaks = c(1, 3, 5)) +
scale_edge_width_continuous(range = c(1, 10), breaks = c(1, 2, 3))
Created on 2020-12-04 by the reprex package (v0.3.0)
plotOutput2
我感到困惑,这可能是一个错误,但这里有解决方法。解决方法基本上是使用 _continuous()
版本的比例尺通过将范围设置为数据的确切范围来尽可能接近地模仿 _identity()
版本。
library(ggraph)
#> Loading required package: ggplot2
require(tidygraph)
#> Loading required package: tidygraph
#>
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#>
#> filter
gr <- create_notable('bull')
layout <- create_layout(gr, layout = 'igraph', algorithm = 'kk')
edge_guide <- guide_legend()
ggraph(layout) +
geom_node_point() +
geom_edge_link(aes(edge_width = 1:5)) +
scale_edge_width_continuous(range = c(1, 5))
ggraph(layout) +
geom_node_point() +
geom_edge_link(aes(edge_alpha = seq(0.1, 1, length.out = 5))) +
scale_edge_alpha_continuous(range = c(0.1, 1))
由 reprex package (v0.3.0)
于 2020-12-03 创建