r - 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)的数据框,即此处。我真的需要分成几列吗?以及如何处理中间值?
解决方案
您可以使用as.yearmon
from zoo
package 对您的月/年数据进行编码。
要计算每个月的照片数量,将group_by
和summarise
。
要绘制线段,将创建第二个数据表来指定最小和最大日期。
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_yearmon
(zoo
不需要包)。
scale_x_date
将允许指示中断(每 3 个月)和标签中的内容(现在月份和 4 位数年份,例如,2019 年 3 月)。
我们可以使用(转换时使用每月的第一天),而不是将您的转换data
为(月/年)。yearmon
Date
还在情节周围添加了小边距。
#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"))
推荐阅读
- cmake - [cmake]包含来自另一个 sdk 文件夹的源文件导致错误
- inference - CoxTimeVaryingFitter 推理模型
- mysql - mysql通过json选择过滤更新
- javascript - 为什么我的滑动效果在我的 react 应用程序中没有按预期工作?
- angular - 从其他文件夹使用时,ng 命令不起作用
- excel - 基于另一列标准并忽略空白的最常见文本
- angular - 不能在未定义上使用地图功能,从订阅返回,服务数据延迟,直到时间地图功能被执行
- javascript - 如何将 AJAX 成功传递给另一个带参数的函数?
- c++ - 如何使用 micro pro 在记事本中打印表情符号
- android - 更改 OutlinedTextField 的边框半径