R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

install.packages("plotly")
## Warning: package 'plotly' is in use and will not be installed
library(plotly)

Including Plots

You can also embed plots, for example:

data("EuStockMarkets")

#Data frame
EuStockMarkets_df<-data.frame(
  time=as.numeric(time(EuStockMarkets)),
  value=as.numeric(EuStockMarkets)

)
EuStockMarkets_df$year <- floor(EuStockMarkets_df$time)
EuStockMarkets_df$month <- round((EuStockMarkets_df$time - EuStockMarkets_df$year) * 12) + 1
head(EuStockMarkets_df)
##       time   value year month
## 1 1991.496 1628.75 1991     7
## 2 1991.500 1613.63 1991     7
## 3 1991.504 1606.51 1991     7
## 4 1991.508 1621.04 1991     7
## 5 1991.512 1618.16 1991     7
## 6 1991.515 1610.61 1991     7
#Data ini berupa data indeks saham di 4 negara eropa yaitu Inggris (FTSE), Germany(DAX), Perancis (CAC), dan Switzerland (SMI) dalam bentuk harga akhir setiap hari dari 1991 sampai 1998. 
#

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

#Stacked Bar Plot
EuStockMarkets_yearly <- aggregate(value ~ year, data = EuStockMarkets_df, sum)
fig1 <- plot_ly(EuStockMarkets_yearly, x = ~year, y = ~value, type = 'bar',
                marker = list(color = 'rgba(255, 100, 102, 0.7)')) %>%
   layout(title = 'Stacked Bar Plot of EU Stock Market (Yearly)',
         xaxis = list(title = 'Year'),
         yaxis = list(title = 'Total Stock Value'),
         barmode = 'stack')
fig1
#Dalam Bar Plot ini. kami bisa melihat bahwa setiap tahun mengalami peningkatan selama periode 8 Tahun kecuali pada Tahun 1998 dimana total nilai dari 4 indeks menurun dari Tahun 1997.
#Stacked Box Plot
fig2 <- plot_ly(EuStockMarkets_df, x = ~factor(year), y = ~value, type = 'box',
                boxpoints = 'all', jitter = 0.0, pointpos = 0,
                marker = list(color = 'rgba(55, 128, 191, 0.7)')) %>%
  layout(title = 'Stacked Boxplot of EU Stock Market (Yearly)',
         xaxis = list(title = 'Year'),
         yaxis = list(title = 'Stock Prices'))
fig2
#Dari boxplot bisa dilihat range relatif konstant dari 1991 sampai 1996. Untuk 2 Tahun terakhir range meningkat secara signifikan.
#Line Chart
Data_Line <- data.frame(EuStockMarkets)
Data_Line$time <- c(1:1860)  
fig3 <- plot_ly(Data_Line, x = ~time, y = ~DAX, type = 'scatter', mode = 'lines',
                line = list(color = 'rgb(150, 0, 0)', width = 2), name ="DAX") %>%
  layout(title = 'Line Chart of EU Stock Market',
         xaxis = list(title = 'Time'),
         yaxis = list(title = 'Stock Prices'))
fig3 <- fig3 %>% add_trace(y = ~SMI, line = list(color = 'rgb(0, 200, 0)', width = 2), name="SMI")
fig3 <- fig3 %>% add_trace(y = ~CAC, line = list(color = 'rgb(0, 0, 200)', width = 2), name="CAC")
fig3 <- fig3 %>% add_trace(y = ~FTSE, line = list(color = 'rgb(100, 150, 200)', width = 2), name="FTSE")
fig3
#Scatter Plot
Data_Line <- data.frame(EuStockMarkets)
Data_Line$time <- c(1:1860)  
fig4 <- plot_ly(Data_Line, x = ~time, y = ~DAX, type = 'scatter', mode = 'markers',
                line = list(color = 'rgb(150, 0, 0)', width = 0.05), name ="DAX") %>%
  layout(title = 'Line Chart of EU Stock Market',
         xaxis = list(title = 'Time'),
         yaxis = list(title = 'Stock Prices'))
fig4 <- fig4 %>% add_trace(y = ~SMI, line = list(color = 'rgb(0, 200, 0)', width = 0.05), name="SMI")
fig4 <- fig4 %>% add_trace(y = ~CAC, line = list(color = 'rgb(0, 0, 200)', width = 0.05), name="CAC")
fig4 <- fig4 %>% add_trace(y = ~FTSE, line = list(color = 'rgb(100, 150, 200)', width = 0.05), name="FTSE")
fig4
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
#Dari grafik garis ini kami bisa melihat tren semua indeks saham meningkat adalah pertumbuhan yang konstant selama periode.
library(plotly)
library(datasets)

data("DNase")
head(DNase)
##   Run       conc density
## 1   1 0.04882812   0.017
## 2   1 0.04882812   0.018
## 3   1 0.19531250   0.121
## 4   1 0.19531250   0.124
## 5   1 0.39062500   0.206
## 6   1 0.39062500   0.215
#DNase adalah data konsentrasi protein pada rat serum.#

#Staked Barplot
bar_data <- as.data.frame(table(DNase$conc, DNase$Run))
colnames(bar_data) <- c("conc", "Run", "Freq")

stacked_barplot <- plot_ly(bar_data, x = ~conc, y = ~Freq, color = ~Run, colors = RColorBrewer::brewer.pal(8, "Set3"), type = 'bar') %>%
  layout(barmode = 'stack', title = "Stacked Barplot DNase", xaxis = list(title = 'Conc'), yaxis = list(title = 'Frequency'))

#Staked Boxplot
stacked_boxplot <- plot_ly(DNase, x = ~Run, y = ~density, color = ~Run, colors = RColorBrewer::brewer.pal(8, "Set3"), type = 'box') %>%
  layout(title = "Stacked Boxplot DNase", xaxis = list(title = 'Run'), yaxis = list(title = 'Density'))

#Line Chart
line_chart <- plot_ly(DNase, x = ~conc, y = ~density, color = ~Run, type = 'scatter', mode = 'lines') %>%
  layout(title = "Line Chart DNase", xaxis = list(title = 'Conc'), yaxis = list(title = 'Density'))

#Scatterplot
scatterplot <- plot_ly(DNase, x = ~conc, y = ~density, color = ~Run, type = 'scatter', mode = 'markers') %>%
  layout(title = "Scatterplot DNase", xaxis = list(titl = 'Conc'), yaxis = list(title = 'Density'))
stacked_barplot
#Dari Barplot dan boxplot kami bisa melihat bahwa semua pengulangan uji coba menghasilkan nilai yang sama sehingga bisa disimpulkan bahwa hasil dari uji coba ini bisa direplikasi.#
stacked_boxplot
line_chart
scatterplot
#Dari scatter plot DNase ini, kami bisa menginferensi bahwa jika density dari sebuah sample protein lebih tinggi, maka konsentrasi portein(conc) juga akan lebih juga.
#Dataset Islands#
#1. Memuat data frame
data("islands")
Islands = data.frame(Island = names(islands), Area = as.numeric(islands))
View(Islands)

#A. Membuat Stacked Barplot
Islands$Category = cut(Islands$Area,
                       breaks = c(0,50,500,5000,10000,Inf),
                       labels = c("Sangat Kecil", "Kecil", " Sedang", "Besar", "Sangat Besar"))
Stacked_Barplot = plot_ly(Islands, 
                          x = ~Category, 
                          y = ~Area, 
                          type = 'bar', 
                          color = ~Island, 
                          text = ~paste("Pulau:", Island, "<br>Area:", Area, "ribuan mil²")) %>%
  layout(barmode = "stack",
         title = "Stacked Bar Plot Luas Pulau Berdasarkan Kategori Ukuran",
         xaxis = list(title = "Kategori Ukuran Pulau"),
         yaxis = list(title = "Area", type = "log"))
Stacked_Barplot
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
#Dari Barplot adalah beberapa inferensi yang kami bisa dapat. Pulau kecil cenderung memiliki populasi yang lebih besar sedangkan jumlah pulau yang besar lebih kecil. Luas ukuran dari setiap kategori juga berbeda jauh sehingga ukuran harus dikonversi ke bentuk log agar skala bisa ditampilkan.#
#B. Membuat Stacked Boxplot
Islands$Category = cut(Islands$Area,
                             breaks = c(0,50,500,5000,10000,Inf),
                            labels = c("Sangat Kecil", "Kecil", " Sedang", "Besar", "Sangat Besar"))
Stacked_Boxplot = plot_ly(Islands, 
                          x = ~Category, 
                          y = ~Area, 
                          type = 'box', 
                          color = ~Category, 
                          text = ~paste("Pulau:", Island, "<br>Area:", Area, "ribuan mil²")) %>%
  layout(barmode = "stack",
         title = "Stacked Box Plot Luas Pulau Berdasarkan Kategori Ukuran",
         xaxis = list(title = "Kategori Ukuran Pulau"),
         yaxis = list(title = "Area", type = "log"))
Stacked_Boxplot
#Karena populasi yang lebih besar, range dari pulau-pulau lebih besar akibat variasi yang semakin besar dibanding dengan pulau besar dan sedang yang memiliki jumlah observasi yang dibawah 4.#