r - 排除 ggplot2 图例中的元素
问题描述
我正在使用以下数据制作 ggplot 条形图。这是我用于 geom_segment 的数据:
dispensed.date.start dispensed.date.end drug_class case
2016-11-28 2016-12-07 opioid 12345
2016-12-08 2016-12-15 benzodiazepene 12345
2016-12-18 2016-12-26 MAT 12345
2016-12-24 2016-12-31 opioid 12345
geom_vline 的数据:
Case Event DOD
123456 death 2018-01-02
这是情节:
#set levels, colors
status_levels <- c("Benzodiazepene", "MAT", "Nerve pain / Anticonvulsant", "Opioid", "Sedative", "Stimulant", "Death")
status_colors <- c("#984ea3", "#a65628", "#4daf4a", "#e41a1c", "#ff7f00", "#377eb8", '#000000')
#Plot bars
timeline_plot<-ggplot(PDMP.data.clean,aes(x=dispensed.date.start, y=.2),show.legend = FALSE) +
geom_segment(aes(x=dispensed.date.start, xend=dispensed.date.end, y=drug_class, yend=drug_class,col=drug_class), size=5,show.legend = FALSE) +
# Plot vertical line for date of death
geom_vline(data = deathDate, mapping = aes(x = DOD, xintercept = DOD, y = 0),size=.5, show.legend = TRUE)
timeline_plot<-timeline_plot+labs(col="Drugs Prescribed", show.legend = FALSE)
timeline_plot<-timeline_plot+scale_color_manual(values=status_colors, labels=status_levels, drop = FALSE)
timeline_plot<-timeline_plot+theme_classic()+ scale_x_date(date_breaks = "1 month",date_labels = "%b")
# Don't show axes, appropriately position legend
timeline_plot<-timeline_plot+theme(
legend.position = "none"
)
# configure legend
timeline_plot<-timeline_plot+theme(
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position = "bottom"
)
print(timeline_plot+ ggtitle(paste("Prescription History for Case # ",PDMP.data.clean$case))+
theme(plot.title = element_text(hjust = 0.5),aspect.ratio = 1/3.2,))
这是结果图:
我只希望标记为“死亡”的垂直线显示在图例中,相反,它显示了每个 drug_class 类别旁边的垂直线。我不确定为什么这会显示在图例中,因为我虽然明确排除了这行代码:
timeline_plot<-ggplot(PDMP.data.clean,aes(x=dispensed.date.start, y=.2),show.legend = FALSE) +
geom_segment(aes(x=dispensed.date.start, xend=dispensed.date.end, y=drug_class, yend=drug_class,col=drug_class), size=5,show.legend = FALSE)
解决方案
可能的解决方案是在geom_vline
语句中设置线型,然后手动定义图例。
请参阅对“geom_vline”行的修改和添加的“scale_linetype("Legend", labels="Death")" 行
timeline_plot<-ggplot(PDMP.data.clean, aes(x=dispensed.date.start, y=.2)) +
geom_segment( aes(x=dispensed.date.start, xend=dispensed.date.end, y=drug_class, yend=drug_class, col=drug_class), size=5, show.legend = FALSE) +
# Plot vertical line for date of death
geom_vline(data = deathDate, mapping = aes(x=DOD, xintercept = DOD, linetype = "2"), size=.5) +
scale_linetype("Legend", labels="Death")
## If color legend is not plotted the following line isn't needed
# timeline_plot<-timeline_plot+labs(col="Drugs Prescribed")
timeline_plot<-timeline_plot+scale_color_manual(values=status_colors, labels=status_levels, drop = FALSE)
timeline_plot<-timeline_plot+theme_classic()+ scale_x_date(date_breaks = "1 month",date_labels = "%b")
# Don't show axes, appropriately position legend
# timeline_plot<-timeline_plot+theme(legend.position = "none") #not needed
# configure legend
timeline_plot<-timeline_plot+theme(
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position = "bottom")
print(timeline_plot+
ggtitle(paste("Prescription History for Case # ", PDMP.data.clean$case)) +
theme(plot.title = element_text(hjust = 0.5), aspect.ratio = 1/3.2,))
推荐阅读
- javascript - List.js 默认应用过滤器
- python - `/bin/sh` 在 docker 内的 Jenkins 内的 tox 中找不到
- python - 为什么对 python 服务器使用 struct 而不是 pickle
- javascript - JS API 根据某些参数的值获取数据库项
- mysql - mysql 错误代码:1066。不是唯一的表/别名:'t2'
- javascript - 条件为真时添加数字的逻辑
- jquery - 格式化文本的数据标题中的 HTML 代码
- android - 在按钮单击时过滤 Firebase 数据库返回单个回收视图列表项
- opencv - 通过 OpenCV 查找表格的轮廓
- html - Bootstrap 两列布局图像和文本响应式