Assignment
Assignment
2. What geom would you use to draw a line chart? A boxplot? A histogram? An
area chart?
ggplot(data = mtcars) +
geom_line(mapping = aes(x = wt, y = mpg)) +
geom_point(mapping = aes(x = wt, y = mpg))
3. Map a continuous variable to color, size & shape. How do these aesthetics behave
differently for categorical verses continuous variables?
ggplot(data=mtcars)+geom_point(mapping=aes(x=wt, y=mpg,shape=model))
ggplot(data=mtcars)+geom_point(mapping=aes(x=disp, y=mpg,shape=factor(cyl)
))
Categorical Data:
ggplot(data=mtcars)+geom_point(mapping=aes(x=cyl, y=carb,color=disp))
ggplot(data=mtcars)+geom_point(mapping=aes(x=cyl, y=carb,size=disp))
4. What happen if same variable would be mapped on multiple aesthetics?
Simply both layers are applied.
ggplot(data=mtcars) + geom_point(mapping = aes(x=wt, y=mpg, color=cyl, si
ze=cyl))
Shows the confidence interval around the line. (the grey area)
Scatter Plot
ggplot(mtcars,aes(mpg,wt)) + geom_point()
Histogram
ggplot(mtcars , aes(mpg)) +
geom_histogram(binwidth = 2) +
labs(title = "Histogram")
Bar Chart
ggplot(mtcars,aes(carb)) +
geom_bar(fill="blue") +
theme_bw() +
coord_flip() +
labs(title="Bar Chart") +
theme_classic()
Stack Bar Chart
ggplot(mtcars,aes(cyl,fill = factor(gear))) +
geom_bar() +
labs(title = "stacked Bar Chart", X="cyl" , y= "gear")
Box Plot
ggplot(mtcars, aes(factor(cyl),mpg)) +
geom_boxplot(fill="blue") +
labs(title = "Box PLot" , X="MPG" , Y="Wt")
Area Chart