7m QC Tools Household

Teknik Sampling dan Survei

awokwowk


1. Check Sheet

1.1 Dataset

1.2 Tabel Frekuensi

# Count frequency
library(dplyr)
library(DT)

Household_summary <- Household %>%
  count(Pengeluaran, sort = TRUE) %>%
  rename(Frequency = n)

# Display summary table
datatable(
  Household_summary,
  options = list(
    scrollCollapse = TRUE,
    searching = FALSE,   # Remove search box
    paging = FALSE       # Remove pagination
  ),
  rownames = FALSE,
  caption = htmltools::tags$caption(
    style = 'caption-side: top; text-align: left; 
             font-size: 18px; font-weight: bold;',
    'Frekuensi Pengeluaran Rumah Tangga'
  ),
  class = 'stripe hover compact'
)

1.3 Visualisasi

library(plotly)

# Interactive bar chart using plotly
plot_ly(Household_summary,
        x = ~Frequency,
        y = ~reorder(Pengeluaran, Frequency),
        type = 'bar',
        orientation = 'h',
        marker = list(
          color = ~Frequency,
          colorscale = 'Viridis',  # Can also try: 'Bluered', 'Cividis', 'YlOrRd'
          showscale = TRUE
        )
) %>%
  layout(
    title = list(text = "Frekuensi Pengeluaran Rumah", font = list(size = 18)),
    xaxis = list(title = "Frequency"),
    yaxis = list(title = "Pengeluaran"),
    margin = list(l = 120)
  )

2. Histogram

1. Dataset

# Ensure all required packages are installed
packages <- c("dplyr", "stringi", "lubridate", "DT")
new_packages <- packages[!(packages %in% installed.packages()[, "Package"])]
if(length(new_packages)) install.packages(new_packages)

# Load libraries
library(dplyr)
library(stringi)
library(lubridate)
library(DT)

## Create a complex dummy health dataset for data transformation
set.seed(42)
n <- 31

# Simulate location and health condition
Jenis <- sample(c("Nasi Goreng", "Soto", "Sayur Asem", "Spageti", "Ayam Goreng"), n, replace = TRUE)

# Simulate cooking times (in minutes) for each food type
waktu_masak <- c("Nasi Goreng" = 15, "Soto" = 60, "Sayur Asem" = 45, "Spageti" = 20, "Ayam Goreng" = 30)
Waktu_Masak <- sapply(Jenis, function(x) waktu_masak[x])

# Create the dataset
Household_2 <- tibble(
  Jenis_Makanan = Jenis,
  Waktu_Masak = Waktu_Masak
)

# Show interactive table with download buttons
datatable(
  Household_2,
  extensions = 'Buttons',
  options = list(
    dom = 'Bfrtip',
    buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
    scrollY = "400px",
    scrollCollapse = TRUE,
    paging = FALSE
  ),
  caption = htmltools::tags$caption(
    style = 'caption-side: top; text-align: left; 
             font-size: 18px; font-weight: bold;'
  ),
  class = 'stripe hover compact'
)

1.3 Visualisasi

# Install and load necessary libraries
library(plotly)
library(DT)

# Gunakan kolom Listrik sebagai data normal
normal_data <- Household_2$Waktu_Masak

# Calculate the density of the normal data
density_data <- density(normal_data)

# Create a histogram of the normal data using plotly
histogram_plot <- plot_ly(
  x = normal_data,
  type = 'histogram',
  marker = list(color = 'lightblue', line = list(color = 'black', width = 1)),
  name = 'Histogram of Normal Data',
  nbinsx = 30,
  opacity = 0.6,
  showlegend = TRUE
) %>%
  # Add the density curve
  add_trace(
    x = density_data$x, 
    y = density_data$y * length(normal_data) * diff(range(normal_data)) / 30,  # Scale density to histogram
    type = 'scatter',
    mode = 'lines',
    name = 'Density Curve',
    line = list(color = 'black', width = 3),
    showlegend = TRUE
  ) %>%
  layout(
    title = 'Histogram of Normal Distribution with Density Curve',
    xaxis = list(title = 'Value', showgrid = FALSE),
    yaxis = list(title = 'Frequency / Density', showgrid = FALSE),
    bargap = 0.1,
    plot_bgcolor = 'white',
    paper_bgcolor = 'white',
    showlegend = TRUE,
    legend = list(
      orientation = 'v',    # vertical legend
      x = 0.98,             # almost at the right edge
      xanchor = 'right',
      y = 0.98,             # almost at the top
      yanchor = 'top',
      bgcolor = 'rgba(255,255,255,0.8)',  # semi-transparent background
      bordercolor = 'black',
      borderwidth = 0.3
    )
  )

# Show the plot
histogram_plot

3. Pareto Chart

# Load libraries
library(dplyr)
library(plotly)
library(RColorBrewer)

# Create the dataset
set.seed(42)
n <- 31

dates <- seq.Date(from = as.Date("2025-05-01"), to = as.Date("2025-05-31"), by = "day")
sample_dates <- sample(dates, n, replace = TRUE)

Pengeluaran <- sample(c("Listrik", "Belanja Mingguan", "Gas LPG", "Transportasi", "Makan di luar", "Internet", "Pengeluaran darurat", "PDAM"), n, replace = TRUE)

Household <- tibble(
  Tanggal = sample_dates,
  Pengeluaran = Pengeluaran
)

# Summarize the number of delays by reason
pareto_data <- Household %>%
  count(Pengeluaran, sort = TRUE) %>%
  mutate(
    cum_freq = cumsum(n) / sum(n) * 100  # cumulative percentage
  )

# Create different colors for each Reason (adjusted for the length of reasons)
colors <- RColorBrewer::brewer.pal(n = min(12, length(pareto_data$Pengeluaran)), name = "Set3")

# Create Plotly Pareto Chart
fig <- plot_ly()

# Add Bar Chart (Count) - with different colors
fig <- fig %>% add_bars(
  x = ~reorder(pareto_data$Pengeluaran, -pareto_data$n),
  y = ~pareto_data$n,
  name = 'Number of Delays',
  marker = list(color = colors),
  yaxis = "y1"
)

# Add Cumulative Line
fig <- fig %>% add_lines(
  x = ~reorder(pareto_data$Pengeluaran, -pareto_data$n),
  y = ~pareto_data$cum_freq,
  name = 'Cumulative (%)',
  yaxis = "y2",
  line = list(color = 'red', dash = 'dash')
)

# Add Cut-off Line at 80%
fig <- fig %>% add_lines(
  x = ~reorder(pareto_data$Pengeluaran, -pareto_data$n),
  y = rep(80, length(pareto_data$Pengeluaran)),
  name = 'Cut-off 80%',
  yaxis = "y2",
  line = list(color = 'green', dash = 'dot')
)

# Adjust layout
fig <- fig %>% layout(
  title = "Pareto Chart - Delay Reasons",
  xaxis = list(
    title = "Delay Reasons",
    tickangle = -45   # tilt 45 degrees
  ),
  yaxis = list(title = "Number of Delays"),
  yaxis2 = list(
    title = "Cumulative (%)",
    overlaying = "y",
    side = "right",
    range = c(0, 100)
  ),
  legend = list(x = 0.8, y = 0.75),
  shapes = list(
    list(
      type = "line",
      x0 = -0.5,
      x1 = length(pareto_data$Pengeluaran) - 0.5,
      y0 = 80,
      y1 = 80,
      yref = "y2",
      line = list(color = "green", width = 2, dash = "dot")
    )
  )
)

# Show chart
fig

4. Fishbone

library(DiagrammeR)
library(rsvg)


grViz("
digraph fishbone {
  graph [layout = dot, rankdir = LR]

  # Default node styles
  node [fontname=Helvetica, fontsize=25, style=filled]

  # Central problem
  Problem [label='Delayed \\n Goods Delivery', shape=ellipse, fillcolor=lightcoral, width=5.0, height=1.2]

  # Category nodes (shared style)
  node [shape=diamond, width=2.5, height=1.0, fillcolor='#FFD700']
  A1 [label='Ekonomi']
  A2 [label='Komunikasi']
  A3 [label='Pembagian Tugas']
  A4 [label='Waktu']
  A5 [label='Pola asuh']
 

  # Reset node style for sub-categories
  node [shape=ellipse, width=2.5, height=0.6, fillcolor='#90EE90']
  A1a [label='Pengeluaran berlebih']
  A1b [label='Penghasilan tidak tetap']
  A1c [label='Tidak ada tabungan darurat']

  A2a [label='Kurang terbuka']
  A2b [label='Salah Paham']

  A3a [label='Tidak Adil']
  A3b [label='Tidak sesuai kesepakatan']

  A4a [label='Kurangnya waktu bersama']
  A4b [label='Terlalu sibuk bekerja']

  A5a [label='Perbedaan gaya asuh']
  A5b [label='Kesepakatan dalam disiplin']


  # Relationships
  A1 -> Problem
  A2 -> Problem
  A3 -> Problem
  A4 -> Problem
  A5 -> Problem

  A1a -> A1
  A1b -> A1
  A1c -> A1

  A2a -> A2
  A2b -> A2

  A3a -> A3
  A3b -> A3

  A4a -> A4
  A4b -> A4

  A5a -> A5
  A5b -> A5
}
")

5. Scatter Diagram

# Load the plotly package
library(plotly)

# Create a dataset: sleep hours vs mood level
set.seed(42)  # For reproducibility
data <- data.frame(
  Sleep_Hours = seq(4, 10, length.out = 20),  # dari 4 hingga 10 jam tidur
  Mood_Level = seq(30, 90, length.out = 20)   # asumsi mood level antara 30-90
)

# Tambahkan noise agar korelasi mendekati 0.9
data$Mood_Level <- data$Mood_Level + rnorm(n = 20, mean = 0, sd = 5)

# Hitung koefisien korelasi
correlation_value <- cor(data$Sleep_Hours, data$Mood_Level)

# Buat scatter plot menggunakan Plotly dengan garis regresi linier
fig <- plot_ly(data, 
               x = ~Sleep_Hours, 
               y = ~Mood_Level, 
               type = 'scatter', 
               mode = 'markers',
               marker = list(color = 'blue', size = 10)) %>%
  add_lines(x = data$Sleep_Hours, 
            y = predict(lm(Mood_Level ~ Sleep_Hours, data = data)), 
            line = list(color = 'red', dash = 'solid', width = 2)) %>%
  layout(title = paste("Scatter Plot: Hubungan antara Jam Tidur dan Suasana Hati\nKorelasi: ", round(correlation_value, 2)),
         xaxis = list(title = "Jam Tidur"),
         yaxis = list(title = "Suasana Hati"))

# Tampilkan plot
fig

6. Control Chart

# Load libraries
library(plotly)
library(dplyr)

# Simulasi data berat kemasan per hari (contoh)
set.seed(1)
df <- tibble(
  Hari = 1:30,
  Berat = round(rnorm(30, mean = 500, sd = 5), 1)
)

# Tentukan batas kendali (CL, UCL, LCL)
CL <- mean(df$Berat, na.rm = TRUE)
UCL <- CL + 3 * sd(df$Berat, na.rm = TRUE)
LCL <- CL - 3 * sd(df$Berat, na.rm = TRUE)

# Tandai outlier
df <- df %>%
  mutate(Outlier = ifelse(Berat > UCL | Berat < LCL, "Ya", "Tidak"))

# Buat plot
plot_ly(df, x = ~Hari, y = ~Berat, type = 'scatter', mode = 'lines+markers',
        line = list(color = 'blue'), 
        marker = list(size = 8, color = ifelse(df$Outlier == "Ya", "red", "blue")),
        hoverinfo = 'text',
        text = ~paste("Hari:", Hari, "<br>Berat:", Berat, "gram")) %>%
  add_lines(y = rep(CL, nrow(df)), name = "CL", line = list(color = 'green', dash = 'dot')) %>%
  add_lines(y = rep(UCL, nrow(df)), name = "UCL", line = list(color = 'red', dash = 'dot')) %>%
  add_lines(y = rep(LCL, nrow(df)), name = "LCL", line = list(color = 'red', dash = 'dot')) %>%
  layout(title = "Control Chart – Berat Kemasan Produk",
         xaxis = list(title = "Hari"),
         yaxis = list(title = "Berat (gram)"),
         legend = list(orientation = 'h', x = 0.3, y = -0.2))

7. Flowchart

library(DiagrammeR)

grViz("
digraph sistem_kegiatan_rumah_tangga {

  graph [layout = dot, rankdir = TB]

  node [shape = box, style = filled, fontsize = 12]

  # Node definisi
  label1 [label = 'Mulai', shape = ellipse, color = lightgreen]
  label2 [label = 'Ingin langsung\nmasak?', shape = diamond, color = orange]
  label3 [label = 'Masak', shape = box, color = pink]
  label4 [label = 'Bangun Tidur', shape = parallelogram, color = lightblue]
  label5 [label = 'Sudah selesai\nsemua?', shape = diamond, color = orange]
  label6 [label = 'Istirahat', shape = trapezium, color = thistle]
  label7 [label = 'Selesai', shape = ellipse, color = lightgreen]

  # Edge (alur)
  label1 -> label2
  label2 -> label3 [label = 'Ya']
  label2 -> label4 [label = 'Tidak']
  label3 -> label5
  label4 -> label5
  label5 -> label7 [label = 'Ya']
  label5 -> label6 [label = 'Tidak']
  label6 -> label7
}
")