본문 바로가기
R

(R)How to add several line graphs to a graph / rearrange the order of colour label/ How to add colours to each graph

by jangpiano 2020. 10. 9.
반응형
> x<-seq(-5,5,0.01)
> df_1<-dt(x,1)
> df_5<-dt(x,5)
> df_20<-dt(x,20)
> T<-data.frame(x,df_1,df_5,df_20)

<several line graphs in a graph> 

> ggplot(data=T,aes(x))+geom_line(aes(y=df_1))+geom_line(aes(y=df_5))+geom_line(aes(y=df_20))+geom_line(aes(y=dnorm(x,0,1)))

<Color each line graphs> 

> ggplot(data=T,aes(x))+geom_line(aes(y=df_1),col="red")+geom_line(aes(y=df_5),col="blue")+geom_line(aes(y=df_20),col="yellow")+geom_line(aes(y=dnorm(x,0,1)))

<Change the title, the name of labs of the graph> 

ggplot(data=T,aes(x))+geom_line(aes(y=df_1,col="df_1"))+geom_line(aes(y=df_5,col="df_5"))+geom_line(aes(y=df_20,col="df_20"))+geom_line(aes(y=dnorm(x,0,1),col="norm"))+labs(y="density",title="Tdistribution")+scale_colour_manual(values=c("df_1"="red","df_5"="blue","df_20"="yellow","norm"="black"))

<Add colors to each geom_line() with legend>


You should recognize that col="" be included in aes() which is the difference with the example above. 


> ggplot(data=T,aes(x))+geom_line(aes(y=df_1,col="df_1"))+geom_line(aes(y=df_5,col="df_5"))+geom_line(aes(y=df_20,col="df_20"))+geom_line(aes(y=dnorm(x,0,1),col="norm"))+labs(y="density",title="Tdistribution")+scale_colour_manual(values=c("df_1"="red","df_5"="blue","df_20"="yellow","norm"="black"))



<Change the order of legend> 


> ggplot(data=T,aes(x))+geom_line(aes(y=df_1,col="df_1"))+geom_line(aes(y=df_5,col="df_5"))+geom_line(aes(y=df_20,col="df_20"))+geom_line(aes(y=dnorm(x,0,1),col="norm"))+labs(y="density",title="Tdistribution")+scale_colour_manual(values=c("df_1"="red","df_5"="blue","df_20"="yellow","norm"="black"),breaks=c("df_1","df_5","df_20","norm"))


반응형

'R' 카테고리의 다른 글

(R) Two sample Bootstrap Method  (0) 2020.10.23
(R) One-sample Bootstrap Method  (0) 2020.10.17
(R) Central limit theorem  (0) 2020.09.27
(R) Normal approximation to Binomial  (0) 2020.09.27
(R) geom_violin() - a density graph  (0) 2020.09.09