r - 在 R 中使用 Geom_Segment
问题描述
我正在尝试使用 Geom_Segment 在 R 中创建一个绘图。我遇到了一个错误,说我需要输入 yend
,但我已经在输入了……这是我的代码:
library(ggplot2)
library(data.table)
library(magrittr)
dataset$From<-Sys.Date()
format(dataset$From, format="%Y-%m-%dT%H:%M:%OS")
dataset$To<-Sys.Date()
format(dataset$To, format="%Y-%m-%dT%H:%M:%OS")
ggplot(dataset, aes(x=datetime_start, y=dataset$Audit_Title,
color=dataset$Employee_Name)) +
geom_segment(aes(x=dataset$From,xend=dataset$To,y=dataset$Audit_Title,yend=dataset$Audit_Title),size=20)+
scale_colour_discrete(guide=guide_legend(override.aes=list(size=15))) +
ggtitle("Audit by Employee Timeline") + xlab("") + ylab("") + theme_bw()
样本数据: 这是样本数据
这就是我如何更改下面的代码以从 Excel 中获取我输入到 Power BI 中的数据:
library(ggplot2)
library(dplyr)
# transform into date
dataset <- dataset %>%
mutate_at(vars(dataset$From, dataset$To),
.funs = function(tt) readr::parse_date(as.character(tt),
format = "%m/%d/%Y"))
ggplot(dataset)+
geom_segment(aes(x=dataset$From, xend=dataset$To,
y=dataset$Employee_Name, yend=dataset$Employee_Name))
解决方案
First of all, ideally you would share your data as a dput(dataset)
. If you can't share real data, you should make a minimal reproducible example and share that. See here
Here's your data
library(ggplot2)
library(dplyr)
df <-
read.table(
text =
"01/03/2020 03/16/2020 Supply_Chain John_Smith
05/08/2020 08/20/2020 Business_Unit Karen_Scott")
names(df) <- c("From", "To", "Audit_Title", "Employee_Name")
# transform into date
df <- df %>%
mutate_at(vars(From, To),
.funs = function(tt) readr::parse_date(as.character(tt),
format = "%m/%d/%Y"))
Now do the actual plot by selecting the proper x
xend
and having y
be the employee (y
=yend
).
ggplot(df)+
geom_segment(aes(x=From, xend=To,
y=Employee_Name, yend=Employee_Name))
Which produces
If you want fancy colors, labels and stuff go ahead and check the proper documentation for ggplot
. See here
推荐阅读
- reactjs - 如何在反应 js 应用程序的响应中添加 http 标头
- python - 进行类不平衡正则化的正确位置(数据级别或批处理级别)
- c++ - 如何为 std::map 重载插入运算符 >> in
C++? - sql - Azure 数据库 - 从带有参数的逻辑应用程序运行 azure 存储过程
- azure - 如何使用 Linux 在 Azure 应用服务上安装依赖二进制文件?
- swift - 在 Swift 中显示操作系统
- javascript - 尝试从传单地理编码器返回纬度和经度值
- strapi - 特定字段的 Strapi 总和值
- java - 如何通过 Maven 运行 Stilliard/pure-ftpd
- swift - 将包写为文件