You are on page 1of 28

ANDI FAHIRA ALSA 06211840000004 AED KELAS B

Membuat violin plot pada R

> library(ggplot2)

Learn more about the underlying theory at https://ggplot2-book.org/

Warning message:

package ‘ggplot2’ was built under R version 3.6.3

> theme_set(theme_bw())

> # plot

> plot <- ggplot(mpg, aes(class, cty))

> plot + geom_violin(fill="blue") +

+ labs(title="Violin plot",

+ caption="Produced by Gary Hutson",

+ x="Class of Vehicle",y="Mileage") +

+ geom_jitter(height = 0, width = 0.1, colour="black")

Membuat density plot pada R

> library(ggplot2)

> theme_set(theme_classic())

> plot <- ggplot(mpg, aes(cty))


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> plot + geom_density(aes(fill=factor(cyl)), alpha=0.3) +

+ labs(title="Density plot",

+ caption="Produced by Gary Hutson",

+ x="City Mileage",

+ fill="Number of Cylinders")

Mengubah jenis chart dengan menambahkan parameter posisi ke lapisan geometri untuk kepadatan

> plot + geom_density(aes(fill=factor(cyl)), alpha=0.3, position="stack")

Mengubah argumen posisi

> plot + geom_density(aes(fill=factor(cyl)), alpha=0.3, position="fill")


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

Cara menggunakan warna heksadesimal

> #mengubah warna

> library(ggplot2)

> theme_set(theme_classic())

> #Specify custom palette

> plot <- ggplot(mpg, aes(cty))

> plot + geom_density(aes(fill=factor(cyl)), alpha=0.5) +

+ labs(title="Density plot",

+ caption="Produced by Gary Hutson",

+ x="City Mileage",

+ fill="Number of Cylinders") +

+ #I force the scale to conform to certain hexadecimal colour codes

+ scale_fill_manual(values=c("#dbdfe0", "#0faeb1",

+ "#0954b7", "#5c93b8"))

> #scale_fill_brewer(palette="Dark2")
ANDI FAHIRA ALSA 06211840000004 AED KELAS B

Pengenalan ggplot

> p <- ggplot(mtcars, aes(factor(cyl), mpg))

> p + geom_violin()

> # Orientation follows the discrete axis

> ggplot(mtcars, aes(mpg, factor(cyl))) +

+ geom_violin()
ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> p <- ggplot(mtcars, aes(factor(cyl), mpg))

> p + geom_violin()

> # Scale maximum width proportional to sample size:

> p + geom_violin(scale = "count")

> p + geom_violin(scale = "width")


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> p + geom_violin(trim = FALSE)

> # Use a smaller bandwidth for closer density fit (default is 1).

> p + geom_violin(adjust = .5)

> # Add aesthetic mappings


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> # Note that violins are automatically dodged when any aesthetic is

> # a factor

> p + geom_violin(aes(fill = cyl))

> p + geom_violin(aes(fill = factor(cyl)))

> p + geom_violin(aes(fill = factor(vs)))


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> p + geom_violin(aes(fill = factor(am)))

> # Set aesthetics to fixed value

> p + geom_violin(fill = "grey80", colour = "#3366FF")


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> # Show quartiles

> p + geom_violin(draw_quantiles = c(0.25, 0.5, 0.75))

> # Scales vs. coordinate transforms -------

> if (require("ggplot2movies")) {

+ # Scale transformations occur before the density statistics are computed.

+ # Coordinate transformations occur afterwards. Observe the effect on the

+ # number of outliers.

+ m <- ggplot(movies, aes(y = votes, x = rating, group = cut_width(rating, 0.5)))

+ m + geom_violin()
ANDI FAHIRA ALSA 06211840000004 AED KELAS B

+ m + geom_violin() + scale_y_log10()

+ m + geom_violin() + coord_trans(y = "log10")

+ m + geom_violin() + scale_y_log10() + coord_trans(y = "log10")

+ # Violin plots with continuous x:

+ # Use the group aesthetic to group observations in violins

+ ggplot(movies, aes(year, budget)) + geom_violin()

+ ggplot(movies, aes(year, budget)) +

+ geom_violin(aes(group = cut_width(year, 10)), scale = "width")}

Pengenalan jitter

Jittered points

> p <- ggplot(mpg, aes(cyl, hwy))

> p + geom_point()
ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> p + geom_jitter()

> # Add aesthetic mappings

> p + geom_jitter(aes(colour = class))

> # Use smaller width/height to emphasise categories


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> ggplot(mpg, aes(cyl, hwy)) + geom_jitter()

> ggplot(mpg, aes(cyl, hwy)) + geom_jitter(width = 0.25)

> # Use larger width/height to completely smooth away discreteness

> ggplot(mpg, aes(cty, hwy)) + geom_jitter()


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> ggplot(mpg, aes(cty, hwy)) + geom_jitter(width = 0.5, height = 0.5)

Pendalaman jitter

Scatterplot Jittering

standar scatter plot data


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> set.seed(1)

> x <- sample(1:10, 200, TRUE)

> y <- 3 * x + rnorm(200, 0, 5)

> plot(y ~ x, pch = 15)

plot(y ~ jitter(x, 1), pch = 15)

> plot(y ~ jitter(x, 2), pch = 15)


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> x2 <- sample(1:10, 500, TRUE)

> y2 <- sample(1:5, 500, TRUE)

> plot(y2 ~ x2, pch = 15)

> plot(y2 ~ jitter(x2), pch = 15)


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> plot(jitter(y2) ~ x2, pch = 15)

> plot(jitter(y2) ~ jitter(x2), pch = 15)


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> plot(jitter(y2, 2) ~ jitter(x2, 2), pch = 15)


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

Tutorial 3 graphics

Histogram pada R

> hist(SL)

> hist(SL[POP==1])

> hist(FINAREA[POP==5])
ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> hist(SL[POP=="U.S.A”])

> hist(SL[POP==1], xlab = “ body length for population 1”)

> hist(SL[POP == 1], xlab = “ body length for population 1”, main=”Size”)

> hist(SL[POP == 1], xlab = “ body length for population 1”, main=”Size”, col=2)

> hist(SL[POP == 1], xlab = “ body length for population 1”, main=”Size”, col=2,
density = 5)

Boxplot
> boxplot(SL[POP==1])
ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> boxplot(SL[POP==1], SL[POP== 2], SL[POP==5])

> boxplot(SL~POP)

> boxplot(SL [POP==1], SL[POP==2], SL[POP==5], col=4, horizontal = TRUE)


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

Scatter plot
> #scatter plot

> plot(FINAREA, TAREA)


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> plot(FINAREA~TAREA)

> abline(lm(FINAREA~TAREA))
ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> abline(lm(FINAREA~TAREA), lwd = 10, lty = 2, col=3)

> plot(jitter(RAYNO), POP)


ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> plot(jitter(RAYNO), jitter(POP))

Multiple figures on one plot


> split.screen(figs=c(2, 3))

[1] 1 2 3 4 5 6

> screen(1)

> hist(SL[POP==1])

> screen(2)

> hist(SL[POP==2])

> screen(3)

> hist(SL[POP==5])

> screen(4)

> boxplot(SL[POP== 1])

> screen(5)

> boxplot(SL[POP==2])

> screen(6)

> boxplot(SL[POP==5])
ANDI FAHIRA ALSA 06211840000004 AED KELAS B

> plot(FINAREA, TAREA, cex.axis=0.5)

> par(mfrow=c(1,3))

> hist(SL[POP==1])

> hist(SL[POP==2])

> hist(SL[POP==5])
ANDI FAHIRA ALSA 06211840000004 AED KELAS B

Exercise 1
> split.screen(fig=c(1,3));

[1] 1 2 3

> screen(1);

> boxplot(data1,xlab="population",ylab="range",col=5);

> screen(2);

> boxplot(data1, xlab="population", ylab="range", main="these are the results of my


first function and the graph heading was made a little smaller in order to fit on the
page", cex.main=0.6, col=6);

> screen(3);

> boxplot(data1,xlab="population",ylab="range",col=7);

> close.screen(all=T);
ANDI FAHIRA ALSA 06211840000004 AED KELAS B

Exercise 2
> #exercise 2

> par(mfrow=c(2,3));

> boxplot(SL[POP==1],xlab="population",ylab="range",col=4);

> boxplot(SL[POP==2],xlab="population",ylab="range",col=5);

> boxplot(SL[POP==5],xlab="population",ylab="range",col=6);

> plot(TAREA[POP==1],FINAREA[POP==1],col=4);

> abline(lm(FINAREA[POP==1]~TAREA[POP==1]),col=4)

> plot(TAREA[POP==2],FINAREA[POP==2],col=5);

> abline(lm(FINAREA[POP==2]~TAREA[POP==2]),col=5)

> plot(TAREA[POP==5],FINAREA[POP==5],col=6);

> abline(lm(FINAREA[POP==5]~TAREA[POP==5]),col=6)

> close.screen(all=T);
ANDI FAHIRA ALSA 06211840000004 AED KELAS B

You might also like