ggplot2: missing legend and how to add?

前端 未结 2 1873
梦谈多话
梦谈多话 2021-01-21 04:31

I want to plot a bar and line chart from a dataframe. Code below,

library(\"ggplot2\")

numb <- c(1,2,3,4,5,6,7,8,9)
mydist <- c(53.846154,15.384615,15.384         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-21 04:33

    Without changing much of the original code, you only need to put your fill into aes mapping, then add the scale to set the colour values and labels:

    ggplot(data=df,aes(x=numb)) + 
      geom_bar(stat="identity", aes(y=mydist, fill="green"), colour="green") +
      geom_line(aes(y=basedist,group=1, colour="base distribution")) +  
      geom_point(aes(y=basedist), colour="red") +
      ggtitle("My Chart") +
      labs(x="numb", y="percentage") +
      scale_x_discrete(limits=c("1","2","3","4","5","6","7","8","9")) +
      scale_y_continuous(breaks=seq(0,100,10)) +
      scale_fill_manual(values = "green", labels = "my distribution") +
      theme(axis.title.x = element_text(size=10, colour ="#666666")) +
      theme(axis.title.y = element_text(size=10, color="#666666")) +
      theme(plot.title = element_text(size=16, face="bold", hjust=0, color="#666666")) +
      theme(axis.text = element_text(size=12)) +
      theme(legend.title = element_text(colour="white", size = 16, face='bold'))
    

提交回复
热议问题