用ggplot操作图例

时间:2019-08-01 18:31:01

标签: r ggplot2

我正在尝试更改图中的图例,而且以前从未遇到过这么多麻烦。我想要1)没有标题&2)而不是变量值“ s1,s2,s3”,我想要“血清型1,2,3”。

我知道我可以重新标记因子,但是我不想这样做,因为我很固执,而且我也不希望变量值中有空格

为什么scale_shape_manual不起作用?

ggplot()+
      geom_point(data=seromelt, aes(x=X, y=value, color=variable, group=variable), 
                 stat= 'identity', size=2, position=position_dodge(width =.2)) + 
      geom_errorbar(data=tbl1, aes(x=X, y=value, color=variable, ymin=value-se, ymax= value+se), 
                    width= 0, size=1, position=position_dodge(width =.2)) +
      ylab("Percent") +
      scale_y_continuous(breaks=seq(0,100,10)) +
      xlab("Health Zone") +
      scale_shape_manual(name="", 
                         breaks= c("s1", "s2", "s3"),
                         labels= c("Serotype 1", "Serotype 2", "Serotype 3")) +
      theme_bw() +
      theme(panel.grid.major.y = element_blank(), 
            axis.text.x = element_text(size=12), 
            axis.text.y = element_text(size=12), 
            legend.text =element_text(size=12)) +
      coord_flip() 

当前结果:

plot of above code

1 个答案:

答案 0 :(得分:1)

它必须是scale_colour_discrete。试试这个:

 {the rest of ggplot} + scale_colour_discrete(name  = "",
                        breaks=c("s1", "s2", "s3"),
                        labels=c("Serotype 1", "Serotype 2", "Serotype 3"))