绘制R中第三个数据值的点

时间:2018-06-06 16:39:37

标签: r ggplot2

我有一个数据框,Savings,有3列,如下所示:

Templates      FTEs        PageTotal  
        1      54.87922      532900.4
      383      34.35612    15165274.1
      765      31.09695    16608751.6
     1147      29.32025    17553846.2
     1529      28.23367    18019766.2
     1911      27.52513    18346629.7
     2293      27.06843    18528129.5
     2675      26.75649    18660953.8
     3057      26.53630    18732909.4
     3439      26.37230    18793627.5
     3821      26.24649    18824156.0
     4203      26.14882    18880440.9
     4585      26.07402    18903224.3
     4967      26.01293    18917600.4
     5349      25.95397    18941023.6
     5731      25.90557    18958726.5
     6113      25.86892    18967352.1
     6495      25.84334    18976647.7
     6877      25.82275    18981660.5
     7259      25.81053    18984535.7

我正在使用以下命令绘制前两列:

ggplot(data=Savings, aes(x=Templates, y=FTEs, color=variable)) +
    geom_line(aes(y=FTEs, col="FTEs"), size=1, color="dodgerblue3") +
    labs(x="Templates", y="FTEs") +
    scale_x_continuous(labels=scales::comma, breaks(0,7700,by=500)) +
    scale_y_continuous(breaks=seq(0,60,by=2))

我想在同一行上绘制第三列“PageTotal”中的一个点的子集,在图表上显示这些点的“PageTotal”值的符号。基本上,我们希望能够看到模板数量的相应页面总数。

根据模板编号,显示的PageTotal子集只能是5个均匀间隔的值。

我不确定使用ggplot2实现此目的的最佳方法。

编辑: 所以我已经做到了这一点:

ggplot(data=Savings, aes(x=Templates, y=FTEs, color=variable)) +
    geom_line(aes(y=FTEs, col="FTEs"), size=1, color="dodgerblue3") +
    geom_point(data=Savings[seq(1,20,by=5),], aes(x=Templats, y=FTEs), color="red") +
    geom_text(data=Savings[seq(1,20,by=5),], aes(y=FTEs, label=format(round(PageTotal, 0), big.mark=",")), hjust=0, vjust=0, color="black") +
    labs(x="Templates", y="FTEs") +
    scale_x_continuous(labels=scales::comma, breaks(0,7700,by=500)) +
    scale_y_continuous(breaks=seq(0,60,by=2))

然而,现在标签太靠近并且在彼此的顶部上方运行。无论如何要么旋转标签,要么交替定位它们以使它们不重叠?

1 个答案:

答案 0 :(得分:1)

使用要绘制的数据创建新数据框,然后更改图层中的数据参数。在这里,我cutTemplates列分成5个大小相同的组,并保留每个组的第一行。

library(tidyverse) 

Savings <- read.table(text = "
Templates      FTEs        PageTotal  
        1      54.87922      532900.4
      383      34.35612    15165274.1
      765      31.09695    16608751.6
     1147      29.32025    17553846.2
     1529      28.23367    18019766.2
     1911      27.52513    18346629.7
     2293      27.06843    18528129.5
     2675      26.75649    18660953.8
     3057      26.53630    18732909.4
     3439      26.37230    18793627.5
     3821      26.24649    18824156.0
     4203      26.14882    18880440.9
     4585      26.07402    18903224.3
     4967      26.01293    18917600.4
     5349      25.95397    18941023.6
     5731      25.90557    18958726.5
     6113      25.86892    18967352.1
     6495      25.84334    18976647.7
     6877      25.82275    18981660.5
     7259      25.81053    18984535.7", header = TRUE)


page_summary <- Savings %>% 
  mutate(Temp_group = cut(Templates, 5)) %>% # cut into groups
  group_by(Temp_group) %>%
  filter(row_number() == 1) # keep first from each group

ggplot(Savings, aes(x = Templates, y = FTEs, label = PageTotal)) +
  geom_line(color="dodgerblue3") + 
  geom_point(data = page_summary, size = 3) +
  geom_label(data = page_summary, hjust = 0, nudge_x = 120, nudge_y = 1) +
  labs(x="Templates", y="FTEs") +
  scale_x_continuous(labels = scales::comma, breaks = seq(0, 7700, by = 500)) +
  scale_y_continuous(breaks = seq(0, 60, by = 2))

enter image description here