Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 排除ggplot2图例中的图元_R_Ggplot2 - Fatal编程技术网

R 排除ggplot2图例中的图元

R 排除ggplot2图例中的图元,r,ggplot2,R,Ggplot2,我正在使用以下数据制作ggplot条形图。以下是我用于geom_段的数据: 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

我正在使用以下数据制作ggplot条形图。以下是我用于geom_段的数据:

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,))
#设置级别、颜色

可能的解决方案是在
geom\u vline
语句中设置线型,然后手动定义图例。
请参见对“几何线”行的修改和添加的“缩放线型(“图例”,labels=“Death”)”行


timeline\u plotDave2e-谢谢-为什么必须设置legend.position=“无”,然后再次将其设置为“底部”?这让我很困惑。@DiamondJoe12,我刚刚从上面复制了你的代码。该行不需要,可以删除。
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)
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,))