【问题标题】:Legend does not fit well ggplot density图例不适合ggplot密度
【发布时间】:2018-08-19 00:46:45
【问题描述】:

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

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

剧情是:

【问题讨论】:

    标签: r ggplot2 label legend


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-20
      • 2018-11-26
      • 2015-10-16
      • 2017-11-05
      • 2019-04-16
      • 2015-12-26
      • 2016-01-22
      • 2015-11-08
      相关资源
      最近更新 更多