Using R Language to Plot Diagrams

Preface

Maybe these methods are quite useful when dealing with the “assignment for Statistics”. —— GGN_2015 from Jilin University

Auto Install Package – pacman

# "pacman" is a package which can install packages automatically when the required package is not found.
install.packages("pacman")

# After "pacman" is installed, use the following code to autoload library "qcc"
library(pacman)
p_load(qcc)

Histogram

Here is a simple method to plot a Histogram with no packages required.

# Use R language to create a histogram
d = c(62,68,69,80,68,79,83,70,74,73,74,75,80,77,80,83,73,79,100,93,92,101,87,96,99,94,102,95,90,98,86,93,91,90,95,97,87,89,100,93,92,98,101,97,102,91,87,110,106,118)
hist(d, xlab = "Temperature", col = "white", border = "black", breaks = seq(60, 120, 10), right = TRUE, main = "Histogram for Temperature")
  • xlab set the title of X axis;
  • col set the color of bars in histogram;
  • border set the color of the border of bars;
  • breaks set the data range of each bar;
    • the left-most and right-most point must contain all the values in data set;
  • right = TRUE to set data range of each bar as Left-Open& Right-Closed;
  • main set the title of the whole diagram.

Histogram

Pareto Diagram

# Please make sure that package "pacman" is installed before you run this program.
library(pacman)

# if there is no "qcc", install and load, otherwise load directly.
p_load(qcc)

# data set vector "Waste" (ordered it yourself)
Wastes= c(75, 31, 22, 8)

# give names to each column of vector "Wastes"
names(Wastes) = c('Plastic', 'Irom', 'Clothes', 'Spares') 

# plot a pareto diagram
pareto.chart(
  Wastes, 
  ylab="Waste Amount",  # left-side-label
  col = "white", # set the color of bars
  # cumperc = seq(0, 120, by = 5),  # coordinate on the Right
  ylab2 = "Acummulative Percentage", # right-side-label
  main = "Perato Diagram" # Title of the
)

Diagram2

Scattered Diagram

x_data = c(2.62,2.875,2.32,3.215,3.44,3.46,3.57,3.19,3.15,3.44,3.44,4.07,3.73,3.78,5.25,5.424,5.345,2.2,1.615,1.835,2.465,3.52,3.435,3.84,3.845,1.935,2.14,1.513,3.17,2.77,3.57,2.78)
y_data = c(21,21,22.8,21.4,18.7,18.1,14.3,24.4,22.8,19.2,17.8,16.4,17.3,15.2,10.4,10.4,14.7,32.4,30.4,33.9,21.5,15.5,15.2,13.3,19.2,27.3,26,30.4,15.8,19.7,15,21.4) 
plot(x_data, y_data, ylab = "y data", xlab = "x data", main = "Scattered Diagram", type = "p")

# Use least-squares-method to get the linear relationship and show it on screen
abline(lm(y_data ~ x_data)) 
  • type="p" create a dot diagram;
  • type="l" create a broken line diagram;
  • type="o" create a broken line diagram with dots between lines.

Scattered Diagram

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值