【问题标题】:Plot points for third data value on line in R在 R 中在线绘制第三个数据值的点
【发布时间】:2018-06-06 16:39:36
【问题描述】:

我有一个数据框,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))

但是,现在标签靠得太近了,彼此重叠。无论如何要旋转标签,还是交替放置它们以使它们不重叠?

【问题讨论】:

  • 您的数据框中没有 valuevariable 列。你错过了一些处理代码
  • 我对其进行了编辑以引用该列。在实际图表中,我绘制了几条不同的线。在 geom_lines 调用中,它认识到 aes(y=FTEs, ... 替换了“值”引用。我不知道它是否应该这样做,但它对我来说是这样的。color=variable 可以同样的方式。

标签: r ggplot2


【解决方案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))

【讨论】:

  • 这正是我想要的。谢谢。有时候ggplot界面感觉有点神奇。
猜你喜欢
  • 2015-03-19
  • 1970-01-01
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 2015-01-24
  • 2020-10-08
  • 2023-01-13
相关资源
最近更新 更多