【问题标题】:How to draw a nice arrow in ggplot2如何在ggplot2中画一个漂亮的箭头
【发布时间】:2016-06-24 08:27:50
【问题描述】:

我正在创建一个ggplot 图表,我希望在两点之间有一些箭头。使用geom_line(arrow = arrow()) 可以轻松完成主要任务。但是,我想要一些“漂亮”的粗箭头。通过size= 调整箭头大小并没有帮助,因为它完全弄乱了箭头的头部。我说明我的问题:

创建一些样本数据和绘图:

 NAME <- c("A", "A", "B", "B", "C", "C")
 YEAR <- c(2016, 2011, 2016, 2011, 2016, 2011)
 YEAR <- as.factor(YEAR)
 VALUE <- c(1, 4, 1, 5, 2, 8)
 DATA <- data.frame(NAME, YEAR, VALUE)

ggplot(DATA, aes(x=VALUE, y=NAME)) + 
  geom_point(size=5, aes(colour=YEAR)) +
  geom_line(arrow = arrow(length=unit(0.30,"cm"), ends="first", type = "closed"))

生成的图如下所示:

现在我尝试“加粗”箭头...

ggplot(DATA, aes(x=VALUE, y=NAME)) + 
  geom_point(size=5, aes(colour=YEAR)) +
  geom_line(arrow = arrow(length=unit(0.30,"cm"), ends="first", type = "closed"), size = 3)

这就是这里显示的结果:

我的问题:有没有办法绘制一些“漂亮”的粗箭头?

【问题讨论】:

  • 改变角度,增加箭头的长度?
  • 尝试使用不同的参数:linetype(可以用虚线等点划线),arrow length
  • 相关:stackoverflow.com/questions/20658071/… - 不幸的是,答案有点像寻宝游戏(我不知道他为什么没有发布完整的修改功能)。另外,我不知道它是否也适用于较新的 ggplot 版本。

标签: r ggplot2 arrows


【解决方案1】:

这里有一些可重现的示例(尝试运行它们)

A Simple arrow(即线段):

library(dplyr)
library(ggplot2)

# Create a scatter plot
i <- ggplot(mtcars, aes(wt, mpg)) + geom_point()

# Add arrow
i + geom_segment(aes(x = 5, y = 30, xend = 3.5, yend = 25),
                  arrow = arrow(length = unit(0.5, "cm")))

A Simple curved arrow

b <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point()

df <- data.frame(x1 = 2.62, x2 = 3.57, y1 = 21.0, y2 = 15.0)

b + geom_curve(
  aes(x = x1, y = y1, xend = x2, yend = y2),
  data = df,
  arrow = arrow(length = unit(0.03, "npc"))
)

Available Arrow Types

您不必理解此代码,只需注意您可用的lineendlinejoin 选项

df2 <- expand.grid(
  lineend = c('round', 'butt', 'square'),
  linejoin = c('round', 'mitre', 'bevel'),
  stringsAsFactors = FALSE
)
df2 <- data.frame(df2, y = 1:9)

ggplot(df2, aes(x = 1, y = y, xend = 2, yend = y, label = paste(lineend, linejoin))) +
  geom_segment(
    lineend = df2$lineend, linejoin = df2$linejoin,
    size = 3, arrow = arrow(length = unit(0.3, "inches"))
  ) +
  geom_text(hjust = 'outside', nudge_x = -0.2) +
  xlim(0.5, 2)

为自己切换的直箭头

这里有一个非常简单的箭头来调整每个参数,看看它的作用

ggplot(iris) +
  geom_segment(
    x = 1, y = 1,
    xend = 4, yend = 7,
    lineend = "round", # See available arrow types in example above
    linejoin = "round",
    size = 2, 
    arrow = arrow(length = unit(0.3, "inches")),
    colour = "#EC7014" # Also accepts "red", "blue' etc
  ) + 
  scale_x_continuous(limits = c(0, 10)) +
  scale_y_continuous(limits = c(0, 10))

自己切换的弯曲箭头

# Nicer curve
b <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point()

b + geom_curve(
  aes(x = 3, y = 22, xend = 3.5, yend = 15),
  arrow = arrow(
    length = unit(0.03, "npc"), 
                type="closed" # Describes arrow head (open or closed)
    ),
  colour = "#EC7014",
  size = 1.2,
  angle = 90 # Anything other than 90 or 0 can look unusual
)

【讨论】:

    【解决方案2】:

    我通常使用geom_segment 来创建箭头。但要做到这一点,我们需要将数据从“长”格式修改为“宽”格式(通常使用来自reshape2data.table 包的dcast)。但是这次我尝试使用basereshape函数。

    ggplot(DATA, aes(x=VALUE, y=NAME)) + 
      geom_point(size=5, aes(colour=YEAR)) +
      geom_segment(data = reshape(DATA, v.names="VALUE", idvar = "NAME", timevar = "YEAR", direction = "wide"),
                   aes(x=VALUE.2011, xend=VALUE.2016, y=NAME, yend=NAME), size = 2,
                   arrow = arrow(length = unit(0.5, "cm")))
    

    编辑:我刚刚发现同样的问题与“封闭”型箭头有关。现在,尝试将绘图保存为矢量图(pdf 或 svg,使用 ggsave 或绘图选项卡中的导出菜单)。结果不是“乱七八糟”。

    【讨论】:

      【解决方案3】:

      ggplot2 的最新版本(我的意思是devtools::install_github("tidyverse/ggplot2") 最新,在撰写此答案时-我相信他们会尽快将其主线化),geom_segment 有一个linejoin 参数.使用linejoin='mitre' 将提供清晰的边缘。请参阅following for details

      【讨论】:

        【解决方案4】:

        有一个非常简单但有点“hacky”的解决方案。

        想法是先画线(以所需的粗细,但没有箭头),但要短一点(在某些情况下可以计算)。然后只画第二条线,不改变箭头的大小。生成的叠加层将按照您想要的方式显示。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-03-21
          • 1970-01-01
          • 2018-11-18
          • 1970-01-01
          • 2019-12-22
          • 1970-01-01
          • 2014-11-01
          • 1970-01-01
          相关资源
          最近更新 更多