ggplot does not show legend in geom_histogram

后端 未结 2 2050
梦毁少年i
梦毁少年i 2020-12-11 21:41

I have this code

ggplot()
+ geom_histogram(aes(x=V1, y=(..count..)/sum(..count..)), fill=\"red\", alpha=.4, colour=\"red\", data=coding, stat = \"bin\", binw         


        
2条回答
  •  无人及你
    2020-12-11 21:52

    If you don't want to put the data in one data.frame, you can do this:

    set.seed(42)
    coding <- data.frame(V1=rnorm(1000))
    lncrna <- data.frame(V1=rlnorm(1000))
    
    
    library(ggplot2)
    ggplot() + 
      geom_histogram(aes(x=V1, y=(..count..)/sum(..count..), fill="r", colour="r"), alpha=.4, data=coding, stat = "bin") +
      geom_histogram(aes(x=V1,y=(..count..)/sum(..count..), fill="b", colour="b"), alpha=.4, data=lncrna, stat = "bin") +
      scale_colour_manual(name="group", values=c("r" = "red", "b"="blue"), labels=c("b"="blue values", "r"="red values")) +
      scale_fill_manual(name="group", values=c("r" = "red", "b"="blue"), labels=c("b"="blue values", "r"="red values"))
    

    enter image description here

提交回复
热议问题