saya akan mencoba menjeleskan mengenai fungsi dan grafik pada bahasa pemrograman R Anda dapat dengan mudah menggambar grafik menggunakan library “mosaicCalc”. Ingat bahwa pertama-tama kita perlu mengimpor perpustakaan dari mosaicCalc ke setiap chunk-nya dengan menambahkan pernyataan berikut: library(mosaicCalc) Pastikan untuk selalu menempatkan pernyataan di atas pada pernyataan pertama dari chunk
Kita dapat membuat fungsi dengan menggunakan keyword makeFun. Namun, keyword ini memerlukan library ‘mosaicCalc’
Berikut contoh membuat fungsi menggunakan makeFun:
library(mosaicCalc)
## Loading required package: mosaic
## Registered S3 method overwritten by 'mosaic':
## method from
## fortify.SpatialPolygonsDataFrame ggplot2
##
## The 'mosaic' package masks several functions from core packages in order to add
## additional features. The original behavior of these functions should not be affected by this.
##
## Attaching package: 'mosaic'
## The following objects are masked from 'package:dplyr':
##
## count, do, tally
## The following object is masked from 'package:Matrix':
##
## mean
## The following object is masked from 'package:ggplot2':
##
## stat
## The following objects are masked from 'package:stats':
##
## binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
## quantile, sd, t.test, var
## The following objects are masked from 'package:base':
##
## max, mean, min, prod, range, sample, sum
## Loading required package: mosaicCore
##
## Attaching package: 'mosaicCore'
## The following objects are masked from 'package:dplyr':
##
## count, tally
##
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
##
## D
k <- makeFun(4*x^2 + 10*x ~ x)
k(2)
## [1] 36
Pada fungsi, bagian ‘~ x’ merupakan variable x yang dapat kita masukkan dan ini juga sangat diperlukan dalam membuat fungsi menggunakan makeFun.
Berikut Beberapa Plot yang ada di RStudio dengan library ‘mosaicCalc’
Slice plot adalah plot yang menampilkan data dalam bentuk grafik garis contoh dari slice plot:
library(mosaicCalc)
slice_plot(3 * x - 2 ~ x, domain(x = range(0, 10)))
Berikut contoh sederhana jika ingin menggunakan variabel
library(mosaicCalc)
m = -3
b = -2
slice_plot(m * x + b ~ x, domain(x = range(0, 10)))
Berikut contoh fungsinya:
g <- makeFun(4*x^2 - 2*x + 2 ~ x)
slice_plot(g(x) ~ x , domain(x = range(-4, 4)))
dari kalkulus berikut contohnya:
d <- makeFun(2*x^2 - 5*x + 2 ~ x)
library(mosaicCalc)
slice_plot(sqrt(abs(d(x))) ~ x, domain(x = range(-5,5)))
kumpulan daya atau dokumen yang berisi satu atau lebih catatan (record). Setiap kelompok record ini tadi disebut sebagai dataset dan memiliki peran untuk menyimpan informasi
Membuat variabel dan mengambil data dari web
Housing = read.csv("http://www.mosaic-web.org/go/datasets/Income-Housing.csv")
Housing
## Income IncomePercentile CrimeProblem AbandonedBuildings IncompleteBathroom
## 1 3914 5 39.6 12.6 2.6
## 2 10817 15 32.4 10.0 3.3
## 3 21097 30 26.7 7.1 2.3
## 4 34548 50 23.9 4.1 2.1
## 5 51941 70 21.4 2.3 2.4
## 6 72079 90 19.9 1.2 2.0
## NoCentralHeat ExposedWires AirConditioning TwoBathrooms MotorVehicle
## 1 32.3 5.5 52.3 13.9 57.3
## 2 34.7 5.0 55.4 16.9 82.1
## 3 28.1 2.4 61.7 24.8 91.7
## 4 21.4 2.1 69.8 39.6 97.0
## 5 14.9 1.4 73.9 51.2 98.0
## 6 9.6 1.0 76.7 73.2 99.0
## TwoVehicles ClothesWasher ClothesDryer Dishwasher Telephone
## 1 17.3 57.8 37.5 16.5 68.7
## 2 34.3 61.4 38.0 16.0 79.7
## 3 56.4 78.6 62.0 25.8 90.8
## 4 75.3 84.4 75.2 41.6 96.5
## 5 86.6 92.8 88.9 58.2 98.3
## 6 92.9 97.1 95.6 79.7 99.5
## DoctorVisitsUnder7 DoctorVisits7To18 NoDoctorVisitUnder7 NoDoctorVisit7To18
## 1 3.6 2.6 13.7 31.2
## 2 3.7 2.6 14.9 32.0
## 3 3.6 2.1 13.8 31.4
## 4 4.0 2.3 10.4 27.3
## 5 4.0 2.5 7.7 23.9
## 6 4.7 3.1 5.3 17.5
Array 2 Dimension ([r, c])
Housing[1, 1]
## [1] 3914
Array 2 Dimension ([r:rmax, c:cmax])
subset1 <- Housing[2:4, 1:2]
subset1
## Income IncomePercentile
## 2 10817 15
## 3 21097 30
## 4 34548 50
Mengambil data pada datatset file Housing
Housing$NoCentralHeat
## [1] 32.3 34.7 28.1 21.4 14.9 9.6
ScatterPlot adalah = Plot/titik yang menampilkan data dalam grafik
Perbedaan dari Slice Plot dan ScatterPlot adalah jika pada slice plot merupakan garis, maka ScatterPlot merupakan titik pada grafik
Berikut Contoh-Contoh dari ScatterPlot :
library(mosaicCalc)
gf_point(CrimeProblem ~ Income, data = Housing)
library(mosaicCalc)
gf_point(
IncomePercentile ~ Income, data=Housing ) %>%
slice_plot(
50 - Income/1000 ~ Income, color = "red")
library(mosaicCalc)
gf_point(
CrimeProblem ~ Income, data = Housing) %>%
slice_plot(
40 - Income / 2000 ~ Income, color = "green") %>%
gf_lims(
x = range(0,100000),
y=range(0,50))
library(mosaicCalc)
gf_point(
CrimeProblem ~ Income, data=Housing) %>%
gf_labs(x= "Income Bracket ($US per household)/year",
y = "Fraction of Households",
main = "Crime Problem") %>%
gf_lims(x = range(0,100000), y = range(0,50))
library(mosaicCalc)
s = read.csv(
"http://www.mosaic-web.org/go/datasets/stan-data.csv")
gf_point(temp ~ time, data=s)
library(mosaicCalc)
h = read.csv(
"http://www.mosaic-web.org/go/datasets/hawaii.csv")
gf_point(water ~ time, data=h)
library(mosaicCalc)
Utilities <- read.csv(
"http://www.mosaic-web.org/go/datasets/utilities.csv")
gf_point(
temp ~ month, data=Utilities) %>%
gf_labs(x = "Month (Jan=1, Dec=12)",
y = "Temperature (F)",
main = "Ave. Monthly Temp.")
Interactive Plot adalah Plot/grafik dalam bentuk 3D. Hal ini dapat memudahkan kita dalam menghitung grafik 3 dimensi
Berikut Contoh dari Interactive Plot
interactive_plot(
sin(2*pi*t/50)*exp(-.1*x) ~ t & x,
domain(t = 0:40, x = 0:20))
Contour Plot adalah plot/grafik yang menampilkan warna, untuk melihat luas grafik
Berikut Contoh dari Contour Plot
library(mosaicCalc)
g <- makeFun(
sin(2*pi*t/10)*exp(-.2*x) ~ t & x)
contour_plot(
g(t, x) ~ t + x,
domain(t=0:20, x=0:10))
library(mosaicCalc)
contour_plot(
sqrt( (v-3)^2 + (w-4)^2 ) ~ v & w,
domain(v=0:6, w=0:6))
library(mosaicCalc)
contour_plot(
sqrt( (v-3)^3 + (w-4)^3 ) ~ v & w,
domain(v=0:36, w=0:36))
## Warning in sqrt((v - 3)^3 + (w - 4)^3): NaNs produced
## Warning in sqrt((v - 3)^3 + (w - 4)^3): NaNs produced