使用ggplot2为甘特图创建图例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用ggplot2为甘特图创建图例相关的知识,希望对你有一定的参考价值。
好的,所以我想做一些简单的事情,但似乎无法完成它。我想为图表上的点做一个传奇,然而,无论我做什么,我似乎都无法使其成功。如果你能帮助我为ggplot中创建的点和线创建一个传奇,我真的很感激!形状15点应为“进入市场”,形状19应为“退出市场”。
table1 <- as.data.frame(matrix(c(
"Drug1", "Filing", "2020-04-12",
"Drug2", "Filing", "2020-05-12",
"Drug3", "Filing", "2020-04-23",
"Drug4", "Filing", "2020-01-27",
"Drug5", "Filing", "2020-02-02",
"Drug1", "Approval", "2021-04-12",
"Drug2", "Approval", "2021-06-12",
"Drug3", "Approval", "2021-07-23",
"Drug4", "Approval", "2021-04-27",
"Drug5", "Approval", "2021-03-02"),byrow = TRUE,ncol=3,nrow=10))
colnames(table1) <- c("Name", "variable", "value")
table1$value <- as.Date(table1$value)
ggplot(table1 , aes(as.Date(value, "%m/%d/%Y"), Name))+
geom_point(data= table1 [table1 $variable=="Filing",], aes(as.Date(value, "%m/%d/%Y"), Name), shape=15, size = 3.5)+
geom_point(data= table1 [table1 $variable=="Approval",], aes(as.Date(value, "%m/%d/%Y"), Name), shape=19, size = 3.5)+
geom_line(data=(table1 [table1 $variable=="Filing" | table1 $variable=="Approval",]), aes(as.Date(value, "%m/%d/%Y"), Name), linetype="solid", size=1)
答案
我自己在这里想出来了!
table1 <- as.data.frame(matrix(c(
"Drug1", "Filing", "2020-04-12",
"Drug2", "Filing", "2020-05-12",
"Drug3", "Filing", "2020-04-23",
"Drug4", "Filing", "2020-01-27",
"Drug5", "Filing", "2020-02-02",
"Drug1", "Approval", "2021-04-12",
"Drug2", "Approval", "2021-06-12",
"Drug3", "Approval", "2021-07-23",
"Drug4", "Approval", "2021-04-27",
"Drug5", "Approval", "2021-03-02"),byrow = TRUE,ncol=3,nrow=10))
colnames(table1) <- c("Name", "variable", "value")
table1$value <-as.Date(table1$value)
shapes <- c("s1" = 15, "s2" = 19)
ggplot(table1 , aes(as.Date(value, "%m/%d/%Y"), Name))+
geom_point(data= table1 [table1 $variable=="Filing",], aes(as.Date(value, "%m/%d/%Y"), Name, shape = "s1"),size=3)+
geom_point(data= table1 [table1 $variable=="Approval",], aes(as.Date(value, "%m/%d/%Y"), Name, shape = "s2"),size=3)+
geom_line(data=(table1 [table1 $variable=="Filing" | table1 $variable=="Approval",]), aes(as.Date(value, "%m/%d/%Y"), Name), linetype="solid", size=1)+
scale_shape_manual(breaks = c("s1", "s2"),values=c("s1" = 15, "s2" = 19),labels = c("Filing Date", "Approval Date"))
以上是关于使用ggplot2为甘特图创建图例的主要内容,如果未能解决你的问题,请参考以下文章