基于ggplot2的密封示例,我正在尝试更改箭头,因此它们的整体大小更好地反映了数据变量.我可以指定长度和厚度,但不知道如何更改箭头的大小.非常感谢您的任何建议.
Building on ggplot2's seals example, I'm trying to change the thickness of arrows so their overall size better reflects the data variable. I can specify length and thickness, but don't know how to change the size of the arrow-head. Very grateful for any suggestions.
require(ggplot2) require(grid) d = seals[sample(1:nrow(seals), 100),] d$size = sqrt(sqrt(d$delta_long^2 + d$delta_lat^2)) ggplot(d, aes(x = long, y = lat, size = size)) + geom_segment(aes(xend = long + delta_long, yend = lat + delta_lat), arrow = arrow(length = unit(0.1,"cm")))
修改
解决方案代码:
ggplot(d, aes(x = long, y = lat, size = size)) + geom_segment(aes(xend = long + delta_long, yend = lat + delta_lat), arrow = arrow(length = unit(d$size/3, "cm"), type='closed')) + scale_size(range = c(0, 2))推荐答案
我不能说这是对您问题的完整解决方案,但至少可以作为一个开始.
I can't say this is a complete solution to your problem, but at least it can be a start.
ggplot(d, aes(x = long, y = lat, size = size)) + geom_segment(aes(xend = long + delta_long, yend = lat + delta_lat), arrow = arrow(length = unit(0.7, "cm"))) + scale_size(range = c(1, 2))我的更改很小:较大的箭头和更大的比例尺.如果您希望避免过度绘图,则尺寸比例上限是最重要的.
My changes are minimal: bigger arrow heads and size scale. The upper limit on size scale is the most important if you aim to avoid overplotting.
从现在开始,最好只留下箭头,因为当尺寸较小时,线条是不可见的.这是一个肮脏的技巧:
From now on, it's probably a good idea to leave only the arrow heads, since the lines are not visible when the size is small. Here's a dirty hack for that:
ggplot(d, aes(x = long, y = lat, size = size)) + geom_segment(aes(xend = long + delta_long/100, yend = lat + delta_lat/100), arrow = arrow(length = unit(0.7,"cm"))) + scale_size(range = c(1, 2))
当然,在大小值之间保持适当的视觉联系至关重要!否则,您的情节可能会产生误导.但这取决于数据,因此我无法给您进一步的建议.抱歉,这很明显.
Of course, it is crucial to keep an appropriate visual relation between big and small values! Otherwise, your plot may become misleading. But that depends on the data, so I cannot give you further advice. Sorry if that's an obvious point.
UPD:事实证明,unit()函数是矢量化的,因此可以解决!
UPD: turns out, unit() function is vectorized, so it comes to the rescue!
ggplot(d, aes(x = long, y = lat, size = size)) + geom_segment(aes(xend = long + delta_long/100, yend = lat + delta_lat/100), arrow = arrow(length = unit(d$size * 5,"cm"))) + scale_size(range = c(1, 2))
更多推荐
ggplot中按比例大小的箭头
发布评论