R語言圖表


條形圖
在R語言中創建條形圖的基本語法是
barplot(H, xlab, ylab, main, names.arg, col)
H是包含在條形圖中使用的數值的向量或矩陣
xlab是x軸的標簽
ylab是y軸的標簽
main是條形圖的標題
names.arg是在每個條下出現的名稱的向量
col用於向圖中的條形提供顏色
 
組合條形圖和堆積條形圖
# Create the input vectors.
colors <- c("green","orange","brown")
months <- c("Mar","Apr","May","Jun","Jul")
regions <- c("East","West","North")
# Create the matrix of the values.
Values <- matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11),nrow = 3,ncol = 5,byrow = TRUE)
# Give the chart file a name.
png(file = "barchart_stacked.png")
# Create the bar chart.
barplot(Values,main = "total revenue",names.arg = months,xlab = "month",ylab = "revenue",col = colors)
# Add the legend to the chart.
legend("topleft", regions, cex = 1.3, fill = colors)
# Save the file.
dev.off()
 
箱線圖表示數據集中的最小值,最大值,中值,第一四分位數和第三四分位數
在R語言中創建箱線圖的基本語法是
boxplot(x, data, notch, varwidth, names, main)
x是向量或公式
data是數據幀
notch是邏輯值。設置為TRUE以繪制凹口
varwidth是一個邏輯值。設置為true以繪制與樣本大小成比例的框的寬度
names是將打印在每個箱線圖下的組標簽
main用於給圖表標題
 
帶槽的箱線圖
# Give the chart file a name.
png(file = "boxplot_with_notch.png")
# Plot the chart.
boxplot(mpg ~ cyl, data = mtcars,
   xlab = "Number of Cylinders",
   ylab = "Miles Per Gallon", 
   main = "Mileage Data",
   notch = TRUE, 
   varwidth = TRUE, 
   col = c("green","yellow","purple"),
   names = c("High","Medium","Low")
)
# Save the file.
dev.off()
 
直方圖表示被存儲到范圍中的變量的值的頻率。 直方圖類似於條形圖,但不同之處在於將值分組為連續范圍。直方圖中的每個柱表示該范圍中存在的值的數量的高度
使用R語言創建直方圖的基本語法是
hist(v,main,xlab,xlim,ylim,breaks,col,border)
v是包含直方圖中使用的數值的向量
main表示圖表的標題
col用於設置條的顏色
border用於設置每個條的邊框顏色
xlab用於給出x軸的描述
xlim用於指定x軸上的值的范圍
ylim用於指定y軸上的值的范圍
break用於提及每個條的寬度
 
在R語言中創建折線圖的基本語法是
plot(v,type,col,xlab,ylab)
v是包含數值的向量
類型采用值“p”僅繪制點,“l”僅繪制線和“o”繪制點和線
xlab是x軸的標簽
ylab是y軸的標簽
main是圖表的標題
col用於給點和線的顏色
 
多線型折線圖
通過使用lines()函數,可以在同一個圖表上繪制多條線
# Create the data for the chart.
v <- c(7,12,28,3,41)
t <- c(14,7,6,19,3)
# Give the chart file a name.
png(file = "line_chart_2_lines.jpg")
# Plot the bar chart.
plot(v,type = "o",col = "red", xlab = "Month", ylab = "Rain fall", main = "Rain fall chart")
lines(t, type = "o", col = "blue")
# Save the file.
dev.off()
 
在R語言中創建散點圖的基本語法是
plot(x, y, main, xlab, ylab, xlim, ylim, axes)
x是其值為水平坐標的數據集
y是其值是垂直坐標的數據集
main要是圖形的圖塊
xlab是水平軸上的標簽
ylab是垂直軸上的標簽
xlim是用於繪圖的x的值的極限
ylim是用於繪圖的y的值的極限
axes指示是否應在繪圖上繪制兩個軸
 
在R中創建散點圖矩陣的基本語法是
pairs(formula, data)
formula表示成對使用的一系列變量
data表示將從其獲取變量的數據集
 
使用R語言創建餅圖的基本語法是
pie(x, labels, radius, main, col, clockwise)
x是包含餅圖中使用的數值的向量
labels用於給出切片的描述
radius表示餅圖圓的半徑(值-1和+1之間)
main表示圖表的標題
col表示調色板
clockwise是指示片段是順時針還是逆時針繪制的邏輯值


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM