Monday, January 23, 2023

Pie Charts Pada Programming-R

 


Pie Charts merupakan tampilan grafis melingkar dari data.  Fungsi pie() untuk menggambar diagram lingkaran:

Contoh :

# Create a vector of pies
x <- c(10,20,30,40)

# Display the pie chart
pie(x)



# Create a vector of pies
x <- c(10,20,30,40)

# Display the pie chart and start the first pie at 90 degrees
pie(x, init.angle = 90)

# Create a vector of pies
x <- c(10,20,30,40)

# Create a vector of labels
mylabel <- c("Apples""Bananas""Cherries""Dates")

# Display the pie chart with labels
pie(x, label = mylabel, main = "Fruits")




# Create a vector of colors
colors <- c("blue""yellow""green""black")

# Display the pie chart with colors
pie(x, label = mylabel, main = "Fruits", col = colors)


# Create a vector of labels
mylabel <- c("Apples""Bananas""Cherries""Dates")

# Create a vector of colors
colors <- c("blue""yellow""green""black")

# Display the pie chart with colors
pie(x, label = mylabel, main = "Pie Chart", col = colors)

# Display the explanation box
legend("bottomright", mylabel, fill = colors)


posisi :

 bottomrightbottombottomleftlefttoplefttoptoprightrightcenter


Bar Charts Pada Programming-R

 


Bar Charts menggunakan batang persegi panjang untuk memvisualisasikan data. Bagan batang dapat ditampilkan secara horizontal atau vertikal. Tinggi atau panjang batang sebanding dengan nilai yang diwakilinya.

Format : barplot()

Contoh :

# x-axis values
x <- c("A""B""C""D")

# y-axis values
y <- c(2468)

barplot(y, names.arg = x)


x <- c("A""B""C""D")
y <- c(2468)

barplot(y, names.arg = x, col = "red")


x <- c("A""B""C""D")
y <- c(2468)

barplot(y, names.arg = x, density = 10)



x <- c("A""B""C""D")
y <- c(2468)

barplot(y, names.arg = x, width = c(1,2,3,4))


x <- c("A""B""C""D")
y <- c(2468)

barplot(y, names.arg = x, horiz = TRUE)



Plot Charts Pada Programming-R

 


Fungsi plot() merupakan fungsi umum untuk membuat grafik R. 

Format : {plot(x, y, type="p")}

Keterangan :

  • x dan y: titik koordinat plot Berupa variabel dengan panjang atau jumlah observasi yang sama.
  • type: jenis grafik yang hendak dibuat. Nilai yang dapat dimasukkan antara lain:
    • p : membuat plot titik atau scatterplot. Nilai ini merupakan default pada fungsi plot().
    • l : membuat plot garis.
    • b : membuat plot titik yang terhubung dengan garis.
    • o : membuat plot titik yang ditimpa oleh garis.
    • h : membuat plot garis vertikal dari titik ke garis y=0.
    • s : membuat fungsi tangga.
    • n : tidak membuat grafik plot sama sekali, kecuali plot dari axis. Dapat digunakan untuk mengatur tampilan suatu plot utama yang diikuti oleh sekelompok plot tambahan.


Point

plot(13)

plot(c(18), c(310))


Multiple Points

plot(c(12345), c(378912))

x <- c(12345)
y <- c(378912)

plot(x, y)

Sequences of Points

plot(1:10)


Draw a Line

plot(1:10type="l")


Plot Labels

plot(1:10, main="My Graph", xlab="The x-axis", ylab="The y axis")


Graph Appearance

plot(1:10, col="red")


Size

plot(1:10, cex=2)


Point Shape

plot(1:10, pch=25, cex=2)



parameter pch




Friday, January 20, 2023

Histogram Charts Pada Programming-R

 


Histogram merupakan jenis diagram batang yang menunjukkan frekuensi jumlah nilai yang dibandingkan dengan sekumpulan rentang nilai. Histogram digunakan untuk distribusi, sedangkan diagram batang digunakan untuk membandingkan entitas yang berbeda. Dalam histogram, setiap bilah mewakili ketinggian jumlah nilai yang ada dalam rentang yang diberikan. Untuk membuat histogram, R menyediakan fungsi hist(), yang mengambil vektor sebagai masukan dan menggunakan lebih banyak parameter untuk menambahkan lebih banyak fungsi

format :  hist(v,main,xlab,ylab,xlim,ylim,breaks,col,border) 

keterangan :

  • v : vektor yang berisi nilai numerik
  • main : menunjukkan judul grafik
  • col : digunakan untuk mengatur warna batang
  • border : digunakan untuk mengatur warna batas setiap batang
  • xlab : digunakan untuk menggambarkan sumbu x
  • ylan : digunakan untuk menggambarkan sumbu y
  • xlim : digunakan untuk menentukan rentang nilai pada sumbu x
  • ylim : digunakan untuk menentukan rentang nilai pada sumbu y
  • breaks : digunakan untuk menyebutkan lebar setiap batang

Contoh :

# Creating data for the graph.  

v <-  c(12,24,16,38,21,13,55,17,39,10,60)  

# Giving a name to the chart file.  

png(file = "histogram_chart.png")  

# Creating the histogram.  

hist(v,xlab = "Weight",ylab="Frequency",col = "green",border = "red")  

# Saving the file.  

dev.off()  



Menggunakan parameter : xlim & ylim

# Creating data for the graph.  

v <-  c(12,24,16,38,21,13,55,17,39,10,60)  

# Giving a name to the chart file.  

png(file = "histogram_chart_lim.png")  

# Creating the histogram.  

hist(v,xlab = "Weight",ylab="Frequency",col = "green",border = "red",xlim = c(0,40), ylim = c(0,3), breaks = 5)  

# Saving the file.  

dev.off() 


Mendapatkan nilai return hist() 

# Creating data for the graph.  

v <-  c(12,24,16,38,21,13,55,17,39,10,60)  

# Giving a name to the chart file.  

png(file = "histogram_chart_lim.png")  

# Creating the histogram.  

m<-hist(v)  


Menggunakan nilai pengembalian histogram untuk label menggunakan text()

# Creating data for the graph.  

v <-  c(12,24,16,38,21,13,55,17,39,10,60,120,40,70,90)  

# Giving a name to the chart file.  

png(file = "histogram_return.png")  

# Creating the histogram.  

m<-hist(v,xlab = "Weight",ylab="Frequency",col = "darkmagenta",border = "pink", breaks = 5)  

#Setting labels  

text(m$mids,m$counts,labels=m$counts, adj=c(0.5, -0.5))  

# Saving the file.  

dev.off()    


Menggunakan lebar yang tidak sama

# Creating data for the graph.  

v <-  c(12,24,16,38,21,13,55,17,39,10,60,120,40,70,90)  

# Giving a name to the chart file.  

png(file = "histogram_non_uniform.png")  

# Creating the histogram.  

hist(v,xlab = "Weight",ylab="Frequency",xlim=c(50,100),col = "darkmagenta",border = "pink", breaks=c(10,55,60,70,75,80,100,120))  

# Saving the file.  

dev.off()

Boxplot Charts Pada Programming-R

 


Boxplot merupakan ukuran seberapa baik data didistribusikan di seluruh kumpulan data. Ini membagi kumpulan data menjadi tiga kuartil. Grafik ini mewakili minimum, maksimum, rata-rata, kuartil pertama, dan kuartil ketiga dalam kumpulan data. Boxplot juga berguna dalam membandingkan distribusi data dalam kumpulan data dengan menggambar boxplot untuk masing-masingnya.

R menyediakan fungsi boxplot() untuk membuat boxplot.

Format : boxplot(x, data, notch, varwidth, names, main)

keterangan :

  • x : Vektor atau rumus
  • data : Data frame
  • notch : Nilai logis yang ditetapkan sebagai benar untuk menggambar notch 
  • varwidth : Merupakan nilai logis yang ditetapkan sebagai benar untuk menggambar lebar kotak sama dengan ukuran sampel
  • names : Kelompok label yang akan dicetak di bawah setiap petak kotak
  • main : Digunakan untuk memberi judul pada grafik

1. Boxplot 

# Giving a name to the chart file.  

png(file = "boxplot.png")  

# Plotting the chart.  

boxplot(mpg ~ cyl, data = mtcars, xlab = "Quantity of Cylinders",  

        ylab = "Miles Per Gallon", main = "R Boxplot Example")  

# Save the file.  

dev.off()



2. Boxplot using notch  

# Giving a name to our chart.  

png(file = "boxplot_using_notch.png")  

# Plotting the chart.  

boxplot(mpg ~ cyl, data = mtcars,   

        xlab = "Quantity of Cylinders",  

        ylab = "Miles Per Gallon",   

        main = "Boxplot Example",  

        notch = TRUE,   

        varwidth = TRUE,   

        ccol = c("green","yellow","red"),  

        names = c("High","Medium","Low")  

)  

# Saving the file.  

dev.off() 


3. Violin Plots

# Loading the vioplot package   

library(vioplot)  

# Giving a name to our chart.  

png(file = "vioplot.png")  

#Creating data for vioplot function  

x1 <- mtcars$mpg[mtcars$cyl==4]  

x2 <- mtcars$mpg[mtcars$cyl==6]  

x3 <- mtcars$mpg[mtcars$cyl==8]  

#Creating vioplot function  

vioplot(x1, x2, x3, names=c("4 cyl", "6 cyl", "8 cyl"),  

        col="green")  

#Setting title   

title("Violin plot example")  

# Saving the file.  

dev.off()  


4. Bagplot- 2-Dimensional Boxplot Extension

# Loading aplpack package  

library(aplpack)  

# Giving a name to our chart.  

png(file = "bagplot.png")  

#Creating bagplot function  

attach(mtcars)  

bagplot(wt,mpg, xlab="Car Weight", ylab="Miles Per Gallon",  

   main="2D Boxplot Extension")  

# Saving the file.  

dev.off() 


Membuat CRUD Pada Programming-R





Untuk melakukan CRUD pada mysql, bisa menggunakan function : fetch() & dbSendQuery() ,

CRUD terdiri dari : Select, Insert, Updare & Delete


1. Penggunakan Command Select

 #Loading RMySQL  
library("RMySQL")  
# Creating a connection Object to MySQL database.  
mysql_connect = dbConnect(MySQL(), user = 'root', password = '', dbname = 'employee',  
   host = 'localhost')  
# selecting the specific record from employee_info table.  
record = dbSendQuery(mysql_connect, "select * from employee_info where dept='IT'")  
# Fetching all the records(with n = -1) and storing it as a data frame.  
data_frame = fetch(record, n = -1)  
print(data_frame) 



2. Penggunakan Command Insert

#Loading RMySQL   
library("RMySQL")  
# Creating a connection Object to MySQL database.  
mysql_connect = dbConnect(MySQL(), user = 'root', password = '', dbname = 'employee',  
   host = 'localhost')    
# Inserting record into employee_info table.  
dbSendQuery(mysql_connect, "insert into employee_info values(9,'Preeti',1025,'8/25/2013','Operations'



3. Penggunakan Command Update

# Loading RMySQL
library("RMySQL")  
# Creating a connection Object to MySQL database.  
mysql_connect = dbConnect(MySQL(), user = 'root', password = '', dbname = 'employee',  
   host = 'localhost')    
# Updating the record in employee_info table.  
dbSendQuery(mysql_connect, "update employee_info set dept='IT' where id=9") 




4. Penggunakan Command Delete

#Loading RMySQL
library("RMySQL")    
# Creating a connection Object to MySQL database.  
mysql_connect = dbConnect(MySQL(), user = 'root', password = '', dbname = 'employee',  
   host = 'localhost')  
# Deleting the specific record from employee_info table.  
dbSendQuery(mysql_connect, "delete from employee_info where id=8") 


5. Penggunaan Command Drop

#Loading RMySQL   

library("RMySQL")  

# Creating a connection Object to MySQL database.  

mysql_connect = dbConnect(MySQL(), user = 'root', password = '', dbname = 'employee',  

   host = 'localhost')    

# Dropping the specific table from the employee database.  

dbSendQuery(mysql_connect, "drop table if exists emp")  


Connect Database Mysql Pada Programming-R

 


Paket RMySQL merupakan salah satu paket bawaan bahasa R yang paling penting. Paket ini menyediakan konektivitas asli antara database R dan MySql. untuk bekerja dengan database MySql, pertama-tama harus menginstal paket RMySQL.

1. Install paket

install.packages("RMySQL")


2.  Buat database : employee dan table : employee_info 


3. Connect Database Mysql

#Loading RMySQL package into R  

library("RMySQL")  

# Creating a connection Object to MySQL database.  

mysql_connect = dbConnect(MySQL(), user = 'root', password = '', dbname = 'employee',  

   host = 'localhost')  

# Listing the tables available in this database.  

 dbListTables(mysql_connect) 



4. Create table

#Loading RMySQL package into R  

library("RMySQL")  

# Creating a connection Object to MySQL database.  

mysql_connect = dbConnect(MySQL(), user = 'root', password = '', dbname = 'employee',  

   host = 'localhost')  

#Creating data frame to create a table   

emp.data<- data.frame(    

name = c("Raman","Rafia","Himanshu","jasmine","Yash"),    

salary = c(623.3,915.2,611.0,729.0,843.25),     

start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11","2015-03-27")),  

dept = c("Operations","IT","HR","IT","Finance"),    

stringsAsFactors = FALSE    

)    

# All the rows of emp.data are taken inot MySql.  

dbWriteTable(mysql_connect, "emp", emp.data[, ], overwrite = TRUE)  



Memunculkan Simbol & Emoji Pada OS Mac

  Memunculkan Simbol & Emoji  1. Buka aplikasi Pages / Notes pada Macbook. 2. Klik pada Menubar Edit --> Pilih Emoji and Symbols a...