본문 바로가기
R

(R)Adjust line graphs' linetype, colour, shapes of points/change the variable of x-axis to factor type

by jangpiano 2020. 8. 21.
반응형

> View(ChickWeight)


> table(ChickWeight$Diet)


  1   2   3   4 

220 120 120 118 


> table(ChickWeight$Time)


 0  2  4  6  8 10 12 14 16 18 20 21 

50 50 49 49 49 49 49 48 47 47 46 45 


> CW<-ChickWeight%>%filter(Chick==1|Chick==21|Chick==31|Chick==41)%>%filter(Time==0|Time==6|Time==12|Time==18|Time==21)

> CW

   weight Time Chick Diet

1      42    0     1    1

2      64    6     1    1

3     106   12     1    1

4     171   18     1    1

5     205   21     1    1

6      40    0    21    2

7      86    6    21    2

8     217   12    21    2

9     307   18    21    2

10    331   21    21    2

11     42    0    31    3

12     73    6    31    3

13    123   12    31    3

14    204   18    31    3

15    256   21    31    3

16     42    0    41    4

17     85    6    41    4

18    155   12    41    4

19    184   18    41    4

20    204   21    41    4


<ggplot(data,aes(x,y,colour))+geom_line()>


> ggplot(data=CW,aes(x=Time,y=weight,colour=Diet))+geom_line()


<adjust the colors, type, size of lines>


> ggplot(data=CW,aes(x=Time,y=weight,colour=Diet))+geom_line()+scale_colour_brewer(palette="Set1")



> ggplot(data=CW,aes(x=Time,y=weight,colour=Diet))+geom_line(linetype="dashed")+scale_colour_brewer(palette="Set1")


> ggplot(data=CW,aes(x=Time,y=weight,colour=Diet))+geom_line(linetype="dashed",size=1)+scale_colour_brewer(palette="Set1")



<ggplot(data,aes(x,y,linetype))+geom_line()>      -distinguish groups by linetypes. 


> ggplot(data=CW,aes(x=Time,y=weight,linetype=Diet))+geom_line()


> ggplot(data=CW,aes(x=Time,y=weight,linetype=Diet))+geom_line()+geom_point(shape=22,colour="darkred",fill="pink",size=2.5)


> ggplot(data=CW,aes(x=Time,y=weight,linetype=Diet))+geom_line()+geom_point(fill="white",shape=21,size=4)



<Change the variable x to factor type when having three variables >


> ggplot(data=CW,aes(x=factor(Time),y=weight,colour=Diet, group=Diet))+geom_line()


<ggplot(data,aes(x,y,colour))+geom_line()+geom_point()>

> ggplot(data=CW,aes(x=factor(Time),y=weight,colour=Diet,group=Diet))+geom_line()+geom_point(size=3)


<ggplot(data,aes(x,y,shape))+geom_line()+geom_point()>

> ggplot(data=CW,aes(x=factor(Time),y=weight,shape=Diet,group=Diet))+geom_line()+geom_point()


> ggplot(data=CW,aes(x=factor(Time),y=weight,shape=Diet,group=Diet))+geom_line()+geom_point(size=2.5)


< Available Shapes of points >



< ggplot(data,aes(x,y,fill)+geom_line()+geom_point(shape) >

> ggplot(data=CW,aes(x=factor(Time),y=weight,fill=Diet,group=Diet))+geom_line()+geom_point(shape=22)

> ggplot(data=CW,aes(x=factor(Time),y=weight,fill=Diet,group=Diet))+geom_line()+geom_point(shape=22,size=2.5)

> ggplot(data=CW,aes(x=factor(Time),y=weight,fill=Diet,group=Diet))+geom_line()+geom_point(shape=21,size=2.5)

> ggplot(data=CW,aes(x=factor(Time),y=weight,fill=Diet,group=Diet))+geom_line()+geom_point(shape=24,size=2.5)

> ggplot(data=CW,aes(x=Time,y=weight,fill=Diet))+geom_line(position=position_dodge(0.5))+geom_point(shape=21,size=4,position=position_dodge(0.5))+scale_fill_manual(values=c("black","white","lightblue","blue"))


< ggplot(data,aes(x,y,colour)+geom_line()+geom_point(shape) >

> ggplot(data=CW,aes(x=factor(Time),y=weight,colour=Diet,group=Diet))+geom_line()+geom_point(shape=3,size=2.5)

> ggplot(data=CW,aes(x=factor(Time),y=weight,colour=Diet,group=Diet))+geom_line()+geom_point(shape=24,size=2.5)

< ggplot(data,aes(x,y,linetype)+geom_line()+geom_point(shape)>

> ggplot(data=CW,aes(x=factor(Time),y=weight,linetype=Diet,group=Diet))+geom_line()+geom_point(shape=24,size=2.5)


> ggplot(data=CW,aes(x=Time,y=weight,colour=Diet))+geom_line(linetype="dashed",size=1)+geom_point(shape=21,size=2.5,fill="white")


< adjust the position of lines and points - geom_line(position=position_dodge())+geom_point(position=position_dodge()) >


 > ggplot(data=CW,aes(x=factor(Time),y=weight,shape=Diet,group=Diet))

+geom_line()+geom_point(size=2.5)

 > ggplot(data=CW,aes(x=factor(Time),y=weight,shape=Diet,group=Diet))

+geom_line(position=position_dodge(0.5))

+geom_point(position=position_dodge(0.5),size=2.5)

 

The dots at factor(Time) are overlapped so they are hard to see. 

 


You can see 4 dots in different shapes by adjusting their positions. 


If you decide to put position_dodge in geom_poit() to separate the dots, you should also put position_dodge in geom_line() with the same number.


반응형