R语言 为速度图添加图例-关键字形长度以表示值[重复]

hec6srdp  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(104)

此问题在此处已有答案

creating a bar that indicates the length of the line segment using ggplot2(1个答案)
3天前关闭。
我最近问了一个关于如何在ggplot中绘制velocity图的问题。
现在,我想请您帮助创建一个图例,该图例将对应于我的数据中的速度。我的目标是像这样的arrow legend,其长度将对应于当前图的5 m/s或0.5 m/s。
谢谢你,我希望你能帮助我。
下面是我的脚本:

Speed <- c(24, 23, 23, 24, 26, 27, 27, 27, 26, 24)
Dir <- c(108, 105, 103, 100, 97, 96, 97, 99, 101, 103)
Date <- c('2016-08-01', '2016-08-02', '2016-08-3', '2016-08-4', '2016-08-5', '2016-08-6', '2016-09-7', '2016-09-8', '2016-09-9', '2016-09-10')

DF <- data.frame( Speed, Dir, Date )
DF$Date <- as.Date(1, as.Date(DF$Date, origin = "2016-08-04"))

# make the plot
ggplot(DF) +
    geom_segment(aes(x = Date,
                     y = 0,
                     xend = Date + Speed * 1 * cos( (90-Dir) / 360 * 2 * pi),
                     yend = Speed * 1 * sin( (90-Dir) / 360 * 2 * pi),
                     col = factor(Date)
    ),
    arrow = arrow(length = unit(0.5, "cm")) ) +
    geom_point(aes(Date, 0), size = 1) +
    scale_x_date(labels = date_format('%b'), breaks = date_breaks('1 month')) +
    coord_fixed(1) +
    theme(legend.position = "none")
vsdwdz23

vsdwdz231#

我认为最简单的方法是在绘图区域外进行注解。如果长度应该与绘图中箭头的实际长度相对应,我认为使用垂直或水平箭头是有意义的。
您可以在单独的数据框中预先计算它们并将其用于注解。这里我将如何为水平箭头图例制作水平箭头。注意,0.5m/s的速度是超级小的,它确实有一个箭头,但它很小!
代码中的其他注解
注意,我稍微修改了三角函数,用90-Dir计算Angular 意味着0度指向上,90度指向右,这可能并不总是惯例。

library(tidyverse)

Speed <- c(24, 23, 23, 24, 26, 27, 27, 27, 26, 24)
Dir <- c(108, 105, 103, 100, 97, 96, 97, 99, 101, 103)
Date <- c("2016-08-01", "2016-08-02", "2016-08-3", "2016-08-4", "2016-08-5", "2016-08-6", "2016-09-7", "2016-09-8", "2016-09-9", "2016-09-10")

DF <- data.frame(Speed, Dir, Date)
DF$Date <- as.Date(1, as.Date(DF$Date, origin = "2016-08-04"))

## I pre-calculate those, because I prefer to have the data frames ready and
## my plots slightly cleaner
DF <-
  DF %>%
  mutate(
    xend = Date + Speed * 1 * cos((90 - Dir) * pi / 180),
    yend = 0 + Speed * 1 * sin((90 - Dir) * pi / 180)
  )

## The joy starts here - defining x or y coordinates for your annotation can
## be daunting.
df_leg <-
  data.frame(
    Speed = c(0.5, 5),
    y = 2*min(DF$yend),
    Dir = 90, Date = range(DF$Date)
  ) %>%
  ## for the label x coord
  mutate(xend = Date + Speed * 1 * cos((90-Dir) * pi / 180))

## your plot, just slightly modified
ggplot(DF) +
  geom_segment(aes(
    x = Date, y = 0,
    xend = xend, yend = yend, col = factor(Date)
  ),
  arrow = arrow(length = unit(0.5, "cm"))
  ) +
  geom_point(aes(Date, 0), size = 1) +
  geom_segment(
    data = df_leg,
    aes(
      x = Date, y = y, xend = xend,
      yend = y
    ), 
    ## you need to experiment with the arrow length, I'd pass a vector
    arrow = arrow(length = unit(3:4, "pt")), show.legend = FALSE
  ) +
  geom_text(
    data = df_leg, aes(x = xend, y = y, label = paste(Speed, "m/s")),
    hjust = -.1
  ) +
  scale_x_date(labels = scales::date_format("%b"), breaks = scales::date_breaks("1 month")) +
  coord_fixed(1, ylim = c(min(DF$yend), 0), clip = "off") +
  theme(legend.position = "none")

创建于2023-03-23带有reprex v2.0.2

相关问题