Assignment #9: Visualization in R – Base Graphics, Lattice, and ggplot2
Github Link: https://github.com/rayhankhan-svg/r-programming-assignments
R Code:
data <- read.csv(file.choose(), stringsAsFactors = FALSE)
head(data)
str(data)
data$rownames <- as.numeric(data$rownames)
data$education <- as.numeric(data$education)
plot(data$rownames, data$education,
main = "Base: Education by Observation",
xlab = "Observation",
ylab = "Education",
col = "blue")
hist(data$education,
main = "Base: Distribution of Education",
xlab = "Education",
col = "lightgreen")
library(lattice)
xyplot(education ~ rownames | gender,
data = data,
main = "Lattice: Education by Observation and Gender")
bwplot(education ~ job,
data = data,
main = "Lattice: Education by Job Type")
library(ggplot2)
ggplot(data, aes(x = rownames, y = education, color = gender)) +
geom_point() +
geom_smooth(method = "lm") +
labs(title = "ggplot2: Education by Observation with Trend")
ggplot(data, aes(x = education)) +
geom_histogram(binwidth = 1, fill = "blue") +
facet_wrap(~ job) +
labs(title = "ggplot2: Education Distribution by Job Type")
Output:
> data <- read.csv(file.choose(), stringsAsFactors = FALSE)
>
> head(data)
rownames job education gender minority
1 1 manage 15 male no
2 2 admin 16 male no
3 3 admin 12 female no
4 4 admin 8 female no
5 5 admin 15 male no
6 6 admin 15 male no
> str(data)
'data.frame': 474 obs. of 5 variables:
$ rownames : int 1 2 3 4 5 6 7 8 9 10 ...
$ job : chr "manage" "admin" "admin" "admin" ...
$ education: int 15 16 12 8 15 15 15 12 15 12 ...
$ gender : chr "male" "male" "female" "female" ...
$ minority : chr "no" "no" "no" "no" ...
>
> data$rownames <- as.numeric(data$rownames)
> data$education <- as.numeric(data$education)
>
> plot(data$rownames, data$education,
+ main = "Base: Education by Observation",
+ xlab = "Observation",
+ ylab = "Education",
+ col = "blue")
>
> hist(data$education,
+ main = "Base: Distribution of Education",
+ xlab = "Education",
+ col = "lightgreen")
>
> library(lattice)
>
> xyplot(education ~ rownames | gender,
+ data = data,
+ main = "Lattice: Education by Observation and Gender")
>
> bwplot(education ~ job,
+ data = data,
+ main = "Lattice: Education by Job Type")
>
> library(ggplot2)
>
> ggplot(data, aes(x = rownames, y = education, color = gender)) +
+ geom_point() +
+ geom_smooth(method = "lm") +
+ labs(title = "ggplot2: Education by Observation with Trend")
`geom_smooth()` using formula = 'y ~ x'
>
> ggplot(data, aes(x = education)) +
+ geom_histogram(binwidth = 1, fill = "blue") +
+ facet_wrap(~ job) +
+ labs(title = "ggplot2: Education Distribution by Job Type")
>






Comments
Post a Comment