Colored Columns
The way to make filled columns which consist of three variables.
<Make new variables >
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")
'R' 카테고리의 다른 글
(R)Make a new variable/ mutate() (0) | 2020.07.27 |
---|---|
(R) [dplyr] Rearrange data in order / arrange() / arrange(desc()) (0) | 2020.07.26 |
(R) ifelse() (0) | 2020.07.23 |
(R) make a summary - group_by(),summarise() (0) | 2020.07.22 |
R reorder() -reorder columns in ascending order, descending order when setting aes() (0) | 2020.07.21 |