欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

R語言繪圖基礎(chǔ)教程(新手入門推薦!)

 更新時(shí)間:2022年11月04日 10:17:41   作者:Annaaphq  
數(shù)據(jù)作圖是數(shù)據(jù)分析的重要方法之一,R提供了豐富的作圖函,下面這篇文章主要給大家介紹了關(guān)于R語言繪圖基礎(chǔ)教程的相關(guān)資料,文中通過實(shí)例代碼以及圖文介紹的非常詳細(xì),需要的朋友可以參考下

一、R語言繪圖初階

首先下載包,繪制一張圖:

#install.packages(c("Hmisc", "RColorBrewer"))
#install.packages(c("vcd", "plotrix", "sm", "vioplot"))
library(ggplot2, lib.loc=""~/R/lib"")

#生存一個(gè)可以修改的當(dāng)前圖形參數(shù)列表
par(ask=TRUE)
opar <- par(no.readonly=TRUE)

#載入數(shù)據(jù)集
attach(mtcars) # mtcars是R中自帶的數(shù)據(jù)集,包含多種汽車的數(shù)據(jù)
plot(wt, mpg)
abline(lm(mpg~wt)) #abline是畫一條或多條直線,lm是線性模型
title("Regression of MPG on Weight")

#不用了一定要detach
detach(mtcars)

1、圖形參數(shù)

字體、顏色、坐標(biāo)軸、標(biāo)簽等通過**函數(shù)par()**來指定選項(xiàng)。以這種方式設(shè)定的參數(shù)值除非被再次修改,否則將在
會(huì)話結(jié)束前一直有效。其調(diào)用格式為:

par(optionname=value,optionname=name,...)

不加參數(shù)地執(zhí)行par()將生成一個(gè)含有當(dāng)前圖形參數(shù)設(shè)置的列表。添加參數(shù)no.readonly=TRUE可以生成一個(gè)可以修改的當(dāng)前圖形參數(shù)列表

例如一個(gè)藥品的例子:

# Input data for drug example
dose  <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
plot(dose, drugA, type="b")
opar <- par(no.readonly=TRUE) #很重要

2、符號和線條

對上面的例子,現(xiàn)修改一些參數(shù):

par(lty=2, pch=17)            # change line type and symbol
plot(dose, drugA, type="b")   # generate a plot
par(opar)                     # restore the original settings,恢復(fù)原設(shè)置 

plot(dose, drugA, type="b", lty=3, lwd=3, pch=17, cex=2)

3、顏色

# choosing colors
library(RColorBrewer)
n <- 7
mycolors <- brewer.pal(n, "Set1")
barplot(rep(1,n), col=mycolors)

n <- 10
mycolors <- rainbow(n)
pie(rep(1, n), labels=mycolors, col=mycolors)

把餅圖改成灰色:

mygrays <- gray(0:n/n)
pie(rep(1, n), labels=mygrays, col=mygrays)

4、文本屬性

# View font families 
opar <- par(no.readonly=TRUE)
par(cex=1.5)
plot(1:7,1:7,type="n")
text(3,3,"Example of default text")
text(4,4,family="mono","Example of mono-spaced text")
text(5,5,family="serif","Example of serif text")
par(opar)

仍用上方藥物例子:

par(lwd=2, cex=1.5)
par(cex.axis=.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)				

5、圖形尺寸與邊界尺寸

par(pin=c(2, 3))#圖形大小

6、添加文本、自定義坐標(biāo)軸和圖例

可使用title()函數(shù)為圖形添加標(biāo)題和坐標(biāo)軸標(biāo)簽,調(diào)用格式:

 title(main="maintitle", sub="subtitle", xlab="x-axis label", ylab="y-axis label")
plot(dose, drugA, type="b",  
     col="red", lty=2, pch=2, lwd=2,
     main="Clinical Trials for Drug A", 
     sub="This is hypothetical data", 
     xlab="Dosage", ylab="Drug Response",
     xlim=c(18, 60), ylim=c(0, 70)) #x軸刻度大小

你可以使用函數(shù)axis()來創(chuàng)建自定義的坐標(biāo)軸,而非使用R中的默認(rèn)坐標(biāo)軸,其格式為:

axis(side, at=, labels=, pos=, lty=, col=, las=, tck=, ...) #例子在下面

函數(shù)**abline()**可以用來為圖形添加參考線,其使用格式為:

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(lwd=2, cex=1.5, font.lab=2)
plot(dose, drugA, type="b",
     pch=15, lty=1, col="red", ylim=c(0, 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")
abline(h=yvalues,v=xvalues)

我們可以使用**函數(shù)legend()**來添加圖例,其使用格式為:

legend(location,title, legend, ...) 

也可以這樣理解:先繪制drugA的圖,在這基礎(chǔ)上加一天B的線,接下來添加圖例:

library(Hmisc)
minor.tick(nx=3, ny=3, tick.ratio=0.005) #自己加一些小的刻度線
legend("topleft", inset=.005, title="Drug Type", c("A","B"),
       lty=c(1, 2), pch=c(15, 17), col=c("red", "blue"))
par(opar)

函數(shù)text()和mtext()將文本添加到圖形上。 text()可向繪圖區(qū)域內(nèi)部添加文本,而mtext()則向圖形的四個(gè)邊界之一添加文本,使用格式分別為:

text(location, "text to place", pos, ...)
 mtext("text to place", side, line=n, ...) 
x <- c(1:10)
y <- x
z <- 10/x
opar <- par(no.readonly=TRUE)  # make a copy of current settings
par(mar=c(5, 4, 4, 8) + 0.1)
plot(x, y, type="b",
     pch=21, col="red",
     yaxt="n",lty=3, ann=FALSE)
     #yaxt指y軸類型,“n”指阻止y軸畫出,ann指注釋,此處為了下方自己添加,所以都去掉了
lines(x, z, type="b", pch=22, col="blue", lty=2)
axis(2, at=x, labels=x, col.axis="red", las=2) #坐標(biāo)軸另一種加法
axis(4, at=z, labels=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)

又如:

attach(mtcars)
plot(wt, mpg,
     main="Mileage vs. Car Weight",
     xlab="Weight", ylab="Mileage",
     pch=18, col="blue")
text(wt, mpg,
     row.names(mtcars),
     cex=0.6, pos=4, col="red")
detach(mtcars)

7、圖形的組合

par()函數(shù)中使用圖形參數(shù) mfrow=c(nrows, ncols) 來創(chuàng)建按行填充的、行數(shù)為nrows、列數(shù)為ncols的圖形矩陣。另外,可以使用 mfcol=c(nrows, ncols) 按列填充矩陣

可以自己選擇布局大?。?/p>

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)
detach(mtcars)

也可以嘗試下三行一列:

attach(mtcars)
opar <- par(no.readonly=TRUE)
par(mfrow=c(3,1))
hist(wt)
hist(mpg)
hist(disp)
par(opar)
detach(mtcars)

填充矩陣:

attach(mtcars)
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
hist(wt)
hist(mpg)
hist(disp)
detach(mtcars)

attach(mtcars)
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)
detach(mtcars)

還可以這樣布局:

opar <- par(no.readonly=TRUE)
par(fig=c(0, 0.8, 0, 0.8))
plot(mtcars$wt, mtcars$mpg,
     xlab="Miles Per Gallon",
     ylab="Car Weight")
par(fig=c(0, 0.8, 0.55, 1), new=TRUE)
boxplot(mtcars$wt, horizontal=TRUE, axes=FALSE)
par(fig=c(0.65, 1, 0, 0.8), new=TRUE)
boxplot(mtcars$mpg, axes=FALSE)
mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3)
par(opar)

二、R語言基本圖形繪制

1、條形圖

1.1 簡單條形圖

若height是一個(gè)向量,則它的值就確定了各條形的高度,并將繪制一幅垂直的條形圖。使用選項(xiàng)horiz=TRUE則會(huì)生成一幅水平條形圖。你也可以添加標(biāo)注選項(xiàng)。選項(xiàng)main可添加一個(gè)圖形標(biāo)題,而選項(xiàng)xlab和ylab則會(huì)分別添加x軸和y軸標(biāo)簽

par(ask=TRUE)
opar <- par(no.readonly=TRUE) # save original parameter settings

library(vcd)
counts <- table(Arthritis$Improved) #自帶的
counts

#大致查看一下數(shù)據(jù)前幾行
head(Arthritis)

barplot(counts, 
        main="Simple Bar Plot",
        xlab="Improvement", ylab="Frequency")

# 水平條形圖   
barplot(counts, 
        main="Horizontal Bar Plot", 
        xlab="Frequency", ylab="Improvement", 
        horiz=TRUE)

1.2 堆砌條形圖和分組條形圖

如果height是一個(gè)矩陣而不是一個(gè)向量,則繪圖結(jié)果將是一幅堆砌條形圖或分組條形圖。若beside=FALSE(默認(rèn)值),則矩陣中的每一列都將生成圖中的一個(gè)條形,各列中的值將給出堆砌的“子條”的高度。若beside=TRUE,則矩陣中的每一列都表示一個(gè)分組,各列中的值將并列而不是堆砌

堆砌條形圖

library(vcd)
counts <- table(Arthritis$Improved, Arthritis$Treatment)
counts

barplot(counts, 
        main="Stacked Bar Plot",
        xlab="Treatment", ylab="Frequency", 
        col=c("red", "yellow","green"),  
        legend=rownames(counts)) 
        
#調(diào)整下圖例大?。喝绻麍D例過大遮住圖形,可以設(shè)置一下電腦的顯示比例,調(diào)小

分組條形圖

barplot(counts, 
        main="Grouped Bar Plot", 
        xlab="Treatment", ylab="Frequency",
        col=c("red", "yellow", "green"),
        legend=rownames(counts), beside=TRUE)

1.3 均值條形圖

條形圖并不一定要基于計(jì)數(shù)數(shù)據(jù)或頻率數(shù)據(jù)。你可以使用數(shù)據(jù)整合函數(shù)并將結(jié)果傳遞給barplot()函數(shù),來創(chuàng)建表示均值、中位數(shù)、標(biāo)準(zhǔn)差等的條形圖

states <- data.frame(state.region, state.x77) #本身有的數(shù)據(jù)集
means <- aggregate(states$Illiteracy, by=list(state.region), FUN=mean)
means

means <- means[order(means$x),]  
means

barplot(means$x, names.arg=means$Group.1) 
title("Mean Illiteracy Rate") 

1.4 條形圖的微調(diào)

有若干種方式可以微調(diào)條形圖的外觀。例如,隨著條數(shù)的增多,條形的標(biāo)簽可能會(huì)開始重疊。你可以使用參數(shù)cex.names來減小字號。將其指定為小于1的值可以縮小標(biāo)簽的大小。可選的參數(shù)names.arg允許你指定一個(gè)字符向量作為條形的標(biāo)簽名,你同樣可以使用圖形參數(shù)輔助調(diào)整文本間隔

# Listing 6.4 - Fitting labels in bar plots
par(las=2)                # set label text perpendicular to the axis
par(mar=c(5,8,4,2))       # increase the y-axis margin
counts <- table(Arthritis$Improved) # get the data for the bars

# produce the graph
barplot(counts, 
        main="Treatment Outcome", horiz=TRUE, cex.names=0.8,
        names.arg=c("No Improvement", "Some Improvement", "Marked Improvement")
)
par(opar)

2、棘狀圖

是一種特殊的條形圖,它稱為棘狀圖(spinogram)。棘狀圖對堆砌條形圖進(jìn)行了重縮放,這樣每個(gè)條形的高度均為1,每一段的高度即表示比例。棘狀圖可由vcd包中的函數(shù) spine() 繪制

library(vcd)
attach(Arthritis)
counts <- table(Treatment,Improved)
spine(counts, main="Spinogram Example")
detach(Arthritis)

3、餅圖

餅圖可由以下函數(shù)創(chuàng)建: pie(x, labels),其中x是一個(gè)非負(fù)數(shù)值向量,表示每個(gè)扇形的面積,而labels則是表示各扇形標(biāo)簽的字符型向量

par(mfrow=c(2,2)) #畫布的位置                             
slices <- c(10, 12, 4, 16, 8) #對應(yīng)下方5個(gè)國家位置
lbls <- c("US", "UK", "Australia", "Germany", "France")

pie(slices, labels = lbls, 
    main="Simple Pie Chart")

#更復(fù)雜一點(diǎn)
pct <- round(slices/sum(slices)*100)  #算百分比比例                    
lbls <- paste(lbls, pct) 
lbls <- paste(lbls,"%",sep="")
pie(slices,labels = lbls, col=rainbow(length(lbls)),
    main="Pie Chart with Percentages")

#畫立體餅圖
library(plotrix)                                               
pie3D(slices, labels=lbls,explode=0.1,
      main="3D Pie Chart ") #設(shè)置explode立體效果
      
mytable <- table(state.region)                                   
lbls <- paste(names(mytable), "\n", mytable, sep="")
pie(mytable, labels = lbls, 
    main="Pie Chart from a dataframe\n (with sample sizes)") 
par(opar)

在醫(yī)學(xué)中,餅圖與扇形圖用的都不多

下面列出扇形圖:

library(plotrix)
slices <- c(10, 12,4, 16, 8) 
lbls <- c("US", "UK", "Australia", "Germany", "France")   
fan.plot(slices, labels = lbls, main="Fan Plot")

4、直方圖

直方圖通過在x軸上將值域分割為一定數(shù)量的組,在y軸上顯示相應(yīng)值的頻數(shù),展示了連續(xù)型變量的分布??梢允褂萌缦潞瘮?shù)創(chuàng)建直方圖: hist(x)

其中的x是一個(gè)由數(shù)據(jù)值組成的數(shù)值向量。參數(shù) freq=FALSE 表示根據(jù)概率密度而不是頻數(shù)繪制圖形。參數(shù)breaks用于控制組的數(shù)量。在定義直方圖中的單元時(shí),默認(rèn)將生成等距切分

hist(mtcars$mpg)

調(diào)整下間隔:

#頻數(shù)分布直方圖
hist(mtcars$mpg, 
     breaks=12, 
     col="red", 
     xlab="Miles Per Gallon", 
     main="Colored histogram with 12 bins")

#密度直方圖,根據(jù)概率密度繪圖
hist(mtcars$mpg, 
     freq=FALSE, 
     breaks=12, 
     col="red", 
     xlab="Miles Per Gallon", 
     main="Histogram, rug plot, density curve")  
rug(jitter(mtcars$mpg)) #添加對應(yīng)的線
lines(density(mtcars$mpg), col="blue", lwd=2) #畫一條曲線

我們往往希望加入正態(tài)分布曲線

# histogram with superimposed normal curve (Thanks to Peter Dalgaard)  
x <- mtcars$mpg 
h<-hist(x, 
        breaks=12, 
        col="red", 
        xlab="Miles Per Gallon", 
        main="Histogram with normal curve and box") 
xfit<-seq(min(x),max(x),length=40) 
yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) 
yfit <- yfit*diff(h$mids[1:2])*length(x) 
lines(xfit, yfit, col="blue", lwd=2)
box()

5、核密度圖

用術(shù)語來說,核密度估計(jì)是用于估計(jì)隨機(jī)變量概率密度函數(shù)的一種非參數(shù)方法。雖然其數(shù)學(xué)細(xì)節(jié)已經(jīng)超出了本書
的范疇,但從總體上講,核密度圖不失為一種用來觀察連續(xù)型變量分布的有效方法。繪制密度圖的方法(不疊加到另一幅圖上方)為: plot(density(x))

其中的x是一個(gè)數(shù)值型向量。由于plot()函數(shù)會(huì)創(chuàng)建一幅新的圖形,所以要向一幅已經(jīng)存在的圖形上疊加一條密度曲線,可以使用lines()函數(shù)
使用sm包中的sm.density.compare()函數(shù)可向圖形疊加兩組或更多的核密度圖,使用格式為:

sm.density.compare(x, factor) 
d <- density(mtcars$mpg) # returns the density data  
plot(d) # plots the results 

d <- density(mtcars$mpg)                                  
plot(d, main="Kernel Density of Miles Per Gallon")       
polygon(d, col="red", border="blue") #填充顏色                     
rug(mtcars$mpg, col="brown") 

par(lwd=2)                                                       
library(sm)
attach(mtcars)

# create value labels 
cyl.f <- factor(cyl, levels= c(4, 6, 8),                               
                labels = c("4 cylinder", "6 cylinder", "8 cylinder")) #設(shè)置一個(gè)因子與它的水平

# plot densities 
sm.density.compare(mpg, cyl, xlab="Miles Per Gallon")                
title(main="MPG Distribution by Car Cylinders")

#加圖例
colfill<-c(2:(2+length(levels(cyl.f)))) 
cat("Use mouse to place legend...","\n\n")
legend(locator(1), levels(cyl.f), fill=colfill) 
detach(mtcars)

6、箱式圖

箱線圖(又稱盒須圖)通過繪制連續(xù)型變量的五數(shù)總括,即最小值、下四分位數(shù)(第25百分位數(shù))、中位數(shù)(第50百分位數(shù))、上四分位數(shù)(第75百分位數(shù))以及最大值,描述了連續(xù)型變量的分布。箱線圖能夠顯示出可能為離群點(diǎn)(范圍±1.5*IQR以外的值, IQR表示四分位距,即上四分位數(shù)與下四分位數(shù)的差值)的觀測,如:

boxplot(mtcars$mpg, main="Box plot", ylab="Miles per Gallon") 
boxplot(mpg~cyl,data=mtcars,
        main="Car Milage Data", 
        xlab="Number of Cylinders", 
        ylab="Miles Per Gallon")

# notched box plots凹槽箱式圖
boxplot(mpg~cyl,data=mtcars, 
        notch=TRUE, 
        varwidth=TRUE,
        col="red",
        main="Car Mileage Data", 
        xlab="Number of Cylinders", 
        ylab="Miles Per Gallon")

7、使用并列箱線圖進(jìn)行跨組比較

箱線圖可展示單個(gè)變量或分組變量。使用格式:

 boxplot(formula,data=dataframe)

其中的formula是一個(gè)公式, dataframe代表提供數(shù)據(jù)的數(shù)據(jù)框(或列表)。一個(gè)示例公式為y ~A,這將為類別型變量A的每個(gè)值并列地生成數(shù)值型變量y的箱線圖。公式y(tǒng) ~ A*B則將為類別型變量A和B所有水平的兩兩組合生成數(shù)值型變量y的箱線圖

添加參數(shù)varwidth=TRUE 將使箱線圖的寬度與其樣本大小的平方根成正比。參數(shù)horizontal=TRUE可以反轉(zhuǎn)坐標(biāo)軸的方向
箱線圖靈活多變,通過添加notch=TRUE,可以得到含凹槽的箱線圖。若兩個(gè)箱的凹槽互不重疊,則表明它們的中位數(shù)有顯著差異

# Listing 6.9 - Box plots for two crossed factors
# create a factor for number of cylinders
mtcars$cyl.f <- factor(mtcars$cyl,
                       levels=c(4,6,8),
                       labels=c("4","6","8"))

# create a factor for transmission type
mtcars$am.f <- factor(mtcars$am, 
                      levels=c(0,1), 
                      labels=c("auto","standard"))

# generate boxplot
boxplot(mpg ~ am.f *cyl.f, 
        data=mtcars, 
        varwidth=TRUE,
        col=c("gold", "darkgreen"),
        main="MPG Distribution by Auto Type", 
        xlab="Auto Type")

8、小提琴圖

小提琴圖是箱線圖與核密度圖的結(jié)合。你可以使用vioplot包中的vioplot()函數(shù)繪制它。請?jiān)诘谝淮问褂弥鞍惭bvioplot包,vioplot()函數(shù)的使用格式為:

 vioplot(x1, x2, ... , names=, col=) 

其中x1, x2, …表示要繪制的一個(gè)或多個(gè)數(shù)值向量(將為每個(gè)向量繪制一幅小提琴圖)。參數(shù)names是小提琴圖中標(biāo)簽的字符向量,而col是一個(gè)為每幅小提琴圖指定顏色的向量

x1 <- mtcars$mpg[mtcars$cyl==4] #是一個(gè)向量,包含mpg與所有cy1為4的值
x2 <- mtcars$mpg[mtcars$cyl==6]
x3 <- mtcars$mpg[mtcars$cyl==8]
vioplot(x1, x2, x3, 
        names=c("4 cyl", "6 cyl", "8 cyl"), 
        col="gold")
title("Violin Plots of Miles Per Gallon")

9、點(diǎn)圖與散點(diǎn)圖

點(diǎn)圖提供了一種在簡單水平刻度上繪制大量有標(biāo)簽值的方法。你可以使用dotchart()函數(shù)創(chuàng)建點(diǎn)圖,格式為:

 dotchart(x, labels=)

其中的x是一個(gè)數(shù)值向量,而labels則是由每個(gè)點(diǎn)的標(biāo)簽組成的向量。你可以通過添加參數(shù)groups來選定一個(gè)因子,用以指定x中元素的分組方式。如果這樣做,則參數(shù)gcolor可以控制不同組標(biāo)簽的顏色, cex可以控制標(biāo)簽的大小

dotchart(mtcars$mpg,labels=row.names(mtcars),cex=.7, #每加侖跑的里程
         main="Gas Mileage for Car Models", 
         xlab="Miles Per Gallon")
x <- mtcars[order(mtcars$mpg),]                      
x$cyl <- factor(x$cyl)                                 
x$color[x$cyl==4] <- "red"                              
x$color[x$cyl==6] <- "blue"
x$color[x$cyl==8] <- "darkgreen" 
dotchart(x$mpg,
         labels = row.names(x),                               
         cex=.7, 
         pch=19,                                              
         groups = x$cyl,                                       
         gcolor = "black",
         color = x$color,
         main = "Gas Mileage for Car Models\ngrouped by cylinder",
         xlab = "Miles Per Gallon")

R中創(chuàng)建散點(diǎn)圖的基礎(chǔ)函數(shù)是plot(x, y),其中, x和y是數(shù)值型向量,代表著圖形的(x, y)點(diǎn)

# install.packages(c("car", "scatterplot3d", "gclus", "hexbin", "IDPmisc", "Hmisc",  #
#                    "corrgram", "vcd", "rld"))  

par(ask=TRUE)
opar <- par(no.readonly=TRUE) # record current settings

# Listing 11.1 - A scatter plot with best fit lines
attach(mtcars)                                                     
plot(wt, mpg, 
     main="Basic Scatterplot of MPG vs. Weight",       
     xlab="Car Weight (lbs/1000)", 
     ylab="Miles Per Gallon ", pch=19)
abline(lm(mpg ~ wt), col="red", lwd=2, lty=1)            
lines(lowess(wt, mpg), col="blue", lwd=2, lty=2)
detach(mtcars)

10、折線圖

如果將散點(diǎn)圖上的點(diǎn)從左往右連接起來,就會(huì)得到一個(gè)折線圖。以基礎(chǔ)安裝中的Orange數(shù)據(jù)集為例,它包含五種橘樹的樹齡和年輪數(shù)據(jù)

library(car) 
scatterplot(mpg ~ wt | cyl, data=mtcars, lwd=2,
            main="Scatter Plot of MPG vs. Weight by # Cylinders", 
            xlab="Weight of Car (lbs/1000)", 
            ylab="Miles Per Gallon", id.method="identify",
            legend.plot=TRUE, labels=row.names(mtcars), 
            boxplots="xy")

# Listing 11.2 - Creating side by side scatter and line plots
opar <- par(no.readonly=TRUE)
par(mfrow=c(1,2)) #設(shè)置畫布,一行兩列
t1 <- subset(Orange, Tree==1) #自帶的數(shù)據(jù)集
plot(t1$age, t1$circumference,
     xlab="Age (days)",
     ylab="Circumference (mm)",
     main="Orange Tree 1 Growth")
plot(t1$age, t1$circumference,
     xlab="Age (days)",
     ylab="Circumference (mm)",
     main="Orange Tree 1 Growth",
     type="b")
par(opar)

t | cyl, data=mtcars, lwd=2,
main=“Scatter Plot of MPG vs. Weight by # Cylinders”,
xlab=“Weight of Car (lbs/1000)”,
ylab=“Miles Per Gallon”, id.method=“identify”,
legend.plot=TRUE, labels=row.names(mtcars),
boxplots=“xy”)

Listing 11.2 - Creating side by side scatter and line plots

opar <- par(no.readonly=TRUE)
par(mfrow=c(1,2)) #設(shè)置畫布,一行兩列
t1 <- subset(Orange, Tree==1) #自帶的數(shù)據(jù)集
plot(t1 a g e , t 1 age, t1 age,t1circumference,
xlab=“Age (days)”,
ylab=“Circumference (mm)”,
main=“Orange Tree 1 Growth”)
plot(t1 a g e , t 1 age, t1 age,t1circumference,
xlab=“Age (days)”,
ylab=“Circumference (mm)”,
main=“Orange Tree 1 Growth”,
type=“b”)
par(opar)

總結(jié) 

到此這篇關(guān)于R語言繪圖基礎(chǔ)教程的文章就介紹到這了,更多相關(guān)R語言繪圖教程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • R語言編程學(xué)習(xí)從Github上安裝包解決網(wǎng)絡(luò)問題

    R語言編程學(xué)習(xí)從Github上安裝包解決網(wǎng)絡(luò)問題

    這篇文章主要為大家介紹了R語言編程從Github上安裝包的過程詳解,這樣可以解決很多網(wǎng)絡(luò)問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • ComplexHeatmap繪制單個(gè)熱圖

    ComplexHeatmap繪制單個(gè)熱圖

    這篇文章主要為大家介紹了ComplexHeatmap繪制單個(gè)熱圖示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • R語言-如何將list轉(zhuǎn)換為向量

    R語言-如何將list轉(zhuǎn)換為向量

    這篇文章主要介紹了R語言-將list轉(zhuǎn)換為向量的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • R語言正態(tài)分布的實(shí)現(xiàn)示例

    R語言正態(tài)分布的實(shí)現(xiàn)示例

    R語言中正態(tài)分布包括四個(gè)主要函數(shù):rnorm、dnorm、pnorm、qnorm,分別用于生成隨機(jī)數(shù)、計(jì)算概率密度、累積概率和計(jì)算分位數(shù),本文就來詳細(xì)的介紹一下具體用法,感興趣的可以了解一下
    2024-10-10
  • R語言-如何按照某一列分組求均值

    R語言-如何按照某一列分組求均值

    這篇文章主要介紹了R語言實(shí)現(xiàn)按照某一列分組求均值的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • R語言繪制Vonoroi圖的完整代碼

    R語言繪制Vonoroi圖的完整代碼

    今天來給大家分享一篇教程關(guān)于R語言繪制Vonoroi圖的完整代碼,包括deldir包繪制Voronoi圖,ggplot2繪制Voronoi圖的實(shí)現(xiàn)代碼,感興趣的朋友跟隨小編一起看看吧
    2021-06-06
  • R語言繪圖數(shù)據(jù)可視化pie?chart餅圖

    R語言繪圖數(shù)據(jù)可視化pie?chart餅圖

    這篇文章主要介紹了R語言繪圖數(shù)據(jù)可視化pie?chart餅圖,教大家如何用R語言來畫大餅,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-02-02
  • 詳解R語言中的PCA分析與可視化

    詳解R語言中的PCA分析與可視化

    這篇文章主要介紹了R語言中的PCA分析與可視化的相關(guān)資料,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • R語言繪制頻率直方圖的案例

    R語言繪制頻率直方圖的案例

    這篇文章主要介紹了R語言繪制頻率直方圖的案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • R語言編程學(xué)習(xí)繪制動(dòng)態(tài)圖實(shí)現(xiàn)示例

    R語言編程學(xué)習(xí)繪制動(dòng)態(tài)圖實(shí)現(xiàn)示例

    這篇文章主要介紹了R語言編程學(xué)習(xí)繪制動(dòng)態(tài)圖實(shí)現(xiàn)示例,有需要的的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2021-10-10

最新評論