본문 바로가기
R

(R) Fill,Color columns based on a variable/aes(x=,y=,fill=)/geom_col(position="dodge")

by jangpiano 2020. 7. 24.
반응형

Colored Columns


The way to make filled columns which consist of three variables.


<Make new variables >

[R] - (R) ifelse()

diamonds {ggplot2} R Documentation


Format

A data frame with 53940 rows and 10 variables:


price

price in US dollars (\$326–\$18,823)

carat

weight of the diamond (0.2–5.01)

x

length in mm (0–10.74)

y

width in mm (0–58.9)

z

depth in mm (0–31.8)


You should decide three variables assigned to X-axis, Y-axis, Fill.

I will make a new variable "caratt" which consists of three component ("light","middle","heavy") using ifelse(). 


> library(ggplot2) 

> library(dplyr)


<caratt <- carat>


> summary(diamonds$carat)


   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 

 0.2000  0.4000  0.7000  0.7979  1.0400  5.0100 



> diamonds$caratt<-ifelse(diamonds$carat<=0.4,"light",ifelse(diamonds$carat<=1.04,"middle","heavy"))


> table(diamonds$caratt)

    heavy  light middle

 

  13379  14391  26170


<seize <- size <-x,y,z>


> diamonds$size<-diamonds$x*diamonds$y*diamonds$z

> summary(diamonds$size)

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 

   0.00   65.14  114.81  129.85  170.84 3840.60

> diamonds$ssize<-ifelse(diamonds$size>=114,"big","small")

> table(diamonds$ssize)


  big small 

 27434 26506 


<Make a summary of columns >

To make columns, you need to make a summary first.


> ggplot(data=caratt_price_ssize,aes(x=caratt,y=mean_price,fill=ssize))+geom_col()




> ggplot(data=caratt_price_ssize,aes(x=caratt,y=mean_price,fill=ssize))+geom_col(position="dodge")








 





반응형