R語言實戰試卷 第三章 圖形初階

weixin_34192993發表於2018-08-24
  • 圖形引數設定
  • 新增文字、自定義座標軸和圖例
  • 圖形的組合
  1. 下列物件drugA和drugB是對兩種藥物5個劑量水平(dose)上的響應的病人數目,請在R studio上執行以下程式碼並回答問題:
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)

(1). 執行語句plot(dose, drugA, type = "b"),描述該語句得到的圖形並模仿此語句繪製出藥物B的劑量和響應人數關係圖;
(2). 畫出圖形引數設定為lwd=3, type="b", lty=3, pch=15, cex=2, col=2的藥物B的劑量和響應人數關係圖,並依據圖Graph parameters in R解釋這些引數的含義;

6012414-b3f5bd6f1b973537.png
Graph parameters in R

  1. 在R中繪製圖形時下列哪些指定引數為白色( )(多選題)
    A. col = 1
    B. col = "white"
    C. col = "#FFFFFF"
    D. col = rgb(1, 1, 1)
    E. col = hsv(0, 0, 1)
  2. 函式colors()可以返回所有可用顏色的名稱,在R中檢視該函式中有多少種顏色,並回答第657個顏色是什麼;
  3. R中有許多用於建立連續型顏色向量的函式,在R中執行以下語句:
install.packages("RColorBrewer")
library(RColorBrewer)
n <- 7
rainbow_color <- brewer.pal(n, "Set1")
barplot(rep(1, n), col = rainbow_color)

(1). 鍵入display.brewer.all()可以得到如圖brewer all,rainbow_color <- brewer.pal(n, "Set1")中從Set1調色盤中抽取了7種用十六進位制表示的顏色並返回一個向量。請模仿題目中給出的語句寫出可以從BrBG調色盤中抽取7種顏色繪製條形圖的語句;

6012414-a182c9fb5babde30.png
brewer all

(2). 鍵入brewer_all <- as.matrix(brewer.pal.info),檢視物件brewer_all有多少個調色盤,簡要介紹brewer_all中的資料資訊;
(3). 在R中執行以下語句:

getwd()
pdf("myfirstgraph.pdf")
n <- 11
rainbow_color <- brewer.pal(n, "Spectral")
barplot(rep(1, n), col = rainbow_color)
dev.off()

getwd()得到的路徑中檢視檔案myfirstgraph.pdf,若想把該圖儲存為png格式並儲存在當前目錄下,請寫出相應語句;

  1. 下列物件drugA和drugB是對兩種藥物5個劑量水平(dose)上的響應的病人數目,請在R studio上執行以下程式碼並回答問題:
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)

opar <- par(no.readonly = TRUE)
par(pin=c(2, 3))
par(lwd=2, cex=1.5)
par(cex.axis=0.75, font.axis=3)
plot(dose, drugA, type = "b", pch=19, lty=2, col="red")
plot(dose, drugB, type = "b", pch=23, lty=6, col="blue", bg="green")
par(opar)

(1). par()plot()都可以對圖形的引數進行設定,問:他們有什麼區別?(鍵入help(par)可以查閱更多關於par()的資訊來回答此問);
(2). 在R studio上執行以下程式碼,比較與(1)中得到的圖,解釋引數main,sub,xlab,ylab,xlim,ylim;

plot(dose, drugA, type = "b", col="red", pch=2, lty=2, lwd =2,
     main = "Clinical Trials for Drug A", 
     sub= "This is hypothetical data",
     xlab = "Dosage", ylab = "Drugs Response", 
     xlim = c(0, 60), ylim=c(0, 70))

(3). 在R studio上執行以下程式碼, 比較與(2)中得到的圖,解釋引數ann=FALSE與函式title()

plot(dose, drugA, type = "b", col="red", 
     pch=2, lty=2, lwd =2, 
     xlim = c(0, 60), ylim=c(0, 70), 
     ann=FALSE)
title(main = "Clinical Trials for Drug A", col.main="red", cex.main=0.85,
      sub= "This is hypothetical data", col.sub="blue", cex.sub=0.70,
      xlab = "Dosage", ylab = "Drugs Response", col.lab="blue", cex.lab=0.75)

(4). 在R studio上執行以下程式碼,解釋axis()mtext()所在語句,解釋引數ann = FALSE

x <- c(1:10)
y <- x
z <- 10/x
opar <- par(no.readonly = TRUE) #生成一個可以修改的當前圖形的引數列表
par(mar=c(5, 4, 4, 8) + 0.1)
plot(x, y, type = "b",
     pch=21, col="red",
     yaxt="n", lty=3, ann = FALSE)
lines(x, z, type = "b", pch=22, col="blue", lty=2)
axis(2, at=x, lables=x, col.axis="red", las=2)
axis(4, at=z, lables=round(z, digits = 2),
     col.axis="blue", las=2, cex.axis=0.7, tck= -.01)
mtext("y=1/x", side=4, line = 3, cex.lab=1, las=2, col="blue")
title("An example of Creative Axes",
      xlab = "X values",
      ylab = "Y=X")
par(opar)

(5).在R studio上執行以下程式碼,描述所繪圖形中的元素與語句中函式和引數的關係;

opar <- par(no.readonly = TRUE)
par(lwd=2, cex=1.5, font.lab=2)
plot(dose, drugA, type="b",
     pch=15, lty=1, col="red", ylim = c(1, 60),
     main = "Drug A vs Drug B",
     xlab = "Drug Dosage", ylab = "Drug Response")
lines(dose, drugB, type = "b",
      pch=17, lty=2, col="blue")
abline(h=c(30), lwd=1.5, lty=2, col="gray")
library(Hmisc)
minor.tick(nx=3, ny=3, tick.ratio = 0.5)
legend("topleft", inset = 0.05, title="Drug Type", c("A", "B"),
       lty = c(1, 2), pch = c(15,17), col=c("red", "blue"))
par(opar)
  1. 在R studio上執行以下程式碼,回答下列問題:
attach(mtcars)
opar <- par(no.readonly = TRUE)
par(fig=c(0, 0.8, 0, 0.8))
plot(wt, mpg,
     xlab = "Weight", ylab = "Mileage",
     pch=18, col="blue")
text(wt, mpg,
     row.names(mtcars),
     cex = 0.6, pos = 4, col = "red")
par(fig=c(0, 0.8, 0.55, 1), new=TRUE)
boxplot(wt, horizontal = TRUE, axes=FALSE)
par(fig=c(0.65, 1, 0, 0.8), new=TRUE)
boxplot(mpg, axes=FALSE)
mtext("Enhanced Scatterplot", side = 3, outer = TRUE, line = -3)
par(opar)

(1). 在R中,通常可以是使用text()mtext()將文字新增到圖形上,說明這兩個函式的區別;
(2). 解釋par(fig=c(0, 0.8, 0, 0.8))par(fig=c(0, 0.8, 0.55, 1), new=TRUE)par(fig=c(0.65, 1, 0, 0.8), new=TRUE);

attach(mtcars)
opar <- par(no.readonly = TRUE)
par(mfrow=c(2,2))
plot(wt, mpg, main = "Scatterplot of wt vs. mpg")
plot(wt, disp, main = "Scatterplot of wt vs. disp")
hist(wt, main = "Histogram of wt")
boxplot(wt, main = "Boxplot of wt")
par(opar)

(1).執行題目中和以下程式碼,觀察得到的圖形組合的差異,說明par(mfrow=c(2,2))par(mfcol=c(2,2))的區別;

opar <- par(no.readonly = TRUE)
par(mfcol=c(2,2))
plot(wt, mpg, main = "Scatterplot of wt vs. mpg")
plot(wt, disp, main = "Scatterplot of wt vs. disp")
hist(wt, main = "Histogram of wt")
boxplot(wt, main = "Boxplot of wt")
par(opar)
layout(matrix(c(1, 1, 2, 3), 2, 2, byrow = TRUE))
hist(wt)
hist(mpg)
hist(disp)

(1).執行題目中和以下程式碼,說明引數widthsheights

layout(matrix(c(1, 1, 2, 3),  2,  2,  byrow = TRUE),
       widths = c(3, 1), heights = c(1, 2))
hist(wt)
hist(mpg)
hist(disp)

相關文章