传奇不适合ggplot密度

时间:2018-03-10 15:33:31

标签: r ggplot2 label legend

我正在做一个密度图,我想添加一个图例,但与符号重叠。代码如下:

dfGamma = data.frame(a = rgamma(100,shape = 7.1,rate= 0.0055),
                     b = rgamma(100, shape = 10,rate= 0.0055),
                     c = rgamma(100, shape = 7.1,rate= 0.0055))

dfGamma = stack(dfGamma)
p <- ggplot(dfGamma, aes(x = values)) +
  stat_density(aes(group = ind, colour = ind),position="identity",geom="line",size=1)+
  ggtitle("Gamma distribution")+theme(legend.position="right")+
  scale_color_manual(labels = c(expression(paste(alpha,"=7.1 ",beta,"=0.0055")),
                                expression(paste(alpha,"= 10 ",beta,"=0.0055")),
                                expression(paste(alpha,"=7.1 ",beta,"=0.0055"))),
                     values = c('red', 'blue',"green"))

p

情节为:enter image description here

1 个答案:

答案 0 :(得分:0)

guides选项,guide_legend就是您所需要的。您可以阅读更多相关信息in the ggplot reference。这有帮助吗?

p <- ggplot(dfGamma, aes(x = values)) +
  stat_density(aes(group = ind, colour = ind),position="identity",geom="line",size=1)+
  ggtitle("Gamma distribution")+
  theme(legend.position="right") +
  scale_color_manual(labels = c(expression(paste(alpha, "=7.1 ", beta, "=0.0055")),
                                expression(paste(alpha,"= 10 ",beta,"=0.0055")),
                                expression(paste(alpha,"=7.1 ",beta,"=0.0055"))),
                     values = c('red', 'blue',"green")) +
  guides(colour = guide_legend(label.position = "bottom"))

p

enter image description here