可以用R画图表吗?
我想知道R中是否有任何软件包可以使用x、y坐标和形状大小来绘制如下内容: 我有车辆前部中心的坐标及其尺寸(长度和宽度) 编辑 这是原始数据集的外观:可以用R画图表吗?,r,ggplot2,diagram,R,Ggplot2,Diagram,我想知道R中是否有任何软件包可以使用x、y坐标和形状大小来绘制如下内容: 我有车辆前部中心的坐标及其尺寸(长度和宽度) 编辑 这是原始数据集的外观: > head(df) Vehicle.ID Frame.ID Global.X Global.Y Vehicle.Length Vehicle.width Lane Preceding.Vehicle.ID Following.Vehicle.ID Spacing Headway 1 2 43 64512
> head(df)
Vehicle.ID Frame.ID Global.X Global.Y Vehicle.Length Vehicle.width Lane Preceding.Vehicle.ID Following.Vehicle.ID Spacing Headway
1 2 43 6451214 1873261 14.5 4.9 2 0 13 0 0
2 2 44 6451217 1873258 14.5 4.9 2 0 13 0 0
3 2 45 6451220 1873256 14.5 4.9 2 0 13 0 0
4 2 46 6451223 1873253 14.5 4.9 2 0 13 0 0
5 2 47 6451225 1873250 14.5 4.9 2 0 13 0 0
6 2 48 6451228 1873247 14.5 4.9 2 0 13 0 0
对于任何给定的帧,我希望可视化间隙,例如,对于第500帧:
ff <- subset(df, Frame.ID==500)
qplot(x=Global.X, y=Global.Y, data=ff)
ff所以,我不主张你依靠ggplot
来做这件事,因为其他一些建议的解决方案很可能更好,但这个问题让我感兴趣,因为我一直想挖掘ggplot
的精髓。这就是我想出来的:
ggplot(df, aes(x=x, y=y, length=length, width=width, fill=label)) +
geom_hline(yintercept=seq(5, 35, by=10), color="white", size=2, linetype=2) +
geom_car() +
coord_equal() +
theme(panel.background = element_rect(fill="#555555"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
您还可以添加带有geom_段的箭头
,或带有geom_文本
的显式标签,但我们将此作为练习留给读者
现在,为了实现这一点,我们必须创建geom\u car
,但是如果您不需要详细的图片,您可以使用geom\u rect
。这是geom_车
(注意:现在也作为的一部分提供):
# Generate a car 'grob' using a baseline PNG
car.raster <- png::readPNG("~/Downloads/car2.png")
# The `grid` grob actually responsible for rendering our car,
# combines our transparent car elements with a background rectangle
# for color/fill.
carGrob <- function(x, y, length, width, gp) {
grid::grobTree(
grid::rectGrob(
x, y, hjust=.5, height=width, width=length,
gp = gp
),
grid::rasterGrob(
car.raster, x=x, y=y, hjust=.5, height=width, width=length
) ) }
# The `ggproto` object that maps our data to the `grid` grobs
GeomCar <- ggplot2::ggproto("GeomCar", ggplot2::Geom,
# Generate grobs from the data, we have to reconvert length/width so
# that the transformations persist
draw_panel=function(self, data, panel_params, coords) {
with(
coords$transform(data, panel_params),
carGrob(
x, y, length=xmax-xmin, width=ymax-ymin,
gp=grid::gpar(
col = colour, fill = alpha(fill, alpha),
lwd = size * .pt, lty = linetype, lineend = "butt"
) ) ) },
# Convert data to coordinates that will get transformed (length/width don't
# normally).
setup_data=function(self, data, params) {
transform(data,
xmin = x - length / 2, xmax = x + length / 2,
ymin = y - width / 2, ymax = y + width / 2
) },
# Required and default aesthetics
required_aes=c("x", "y", "length", "width"),
default_aes = aes(
colour = NA, fill = "grey35", size = 0.5, linetype = 1, alpha = NA
),
# Use the car grob in the legend
draw_key = function(data, params, size) {
with(
data,
carGrob(
0.5, 0.5, length=.75, width=.5,
gp = grid::gpar(
col = colour, fill = alpha(fill, alpha),
lwd = size * .pt, lty = linetype, lineend = "butt"
) ) ) }
)
# External interface
geom_car <- function(
mapping=NULL, data=NULL, ..., inherit.aes=TRUE, show.legend=NA
) {
layer(
data=data, mapping=mapping, geom=GeomCar, position="identity",
stat="identity", show.legend = show.legend, inherit.aes = inherit.aes,
params=list(...)
)
}
#使用基线PNG生成汽车“grob”
我相信你可以,但我强烈建议你使用另一种工具来完成这项工作。如果你想编程,我会选择tikZ,如果你只需要少量图表,我会选择WYSIWYG绘图程序。我有数千辆车,每辆车的运动(坐标变化)都记录了好几个时间段。我希望能够在任何时刻(即任何给定帧)绘制这种类型的图表。从顶部看,像车辆一样的视图并不重要,简单的矩形(使用长度和宽度绘制)很好。虽然这项任务可能会让人觉得有趣(这是我能想象的任何人都会投票的唯一方式,更不用说6个人了),但在目前的形式下,这不是一个合适的问题。在这里,询问软件包/库建议通常是离题的,除此之外,您还没有提供任何具体的示例数据供人们使用。如果你有一个具体的尝试,有一些你想要反馈的实际数据,那就好了。否则,这个应该关闭。听起来rect()
函数可能会做你想做的一切……我同意@shujaa的观点,并推荐一个更好的工具来做这类事情。@BrodieG Hadley问他是否可以在twitter@Tylerinker上的下一本ggplot2书中分享这个,谢谢你的提醒;可能过了一段时间我才注意到。这很好,但它需要更多的青蛙。:)这几乎可以肯定是Ggplot2/prooto变更问题。有很多变化,特别是如何进行扩展。恐怕我没有时间专门讨论这个问题,但也许你可以以“我如何用新版本的Ggplot2复制这个答案”的形式再次提出这个问题,有人会愿意尝试一下。@umairdurrani现在应该使用新版本的Ggplot2了。
df <- read.table(h=T, t="vehicle x y length width label
1 150 10 14 5 other
2 180 8 12 5 other
3 220 10 18 5 other
4 145 20 15 5 target
5 250 18 14 5 other
6 160 30 13 5 autonomous
7 200 33 15 5 other
8 240 31 22 5 other
")