在控制线型和自定义标签的同时,使用geom_hline添加其他图例

时间:2019-07-04 12:16:39

标签: geom-hline

我希望有一个多面的箱线图,其中每个力量训练设备的y轴都有差异,而x轴却有参与者的研究对象。当我想包括单个设备的中位数时,我做了一个额外的data.frame:

structure(list(Machine = c("ABDUCTOR", "ADDUCTOR", "CHEST PRESS", 
"LEG CURL", "LEG EXTENSION", "LEG PRESS", "LOWER BACK", "TOTAL ABDOMINAL", 
"VERTICAL TRACTION"), med = c(-180, -175, -120, -85, -100, -150, 
-92.5, -85, -115), solid = c("solid", "solid", "solid", "solid", 
"solid", "solid", "solid", "solid", "solid")), row.names = c(NA, 
-9L), class = "data.frame")

该图显示的很好,但是我现在面临的问题是,我想在侧面增加一个图例,以指示每个设备的中值(yintercept)。一段时间以来,我一直在挣扎,无法在线找到一个解决方案,该解决方案让我可以控制linetype =“ solid”并添加每个设备的中间值,包括图例中的设备名称。

median_con<-as.data.frame(all_data_wide %>% group_by(Machine) %>% filter(Contraction_Mode_Levels=="Con") %>% summarise(med=median(RaterSmartphone_Diff, na.rm=TRUE)))
median_con$solid<-rep(c("solid"))

plot_con<-ggplot(all_data_wide %>% filter(Contraction_Mode_Levels=="Con")) +
  aes(x = Participant, y = RaterSmartphone_Diff, colour = Participant, fill=Participant) +
  geom_boxplot(outlier.shape = NA) +
  scale_color_viridis_d(option = "viridis") +
  theme_classic() +
  #geom_hline(yintercept=0, color = "black", size=1, linetype="dotted",)+
  geom_hline(data = median_con, aes(yintercept = med,linetype=as.factor(median_con$solid)), size=1,  show.legend =TRUE)+
  scale_linetype_manual(name = "Median per Machine", values = as.factor(median_con$Machine), labels = c("A", "B", "C"),guide = guide_legend(override.aes = list(color = c("black"))))+
  facet_wrap(vars(Machine))+
  coord_cartesian(ylim = c(-1000, 1000))+
  theme(text = element_text(size = 16),axis.text = element_text(size = 12),plot.title = element_text(hjust = 0.5),plot.margin=unit(c(1,1,1.8,1.2),"cm"))+
  labs(y = "Video - Algorithm [ms]")+
  ggtitle(" Concentric Contraction Difference Video - Algorithm per Machine" )
plot_con

enter image description here

所需的图例以红色突出显示。预先非常感谢您提供有用的意见。

0 个答案:

没有答案