본문 바로가기
R

(R) scatter dot plot/ a variable with continuous colour/by continuous size/ the way to adjust legend of the filling plot

by jangpiano 2020. 8. 27.
반응형

<dot plots with continuous 3 variables and 4 variables>


As you can easily understand, the variables x and y on x and y-axis are well represented even if they are continuous. 

Then what about adding another continuous variable to the dot plot?

You can make a dot plot with smoothly colored dots that express the new continuous variable. 

Also, You can make a dot plot with smoothly getting bigger dots that express another continuous variable. 


For this post, I will use the 'USJudgeRatings' data in R.

> ?USJudgeRatings




<Set a continuous variable and distinguish dots by filling continuous colors>

For this part, I will use the variable 'PREP' which consists of continuous elements for filling the dots. 


> ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,colour=PREP))+geom_point()


You can adjust the shape of dots in the plot by using geom_point(shape, size) 

Also, you can adjust the colour to fill the continuous variable by setting scale_fill_gradient(low="",high="")

> ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,fill=PREP,group=PREP))

+geom_point(shape=21,size=3)

+scale_fill_gradient(low="black",high="white")

 > ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,fill=PREP)

+geom_point(shape=21,size=3)

+scale_fill_gradient(low="white",high="black")

 

 



> ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,fill=PREP,group=PREP))

+geom_point(shape=21,size=3)

+scale_fill_gradient(low="white",high="black")

 > ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,fill=PREP,group=PREP))

+geom_point(shape=21,size=3)

+scale_fill_gradient(low="pink",high="black")

 

 



You can concretely set the legend beside the plot. 

legend - <5,6,7,8,9>

The variable 'PREP' consists of elements between 5 to 9.5. So the basic legend has breaks on integers 5,6,7,8,9.


> ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,fill=PREP,group=PREP))+geom_point(shape=21,size=3)+scale_fill_gradient(low="white",high="red")


legend - <5, 6.5, 8>

The variable 'PREP' consists of elements between 5 to 9.5 and I set the breaks of the legend as 2.5 different for each other. 

> ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,fill=PREP))+geom_point(shape=21,size=3)+scale_fill_gradient(low="white",high="red",breaks=seq(5,10,by=1.5))


legend - <to be expressed as discrete>


The variable 'PREP' consists of elements between 5 to 9.5. So the basic legend has breaks on integers 5,6,7,8,9. The only difference with the first example is that this expresses the colors by separating the continous color bar.


> ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,fill=PREP))+geom_point(shape=21,size=3)+scale_fill_gradient(low="white",high="red",guide=guide_legend())



> ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,fill=PREP))+geom_point(shape=21,size=3)+scale_fill_gradient(low="white",high="red",breaks=seq(5,10,by=1.5),guide=guide_legend())


<set a continuous variable and distinguish dots by setting different sizes>

dots in the dot plot is originally set as having sizes between 1 and 6. 


> ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,size=PREP))+geom_point()


> ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,size=PREP))+geom_point()+scale_size_continuous(range=c(0,4))



scale_size_area() is needed to set the size of dots proportional to numeric elements. 


> ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,size=PREP))+geom_point()+scale_size_area()


Setting alpha is the usual task when making a dot plot because of overlapped dots. 


> ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,size=PREP))+geom_point(alpha=0.6)+scale_size_area()


<set two continuous variables and distinguish dots by setting different sizes and colors>

> ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,size=DECI,fill=PREP))+geom_point(shape=21)+scale_size_area()+scale_fill_gradient(low="white",high="blueviolet")

> ggplot(data=USJudgeRatings,aes(x=DILG,y=FAMI,size=DECI,fill=PREP))+geom_point(shape=21)+scale_size_continuous(range=c(0,6))+scale_fill_gradient(low="white",high="blueviolet")




반응형