在R中绘制重复值

时间:2017-10-18 14:10:35

标签: r ggplot2

这似乎是一个基本问题,但我无法找到解决方案。我有下面提到的数据;

  x             y        group
  1  5.010786 47.29396     1
  2  5.010719 47.29402     2
  3  5.010679 47.29400     3
  4  5.010642 47.29399     4
  5  5.010862 47.29398     5
  6  5.010707 47.29399     6
  7  5.010835 47.29396     7
  8  5.010823 47.29397     8
  9  5.010722 47.29400     9
  10 5.010823 47.29397    10
  11 5.010722 47.29400    11
  12 5.010823 47.29397    12

我正在绘制此数据但忽略了绘图的重复值。在上述数据中有12个数据点。但下面的情节只显示9.我需要绘制每一点。任何人都可以告诉我它是否可能?其次,有什么方法可以防止在绘图时舍入十进制数。 (例如; 5.01078627407352不应该像5.010786那样。)

plot

CODE;

     df <- data.frame(x = lon_redc, y = lat_redc, group = 1:12)
     g <- ggplot(df, aes(x = x, y = y, color = as.factor(group),label = 
     group))
     g <- g + geom_segment(aes(xend=c(tail(x, n=-1), NA),
                      yend=c(tail(y, n=-1), tail(y, n = 1))),
                  arrow=arrow(length = unit(0.7, "cm")))
      g <- g + theme_bw() + theme(legend.position="none") + 
      xlab("longitude") +
      ylab("latitude") + geom_text(aes(x = jitter(x), y = jitter(y)), 
      check_overlap = TRUE, size = 7)
      g

1 个答案:

答案 0 :(得分:0)

问题是你告诉ggplot2删除重叠的文本。绘图时不会出现舍入,所以我不知道你指的是什么。

df <- read.table(text ="    x             y        group
1  5.010786 47.29396     1
2  5.010719 47.29402     2
3  5.010679 47.29400     3
4  5.010642 47.29399     4
5  5.010862 47.29398     5
6  5.010707 47.29399     6
7  5.010835 47.29396     7
8  5.010823 47.29397     8
9  5.010722 47.29400     9
10 5.010823 47.29397    10
11 5.010722 47.29400    11
12 5.010823 47.29397    12", header = TRUE)

g <- ggplot(df, aes(x = x, y = y, color = as.factor(group),label = 
                      group)) + 
  geom_segment(data = cbind(head(df, -1),
                            xend=tail(df$x, n=-1),
                            yend=tail(df$y, n=-1)), 
               aes(xend=xend,
                   yend=yend),
                   arrow=arrow(length = unit(0.7, "cm"))) + 
  theme_bw() + theme(legend.position="none") + 
  xlab("longitude") +
  ylab("latitude") + 
  geom_text(aes(x = jitter(x), y = jitter(y)), 
                               check_overlap = FALSE, size = 7)
g

resulting plot