- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力绘制所谓的spider network
或 desire line
它说明了事物(人、车辆等)在特定区域之间按方向的移动。
这是我正在使用的数据框:
df <- data.frame(O=c(1,2,4,4,4,6,6,6,7,7,7,9,9,9,9,10,10,10,11,12,12,12,32,86,108,128,128,157,157,157,157,157),
D=c(2,1,6,7,32,4,7,157,4,6,157,10,11,12,157,9,12,157,9,9,10,157,4,128,128,86,108,6,7,9,10,12),
trip=c(971,971,416,621,330,416,620,1134,621,620,625,675,675,378,439,675,724,472,675,378,724,563,330,610,405,610,405,1134,625,439,472,563),
lon.x=c(697746.6,696929.6,696748.8,696748.8,696748.8,694906.4,694906.4,694906.4,696769.4,696769.4,696769.4,698802.2,698802.2,698802.2,698802.2,698900.5,698900.5,698900.5,699686.7,696822.0,696822.0,696822.0,698250.7,702314.7,700907.1,702839.5,702839.5,694518.9,694518.9,694518.9,694518.9,694518.9),
lat.x=c(9312405,9311051,9308338,9308338,9308338,9307087,9307087,9307087,9305947,9305947,9305947,9304338,9304338,9304338,9304338,9302314,9302314,9302314,9306300,9303080,9303080,9303080,9309423,9320738,9321302,9322619,9322619,9301921,9301921,9301921,9301921,9301921),
lon.y=c(696929.6,697746.6,694906.4,696769.4,698250.7,696748.8,696769.4,694518.9,696748.8,694906.4,694518.9,698900.5,699686.7,696822.0,694518.9,698802.2,696822.0,694518.9,698802.2,698802.2,698900.5,694518.9,696748.8,702839.5,702839.5,702314.7,700907.1,694906.4,696769.4,698802.2,698900.5,696822.0),
lat.y=c(9311051,9312405,9307087,9305947,9309423,9308338,9305947,9301921,9308338,9307087,9301921,9302314,9306300,9303080,9301921,9304338,9303080,9301921,9304338,9304338,9302314,9301921,9308338,9322619,9322619,9320738,9321302,9307087,9305947,9304338,9302314,9303080))
df
包含以下字段:
O
: 旅行的起源
D
: 旅行目的地
trip
:
O
之间的行程次数和
D
lon.x
: 原点经度
lat.x
: 原点纬度
lon.y
: 目的地区域的经度
lat.y
: 目的地区域的纬度
geom_segment
通过这里的脚本绘制下图在
ggplot2
包裹:
library(ggplot2)
ggplot() +
geom_segment(data = df, aes(x = lon.x, y = lat.x, xend = lon.y, yend = lat.y, size = trip),
color = "blue", alpha = 0.5, show.legend = TRUE,
position = position_dodge2(width = 100)) +
scale_size_continuous(range = c(0, 5), breaks = c(300, 600, 900, 1200),
limits = c(100, 1200), name = "Person trips/day (over 100 trips)") +
theme(legend.key = element_rect(colour = "transparent", fill = alpha("black", 0))) +
guides(size = guide_legend(override.aes = list(alpha = 1.0))) +
geom_point(data = df, aes(x = lon.x, y = lat.x), pch = 16, size = 2.4)
O
的每一行至
D
来自
D
至
O
是重叠的。我更喜欢绘制基于中心线躲避的线段,以正确可视化总行程数并查看区域对之间的行程平衡。
width
中设置段之间的边距使用变量,但是如果我使用
trip
在其中,它返回错误。此外,不清楚我应该为适当的偏移设置多少值以使线段遵循中心线。
geom_curve
和 arrow
color=variable
和 position=dodge
spread/gather
df
获取新变量
direction
并删除相反方向的 OD 对,以便我认为我可以使用
color=direction
轻松躲避线段和
position=dodge
在
ggplot2
,但是效果不佳(段仍然重叠)。小例子如下所示。
O D trip direction lon.x lat.x lon.y lat.y
1 2 971 clock 697746.6 9312405 696929.6 9311051
2 1 300 anticlock 696929.6 9311051 697746.6 9312405
4 6 416 clock 696748.8 9308338 694906.4 9307087
4 7 621 anticlock 694906.4 9307087 696748.8 9308338
spider network
的实际使用情况。 .
最佳答案
您可以使用三角函数来计算偏移值,然后将其插入 ggplot()
称呼。以下是使用上述数据集的示例。我不太清楚你所说的顺时针是什么意思,所以我输入了一个简单的虚拟变量。
# make a dummy "clockwise" variable for now
df$clockwise = df$O > df$D
# angle from coordinates of stations
df$angle = atan((df$lat.y - df$lat.x)/(df$lon.y - df$lon.x))
# offsets from cos/sin of orthogonal angle
# scale the distance of the offsets by the trip size so wider bars offset more
# offset them one way if the trip is clockwise, the other way if not clockwise
df$xoffset = cos(df$angle - pi/2) * df$trip/5 * (2 * df$clockwise - 1)
df$yoffset = sin(df$angle - pi/2) * df$trip/5 * (2 * df$clockwise - 1)
ggplot() +
geom_segment(data = df, aes(x = lon.x + xoffset, y = lat.x + yoffset, xend = lon.y + xoffset, yend = lat.y + yoffset, size = trip, color = clockwise),
alpha = 0.5, show.legend = TRUE) +
scale_size_continuous(range = c(0, 5), breaks = c(300, 600, 900, 1200),
limits = c(100, 1200), name = "Person trips/day (over 100 trips)") +
theme(legend.key = element_rect(colour = "transparent", fill = alpha("black", 0))) +
guides(size = guide_legend(override.aes = list(alpha = 1.0))) +
geom_point(data = df, aes(x = lon.x, y = lat.x), pch = 16, size = 2.4) +
coord_fixed()
关于r - 如何在R中的geom_segment/ggplot2中绘制定向蜘蛛网络?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51300440/
我有下面的代码,它工作正常。问题是,我想添加“k”并绘制一条类似于“z”的直线,但“k”是不同数字的向量。 “k”中的每个元素都应绘制为创建的 3 个面上的一条线。如果 k 是一个奇异值,我会用不同的
在我更新到版本 2.2.0 之前,以下代码在 ggplot2 中有效。现在我得到了 Error: Aesthetics must be either length 1 or the same as t
我想使用 geom_segment 按因子组显示段簇,但 position 参数似乎没有任何作用。这是一个例子: mydata = data.frame(variable = factor(c("A"
我想弄清楚如何更改使用 geom_segment 的甘特图的边框颜色. 这是一些数据: df <- structure(list(start_int = c(0, 5, 0, 0, 9, 10, 12
我正在尝试在 ggplot 中构建网络图。两件事:1)我需要将节点放在特定的(x,y)值上。这不是问题。 2)网络图是定向的,但我需要能够显示从节点 B 到节点 A 与从节点 A 到节点 B 的差异。
使用 facet_grid 时,如何根据数据中的一个因素为 geom_segments 着色?我的方法失败了,因为颜色的分配是错误的。 这是一些数据: visual_data=data.frame(V
大家好,我需要帮助才能使用 ggplot2 获得一个不错的 geom_segment 图。 这是数据 structure(list(molecule = structure(c(1L, 1L, 2L,
ggplot(int_times,aes(x=-stim ,y=num,colour=gene)) + scale_y_continuous(breaks=int_times$num,l
我正在尝试使用 ggplot2 在直方图底部绘制一些线段,覆盖几条线段以指示分布的相关部分。我希望绿色段的厚度是灰色段的两倍,但绿色段似乎总是灰色段厚度的五倍。这是一些玩具数据和我的 ggplot2
下面的绘图代码使所有颜色的线段线会聚在离散 x 变量的中心。可以做些什么来使线段线对于每个 x 变量各自的颜色是垂直的? #~ Plot data ggplot(data = data0, aes(x
我正在制作一些室内和室外事件的相当复杂的情节,但我有点陷入困境。我想通过(Xmin、Ymin 和 Zmin)在 geom_segment 中添加对室内/室外花费的分钟数进行着色> 我的情节的步骤(见下
我正在尝试在 ggplot2 中制作一个带有箭头的绘图,看起来像这样,它是使用基本 R 图形制作的。 (颜色并不重要) 使用ggplot2: library(ggplot2) library(scal
下面的图有 2 个部分。 如何为线段添加图例? 理想情况下,最终结果有 2 个图例: 其中一个是当前的“点图例” 另一个图例是一个带有红色虚线标记为“段图例”的单个图例 这是代码 set.seed(1
我正在研究主成分分析 (PCA)。我发现ggfortify效果很好,但想做一些手动调整。 这里尝试绘制 PCA 结果如下: evec % ggplot(mapping = aes(x=PC1, y
我有两组纬度和经度变量,用于我的数据框中的大量行 (~100,000)。我正在尝试制作一个连接这两组坐标的图(即,从 latitude1,longitude1 到 latitude2,longitud
这是我的可重现示例: IND.factions <- rbind(c("Mughal Empire", "IND", "1526-1-1", "1857-1-1", "IND"),
这个问题在这里已经有了答案: geom_rect and alpha - does this work with hard coded values? (4 个回答) 去年关闭。 我在我的情节中尝试了
问题: 我想将 geom_hline 中的 geom_segment 添加到 3 个 stat_summary 点中的每一个,以确定每个物种的平均值。 当前代码: library(tidyverse)
我正在尝试通过 geom_segment 在绘图中放置一条引用线。 一个简化的例子: tryCatch({ spaghetti_smooth_ref = qplot(age,T_self, data=
我正在尝试获得类似于 Glen_b 对 this question 的回答的内容.基本上,我希望箱线图顶部有一个箭头,以指示使用 ggplot 时有更多超出比例的异常值。 我可以让情节的主要部分看起来
我是一名优秀的程序员,十分优秀!