首页 > 解决方案 > r 中的时间与 ggplot 没有确定初始值和最终值

问题描述

我需要帮助来创建图表脚本。信息如下:

我有这个虚构的数据

table <- data.frame(ind = c("Ind1","Ind1","Ind1","Ind1","Ind1","Ind1","Ind2",
                      "Ind2","Ind2","Ind3","Ind3","Ind3","Ind3","Ind4",
                      "Ind4","Ind4","Ind5","Ind5","Ind5","Ind5","Ind5",
                      "Ind5"),

           photo = c("55", "62", "63", "65", "70", "97", "100", "105",
                    "109", "72", "74", "76", "101", "140", "150", "170",
                    "168", "172", "182", "185", "189", "194"),


           data = c("jan/17", "mar/17", "mar/17", "apr/17",
                     "jun/17", "oct/17", "dec/17", "apr/18",
                     "may/18", "aug/17", "sep/17", "sep/17",
                     "dec/17", "aug/18", "nov/18", "feb/19",
                     "jan/19", "feb/19", "mar/19", "mar/19",
                     "mar/19", "jul/19")) 

我想生成一个这样的图表,其中包含个人姓名 x 会议日期。我想要根据当月存在的照片数量以及符号上方的照片数量(像这样)来确定符号的大小。

我在互联网上找到的所有内容都使用具有两列(开始 x 和最终 x)的数据框,即此处。我真的需要分成几列吗?以及如何处理中间值?

标签: rggplot2timeline

解决方案


您可以使用as.yearmonfrom zoopackage 对您的月/年数据进行编码。

要计算每个月的照片数量,将group_bysummarise

要绘制线段,将创建第二个数据表来指定最小和最大日期。

library(zoo)
library(ggplot2)
library(dplyr)

my_table$ind <- factor(my_table$ind)
my_table$mo_yr <- as.yearmon(my_table$data, "%b/%y")

my_table_sum <- my_table %>%
  group_by(mo_yr, ind) %>%
  summarise(count = n())

my_table_range <- my_table_sum %>%
  group_by(ind) %>%
  summarise(min = min(mo_yr),
            max = max(mo_yr))

ggplot(data = my_table_sum, aes(x = mo_yr, y = ind)) +
  scale_x_yearmon() +
  geom_point(aes(size = count)) +
  geom_text(aes(label = ifelse(count > 1, as.character(count), '')), vjust = -1) +
  scale_size_continuous(range = c(1, 3), breaks = c(1,2,3)) +
  geom_segment(data = my_table_range, aes(x = min, xend = max, y = ind, yend = ind)) +
  theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.position="none")

时间线图

编辑:为了在 x 轴刻度和标签中获得更大的灵活性,您可能想要使用scale_x_date而不是scale_x_yearmonzoo不需要包)。

scale_x_date将允许指示中断(每 3 个月)和标签中的内容(现在月份和 4 位数年份,例如,2019 年 3 月)。

我们可以使用(转换时使用每月的第一天),而不是将您的转换data为(月/年)。yearmonDate

还在情节周围添加了小边距。

#library(zoo)
library(ggplot2)
library(dplyr)

my_table$ind <- factor(my_table$ind)
#my_table$mo_yr <- as.yearmon(my_table$data, "%b/%y")
my_table$dates <- as.Date(paste0("1/", my_table$data), format = "%d/%b/%y")

my_table_sum <- my_table %>%
  group_by(dates, ind) %>%
  summarise(count = n())

my_table_range <- my_table_sum %>%
  group_by(ind) %>%
  summarise(min = min(dates),
            max = max(dates))

ggplot(data = my_table_sum, aes(x = dates, y = ind)) +
  scale_x_date(date_breaks = "3 months", date_labels = "%b %Y") +
  geom_point(aes(size = count)) +
  geom_text(aes(label = ifelse(count > 1, as.character(count), '')), vjust = -1) +
  scale_size_continuous(range = c(1, 3), breaks = c(1,2,3)) +
  geom_segment(data = my_table_range, aes(x = min, xend = max, y = ind, yend = ind)) +
  theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.position="none",
        plot.margin=unit(c(1,1,1,1),"cm"))

每 3 个月绘制一次刻度和标签


推荐阅读