0% found this document useful (0 votes)
90 views

Density, Boxplot, Violinplot, Scatterplot

This document contains code to create various plots using the ggplot2 package in R including: 1) A density plot of city mileage grouped by number of cylinders using the mpg dataset. 2) A boxplot and violin plot of city mileage grouped by class of vehicle from the mpg dataset. 3) Several scatter plots using built-in datasets like iris and midwest, including adding colors and sizes based on variables. 4) A scatter plot with a loess smoothing line added and zoomed into a specific area range and population range using the midwest data.

Uploaded by

HabibJazuli
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Density, Boxplot, Violinplot, Scatterplot

This document contains code to create various plots using the ggplot2 package in R including: 1) A density plot of city mileage grouped by number of cylinders using the mpg dataset. 2) A boxplot and violin plot of city mileage grouped by class of vehicle from the mpg dataset. 3) Several scatter plots using built-in datasets like iris and midwest, including adding colors and sizes based on variables. 4) A scatter plot with a loess smoothing line added and zoomed into a specific area range and population range using the midwest data.

Uploaded by

HabibJazuli
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

library(ggplot2)

data(mpg)
mpg
theme_set(theme_classic())

#density plot
g <- ggplot(mpg,aes(cty))
g+geom_density(aes(fill=factor(cyl)),alpha = 0.8) + labs(title="Density Plot", subtitle= "City Milleage
Grouped by Number of Cylinders", caption = "Source:mpg", x = "City Mileage", fill = "#Cylinders")

#Boxplot
g <- ggplot(mpg, aes(class,cty))
g+geom_boxplot(varwidth=T, fill= "violet")+
labs(title="Boxplot", subtitle= "City Mileage Grouped by Class of Vehicle",
caption="Source:mpg",
x="Class of Vehicle",
y = "City Mileage",
fill="City Mileage")
# Violin Plot
g <- ggplot(mpg, aes(class,cty))
g+geom_violin(fill = "violet")+
labs(title="Violin Plot", subtitle= "City Mileage Grouped by Class of Vehicle",
caption="Source:mpg",
x="Class of Vehicle",
y = "City Mileage")

# Scatterplot
data <- read.csv("D:\\eda16418.csv", header = TRUE)
data

plot(x= data$Advertis,y = data$Sales)

# Scatterplot-RColorBrewer
library(RColorBrewer)
par(mfrow=c(2,2))
plot(x=data$Advertis, y=da
plot(x=data$Advertis, y=da
plot(x=data$Advertis, y=da
plot(x=data$Advertis, y=da

# Scatter Plot dengan ggplo


library(ggplot2)
theme_set(theme_grey())
g<- ggplot(data, aes(x= Adv
geom_point(aes(col=AdAg
g
theme_set(theme_light())
h <- ggplot(data,aes(x=Advertis, y = Sales)) +
geom_point(aes(col=AdAgency), size =3)
h
# Data Iris
data(iris)
iris
par(mfrow=c(1,1))
plot(x=iris$Petal.Length) # Simple Scatter Plot
plot(x=iris$Petal.Length, y=iris$Species) #Multivariate Scatter Plot
plot(iris, col=brewer.pal(3,"Set2"))
# Data Midwest
data("midwest")
midwest
theme_set(theme_bw())
g <- ggplot(midwest,aes(x=area,y=poptotal)) +
geom_point(aes(col=state, size=popdensity)) # col=state (warna dibedakan berdasarkan state)
g #size = popdensity (ukuran dibedakan berdasarkan poopdensity)
# Kemudian kita potong datanya (zoom) dengan menggunakan geom_smooth (di zoom bukan dipotong)
g + geom_smooth(method="loess",se=F) +
xlim(c(0,0.1)) +
ylim(c(0,500000)) +
labs(subtitle = "Area VS Population", y="Population", x="Area", title="Scatterplot",
caption="Source:midwest")
# xlim =... menunjukkan x dibatasi dari 0 sampai dengan 0.1
# ylim =... menunjukkan y dibatasi dari 0 sampai 500000

You might also like