Search what you want

Monday, March 23, 2015

Menampilkan data dalam bentuk visual (grafik) pada R-Studio [Data Mining]

Visualisasi dalam Grafik
a.  Pie Chart
 ># Define cars vector with 5 values
 >cars <- c(1, 3, 6, 4, 9)
 ># Define some colors ideal for black & white print
 >colors <- c("white","grey70","grey90","grey50","black")
 ># Calculate the percentage for each day, rounded to one 
 ># decimal place
 >car_labels <- round(cars/sum(cars) * 100, 1)
 ># Concatenate a '%' char after each value
 >car_labels <- paste(car_labels, "%", sep="")
 ># Create a pie chart with defined heading and custom colors
 ># and labels
 >pie(cars, main="Cars", col=colors, labels=car_labels,cex=0.8)
 ># Create a legend at the right 
 >legend(1.5, 0.5, c("Mon","Tue","Wed","Thu","Fri"), cex=0.8, fill=colors)

b. Line Chart
 ># Define 2 vectors
 >cars <- c(1, 3, 6, 4, 9)
 >trucks <- c(2, 5, 4, 5, 12)
 ># Calculate range from 0 to max value of cars and trucks
 >g_range <- range(0, cars, trucks)
 ># Graph autos using y axis that ranges from 0 to max 
 ># value in cars or trucks vector. Turn off axes and 
 ># annotations (axis labels) so we can specify them ourself
 >plot(cars, type="o", col="blue", ylim=g_range, axes=FALSE, ann=FALSE)
 ># Make x axis using Mon-Fri labels
 >axis(1, at=1:5, lab=c("Mon","Tue","Wed","Thu","Fri"))
 ># Make y axis with horizontal labels that display ticks at 
 ># every 4 marks. 4*0:g_range[2] is equivalent to c(0,4,8,12).
 >axis(2, las=1, at=4*0:g_range[2])
 ># Create box around plot
 >box()
 ># Graph trucks with red dashed line and square points
 >lines(trucks, type="o", pch=22, lty=2, col="red")
 ># Create a title with a red, bold/italic font
 >title(main="Autos", col.main="red", font.main=4)
 ># Label the x and y axes with dark green text
 >title(xlab="Days", col.lab=rgb(0,0.5,0))
 >title(ylab="Total", col.lab=rgb(0,0.5,0))
 ># Create a legend at (1, g_range[2]) that is slightly smaller 
 ># (cex) and uses the same line colors and points used by 
 ># the actual plots 
 >legend(1, g_range[2], c("cars","trucks"), cex=0.8, col=c("blue","red"), pch=21:22, lty=1:2);

c. Histogram
 ># Read values from tab-delimited autos.dat 
 >autos_data <- read.table("C:/R/autos.dat", header=T, sep="\t")
 ># Graph autos with adjacent bars using rainbow colors
 >barplot(as.matrix(autos_data), main="Autos", ylab= "Total",beside=TRUE, col=rainbow(5))
 ># Place the legend at the top-left corner with no frame 
 ># using rainbow colors
 >legend("topleft", c("Mon","Tue","Wed","Thu","Fri"), cex=0.6, bty="n", fill=rainbow(5));

No comments:

Post a Comment