是否可以在R中绘制图表? [英] Is it possible to draw diagrams in R?

查看:58
本文介绍了是否可以在R中绘制图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道R中是否有任何包可以使用x,y坐标和形状大小来绘制如下内容:

我有车辆前中心的坐标及其大小(长度和宽度).

编辑

原始数据集如下所示:

 >头(df)Vehicle.ID Frame.ID Global.X Global.Y Vehicle.Length车辆宽度车道Preceding.Vehicle.ID之后.Vehicle.ID间距车距1 2 43 6451214 1873261 14.5 4.9 2 0 13 0 02 2 44 6451217 1873258 14.5 4.9 2 0 13 0 03 2 45 6451220 1873256 14.5 4.9 2 0 13 0 04 2 46 6451223 1873253 14.5 4.9 2 0 13 0 05 2 47 6451225 1873250 14.5 4.9 2 0 13 0 06 2 48 6451228 1873247 14.5 4.9 2 0 13 0 0 

对于任何给定的帧,我想可视化间隙,例如,对于帧号.500:

  ff<-子集(df,Frame.ID == 500)qplot(x = Global.X,y = Global.Y,data = ff) 

所有这些点是车辆的前中心坐标.我不知道如何显示每辆车的长度和宽度以及如何标注间隙值.

解决方案

因此,我不主张您依靠 ggplot 来执行此操作,因为大多数其他建议的解决方案都可能更好,但是这个问题引起了我的兴趣,因为我一直想深入研究 ggplot 的勇气.这是我设法提出的:

  ggplot(df,aes(x = x,y = y,长度=长度,宽度=宽度,填充=标签))+geom_hline(yintercept = seq(5,35,by = 10),color ="white",size = 2,linetype = 2)+geom_car()+coord_equal()+主题(panel.background = element_rect(fill =#555555"),panel.grid.major = element_blank(),panel.grid.minor = element_blank()) 

您还可以添加带有 geom_segment 的箭头或带有 geom_text 的显式标签,但是我们将其留给读者练习.

现在,要使其正常工作,我们必须创建 geom_car ,尽管如果您不需要详细的图片,则可以只使用 geom_rect .这是 geom_car (注意:现在也可以作为

数据:

  df<-read.table(h = T,t =车辆x y长度宽度标签1 150 10 14 5其他2 180 8 12 5其他3 220 10 18 5其他4 145 20 15 5目标5 250 18 14 5其他6160 30 13 5自治7 200 33 15 5其他8240 31 22 5其他") 

I was wondering if there is any package in R that could use x, y coordinates and shape sizes to draw something like this:

I have the coordinates of vehicles' front centers and their sizes (length and width).

Edit

This is what the original data set looks like:

> 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

For any given frame, I want to visualize the gaps, e.g., for frame no. 500:

ff <- subset(df, Frame.ID==500)
qplot(x=Global.X, y=Global.Y, data=ff)

All these dots are the front center coordinates of vehicles. I don't know how to display the length and width of each vehicle and label the gap values.

解决方案

So, I don't advocate you rely on ggplot to do this as most likely some of the other suggested solutions are better, but this problem got me interested as I've been meaning to dig into the guts of ggplot for a while. This is what I managed to come up with:

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())

You can also add arrows with geom_segment or explicit labels with geom_text, but we leave that as an exercise for the reader.

Now, for this to work, we had to create geom_car, though if you don't require detailed pictures, you could just use geom_rect. Here is geom_car (note: also now available as part of the ggbg package):

# 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(...)
  )
}

The car:

The data:

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
")

这篇关于是否可以在R中绘制图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆