简体   繁体   English

如何在r中向散点图添加箭头?

[英]How to add arrows to scatterplot in r?

I have a 'fire' dataset that looks like this:我有一个像这样的“火”数据集:

    species  THREATEN_1    Total_area   Area_remai
    a.         V.              30.        10
    b.         EN.             100.       50
    c.         V.              5.          2

I want to create a scatterplot which shows the movement of habitat (ie from Total_area to Area_remai).我想创建一个散点图,显示栖息地的运动(即从 Total_area 到 Area_remai)。 Similar to the attached.类似于附件。

在此处输入图片说明

I have started the code, but don't know how to add the lines and movement:我已经开始代码了,但不知道如何添加线条和运动:

    g <- ggplot(fires, aes(x = THREATEN_1, y = Total_area, color = THREATEN_1)) + 
    geom_point(fill = "indianred") +
    labs(x = "Threatened status", y = "Extent of suitable habitat (Ha)") 
    g + geom_jitter(aes(color = THREATEN_1), alpha = 0.25,
              position = position_jitter(width = 0.3)) +
    theme(legend.position = "none") 

Here is a way to plot the arrows.这是绘制箭头的一种方法。 Use geom_segment , argument arrow paying attention to the arrow(length, unit) settings.使用geom_segment ,参数arrow注意arrow(length, unit)设置。

library(ggplot2)

g <- ggplot(fires, aes(x = THREATEN_1, y = Total_area, color = THREATEN_1)) + 
  geom_point() +
  labs(x = "Threatened status", y = "Extent of suitable habitat (Ha)") 

g + geom_segment(aes(xend = THREATEN_1, yend = Area_remai),
                 arrow = arrow(length = unit(0.025, "npc"))) +
  scale_color_manual(values = c("indianred", "dodgerblue")) +
  theme_bw() +
  theme(legend.position = "none") +
  facet_wrap(~ THREATEN_1, scales = "free_x")

在此处输入图片说明

jitter the data. jitter数据。

The code above will overplot the arrows, in the question it is asked to jitter the data set.上面的代码将过度绘制箭头,在要求它抖动数据集的问题中。

library(dplyr)

set.seed(1234)
fires %>%
  group_by(THREATEN_1) %>%
  mutate(THREATEN_1b = jitter(as.numeric(THREATEN_1), amount = 0.3/2)) %>%
  ggplot(aes(x = THREATEN_1b, y = Total_area, color = THREATEN_1)) +
  geom_point(aes(color = THREATEN_1), alpha = 0.25) +
  labs(x = "Threatened status", y = "Extent of suitable habitat (Ha)") -> g

g + geom_segment(aes(xend = THREATEN_1b, yend = Area_remai),
                 arrow = arrow(length = unit(0.025, "npc"))) +
  scale_color_manual(values = c("indianred", "dodgerblue")) +
  theme_bw() +
  theme(legend.position = "none",
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank()) +
  facet_wrap(~ THREATEN_1, scales = "free_x")

Data.数据。

fires <- read.table(text = "
species  THREATEN_1    Total_area   Area_remai
    a         V              30.        10
    b         EN             100.       50
    c         V              5.          2
", header = TRUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM