Visualization using ggplot2

  1. Import the birthwt.csv and healthexp.csv
library(readr)
birthwt <- read_csv("birthwt.csv")
expd <- read_csv("healthexp.csv")
  1. Install ggplot2 using install.packages(“ggplot2”) function (this is done only once). Load the package by using the library(ggplot2) function.
library(ggplot2)
ggplot(data = birthwt,aes(x=low))+geom_bar()

ggplot(data = birthwt,aes(x=age))+geom_bar()

ggplot(data = birthwt,aes(x=age))+geom_bar(fill="aquamarine4")

ggplot(data = birthwt,aes(x=low,fill=factor(race)))+geom_bar(position = "fill")

ggplot(data = birthwt,aes(x=age,fill=factor(low)))+geom_bar(position = "dodge")

a <- ggplot(expd,aes(x=RACE))
a + geom_bar() + theme_bw() + labs(x="Race of Insured", y="Frequency", title="Distribution of Race of Insured")

b <- ggplot(expd,aes(x=REGION,fill=RACE))
b + geom_bar() + theme_grey() + labs(x="Region", y="Frequency", title="Region by Race of Insured")

c <- ggplot(expd,aes(x=RACE,fill=EDUC))
c + facet_wrap(~REGION) + geom_bar() + theme_grey() + labs(x="Region", y="Frequency", title="Race by Education by Region")

d <- ggplot(expd,aes(x=AGE,y=EXPENDOP))
d + geom_point() + theme_light() +  labs(x="Age", y="Expenditures for outpatient visits", title="Outpatient Expenditures")

e <- ggplot(expd,aes(x=AGE))
e + geom_histogram(binwidth=7) + theme_light() +  labs(x="Insured Age (bandwith=7)", y="Insured Count", title="Distribution of Age")

f <- ggplot(expd,aes(x=AGE,fill=EDUC))
f + geom_histogram(binwidth=7) + theme_light() +  labs(x="Insured Age (bandwith=7", y="Insured Count",
title="Distribution of Age by Education")

g <- ggplot(expd,aes(x=REGION,y=AGE))
g + geom_boxplot() + theme_bw() +  labs(x="Region", y="Age",
title="Distribution of Age by Region")

h <- ggplot(expd,aes(x=REGION,y=AGE,color=EDUC))
h + geom_boxplot() + coord_flip() + theme_bw() +  labs(x="Region", y="Age", title="Distribution of Age by Region")