1. Membuat scatter plot dengan fungsi qplot()

library(ggplot2)

f <- function() {
  a <- 1:20
  b <- log(a)
  qplot(a, b, colour=I("seagreen"), xlab="a", ylab="log(a)")
}
f()

2. Membuat scatter plot dengan fungsi ggplot() dari data bangkitan

# membangkitkan data dari distribusi normal
x <- rnorm(50)
y <- x + rnorm(50, mean=0, sd=1)
data <- as.data.frame(cbind(x, y))

# membuat scatter plot antara x dan y
ggplot(data, aes(x=x, y=y)) +
  geom_point(size=2) +
  ggtitle("Scatterplot of X and Y") +
  theme(axis.text = element_text(size=12),
        axis.title = element_text(size=10),
        plot.title = element_text(size=15, face="bold"))

3. Membuat histogram harga berlian dari data diamonds

ggplot(data=diamonds, aes(x=price)) +
  geom_histogram(alpha=0.9, fill="orange", color="black") +
  ggtitle ("Histogram of price") + labs(y="frequency") +
  theme(axis.text = element_text(size=8),
        axis.title = element_text(size=10),
        plot.title = element_text(size=12, face="italic"))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

4. Membuat dotplot

qplot(mpg, data = mtcars, geom = "dotplot", ylim = c(0,5), facets= am~cyl, main="Dotplot")
## Bin width defaults to 1/30 of the range of the data. Pick better value with `binwidth`.

5. Membuat boxplot

ggplot(data=diamonds, aes(x=as.factor(cut), y=price)) +
  geom_point(aes(color=factor(cut)), alpha=0.1, position="jitter") +
  geom_boxplot(outlier.size=0.9, alpha=0.1) + labs(x="cut") +
  theme(axis.text = element_text(size=8),
        axis.title = element_text(size=10),
        plot.title = element_text(size=12),
        legend.position = "none") +
  ggtitle ("Price by cut")

6. Membuat density plot

ggplot(diamonds, aes(x=price, fill=cut)) +
  geom_density(adjust = 1/2, alpha = 0.5, color=NA) +
  theme(legend.position="bottom")

7. Membuat bar chart

diamonds1 <- diamonds[diamonds$color=="D" | diamonds$color=="G"| diamonds$color=="J",]

ggplot(diamonds1, aes(x=cut))+
  geom_bar(aes(fill=color)) + labs(y="frequency")+
  theme(axis.text.x=element_text(angle=45, hjust=1),
        axis.text = element_text(size=8),
        axis.title = element_text(size=10),
        plot.title = element_text(size=12))+
  scale_fill_discrete(labels=c("Best (D)", "Fair (G)", "Worst (J)"))