Its a markdown repository
We will create different data visualizations using the ggplot
package using the inbuilt dataset in "R"
called mtcars
This is set up on (formerly Big Data University) by IBM Developer Skills Network
lab
- Click on the + symbol on the top left and choose
'R Script'
from the menu to open a new R edit window in RStudio OR HoldCTRL
+SHIFT
+ALT
+N
- Read and view the first 5 rows of the Data using the following:
library(datasets)
data(mtcars)
head(mtcars, 5)
-
Type this ?mtcars to get information about the variables. This will print the information at the bottom right panel, on the Help tab
-
Copy and paste the following code to load the ggplot package and create a scatterplot of
disp
andmpg.
ggplot(aes(x=disp,y=mpg,), data=mtcars)+ geom_point()
- Use the following code to add a title.
ggplot(aes(x=disp,y=mpg,), data=mtcars)+ geom_point()+ ggtitle("displacement vs miles per gallon")
ggplot(aes(x=disp,y=mpg,), data=mtcars)+ geom_point()+ ggtitle("displacement vs miles per gallon") + labs(x = "Displacement", y = "Miles per Gallon")
- Use the following to create a boxplot of the the distribution of mpg for the individual Engine types
vs
Engine(0 = V-shaped, 1 = straight)
To do this you have to make vs a string or factor.
mtcars$vs <- as.factor(mtcars$vs)
ggplot(aes(x=vs, y=mpg), data = mtcars) + geom_boxplot()
- Add color to the boxplots to help differentiate:
ggplot(aes(x=vs, y=mpg, fill = vs), data = mtcars) +
geom_boxplot(alpha=0.3) +
theme(legend.position="none")
- Finally, let us create the histogram of weight
wt
.
ggplot(aes(x=wt), data=mtcars) + geom_histogram(binwidth=0.5)
This concludes this lab, I hope that you had fun!
Ekene Emmanuel Ajemba