更改图例以指示线型...(ggplot2)

加里东尼亚26

我有以下公式:

ggplot(Errortrialsmodifyoriginal, aes(x = Target, y = Absolutefirststoperror, color = as.character(Type), shape = as.character(Type))) +
 geom_point(shape=16)+
 geom_point(data=Errortrialoriginal,shape=19,size = 4,mapping=aes(x=Target, y=Absolutefirststoperror)) +
 geom_line(data=Errortrialoriginal,aes(group=Type,linetype=Type),size=2,) +
 scale_color_manual(name = "Condition", values = c("red","green","blue","red","green","blue")) +
 scale_linetype_manual(name = "Condition",values = c("dashed","dashed","dashed","solid","solid","solid")) +
 geom_errorbar(data=Errortrialoriginal,mapping=aes(x=Target, ymin=Absolutefirststoperror-SE,ymax=Absolutefirststoperror+SE),size=0.5) +
 theme_bw() + guides(color = guide_legend("Condition"), shape = guide_legend("Condition"), linetype = guide_legend("Condition")) +
 labs(x = "Target distance (vm)", y = "Absolute error in stop location (vm)") +
 theme(axis.title.x = element_text(size=14, face="bold"), axis.title.y = element_text(size=14, face="bold"),legend.text=element_text(size=14),title=element_text(size=14,face="bold"), plot.title = element_text(hjust = 0.5), legend.title = element_text(size=14,face="bold"), axis.text.x=element_text(size=14),axis.text.y=element_text(size=14),panel.grid.major = element_blank(), panel.grid.minor = element_blank())

产生图:

在此处输入图片说明

如何更改命令以确保虚线和实线显示在图例中?因为目前,图例表明所有线条都是实心的,即使事实并非如此?

如有任何建议,我将不胜感激!

dc37

在我看来,图例可以正确显示,但是您看不到它,因为线型前面有很多点。您应该增加图例框才能看到它。

下面是这个虚拟示例的示例:

library(ggplot2)

ggplot(my_data, aes(x = dose, y = length, color = supp, linetype = supp))+
  geom_line()+
  geom_point(size = 4)

在此处输入图片说明

library(ggplot2)

ggplot(my_data, aes(x = dose, y = length, color = supp, linetype = supp))+
  geom_line()+
  geom_point(size = 4)+
  theme(legend.key.size = unit(3,"line"))

在此处输入图片说明

因此,使用您的代码,您可以执行以下操作:

library(ggplot2)

ggplot(Errortrialsmodifyoriginal, 
       aes(x = Target, 
           y = Absolutefirststoperror, 
           color = Type)) + 
  geom_point()+ 
  geom_line(data=Errortrialoriginal,
            aes(group=Type,
                linetype=Type)) + 
  scale_color_manual(name = "Condition", values = rep(c("red","green","blue"),2)) + 
  scale_linetype_manual(name = "Condition",values = rep(c("dashed","solid"),each =3)) + 
  geom_errorbar(data=Errortrialoriginal,
                mapping=aes(x=Target, 
                            ymin=Absolutefirststoperror-SE,
                            ymax=Absolutefirststoperror+SE),size=0.5) + 
  theme_bw() + 
  guides(color = guide_legend("Condition"), shape = guide_legend("Condition"), linetype = guide_legend("Condition")) + 
  labs(x = "Target distance (vm)", y = "Absolute error in stop location (vm)") + 
  theme(axis.title.x = element_text(size=14, face="bold"), 
        axis.title.y = element_text(size=14, face="bold"),
        legend.text=element_text(size=14),
        title=element_text(size=14,face="bold"), 
        plot.title = element_text(hjust = 0.5), 
        legend.title = element_text(size=14,face="bold"), 
        axis.text.x=element_text(size=14),
        axis.text.y=element_text(size=14),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.key.size = unit(3,"line"))

它能回答您的问题吗?

如果不是,请考虑提供数据集的可复制示例(请参阅:如何制作出色的R可复制示例

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章