当我通过美学提供值时,无法使geom_sf线宽正常运行 [英] Can't get geom_sf line width to function properly when I provide its value through an aesthetic
问题描述
我有一个shapefile, my_sf
.如果我写:
I have a shapefile, my_sf
. If I write:
ggplot() +
geom_sf(
data = my_sf,
size = 0.5 # this is the default value actually
)
然后就可以了.但是,如果我向 my_sf
添加 my_line_width
属性,并将该列中的每个值设置为0.5,然后编写:
then it works just fine. However, if I add a my_line_width
attribute to my_sf
, and set each value in that column to 0.5, and then write:
ggplot() +
geom_sf(
data = my_sf,
aes(size = my_line_width)
)
然后,我得到了一堆巨大的线条,它们重叠得令人难以置信.如果法线的宽度为0.5,则宽度可能为4.我不明白为什么这些线不会产生相同的结果(除了图例,我知道只有在您弹出时使用美学).有人知道发生了什么事吗?
then I get a bunch of massive lines that overlap hideously. If the normal lines are width 0.5, then these are perhaps width 4. I don't understand why those don't produce the same result (aside from the legend, which I know only pops up if you use an aesthetic). Does anyone know what's going on?
我也尝试过使用 lwd
而不是 size
.确实发生了同样的事情:设置静态值很好,但是当我尝试通过美学来提供值时,就会出错.
I have also tried using lwd
instead of size
. Exactly the same thing happens: it's fine when I just set a static value, but when I try to provide values through an aesthetic, it goes wrong.
推荐答案
我敢肯定,这与 aes
将数据映射到绘图中的想法有关.例如,请参见传递选项之间的差异在es()内以及在ggplot2内的外部或美学何时在aes()内部或外部出现?.使用 size
或 aes
内部的任何内容,以 ggplot
可以理解的方式(在下面进行更多介绍)或在 aes
并带有恒定值,以实际获取 ggplot
来使用该值.您找到的行为对于ggplot通常是正确的,而不特定于 geom_sf
.
I'm pretty sure this has to do with the idea behind aes
mapping the data to the plot. For example, see Difference between passing options in aes() and outside of it in ggplot2 or When does the aesthetic go inside or outside aes()?. Use size
or whatever inside aes
to map data to the aesthetic in whatever way ggplot
will understand it (more on that below) or outside aes
with a constant value to actually get ggplot
to use that value. The behavior you are finding is true with ggplot generally and not specific to geom_sf
.
您可以看到,即使使用 geom_point
You can see that this is true for size even with geom_point
mtcars %>% ggplot(aes(mpg, wt, size=cyl))+geom_point()
k<-4
mtcars %>% ggplot(aes(mpg, wt, size=k))+geom_point()
k<-6
mtcars %>% ggplot(aes(mpg, wt, size=k))+geom_point()
k<-10
mtcars %>% ggplot(aes(mpg, wt, size=k))+geom_point()
此外,默认的大小处理方法与ggplot2,IMO相当混乱:点面积是否会随着scale_size_continuous的大小而线性增加?/a>.如果仅在aes内提供1值,则线宽可能始终是基线默认大小.正如您的评论者所指出的,您可以使用 scale
调整此行为,并且可以在某种程度上使用身份",但是提供 range
https://stackoverflow.com/a/11570926/8400969 .
Also, the default treatment of size is pretty confusing with ggplot2, IMO: Does point area not increase linearly with size with scale_size_continuous?. When only provided 1 value inside aes, the line thickness will probably always be the baseline default size. As your commenter noted, you can adjust this behavior with scale
, and you can use "identity" to some extent but it may be helpful to provide a range
https://stackoverflow.com/a/11570926/8400969.
这篇关于当我通过美学提供值时,无法使geom_sf线宽正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!