目录
type 修改图形类型
颜色类参数: 设置图形各部分颜色的参数:
点/线类型参数:
大小类参数:
文本参数:
总结:
柱状图
plot( )函数:
library(Ecdat) data(Gasoline) head(Gasoline,10)
## country year lgaspcar lincomep lrpmg lcarpcap
## 1 AUSTRIA 1960 4.173244 -6.474277 -0.3345476 -9.766840
## 2 AUSTRIA 1961 4.100989 -6.426006 -0.3513276 -9.608622
## 3 AUSTRIA 1962 4.073177 -6.407308 -0.3795177 -9.457257
## 4 AUSTRIA 1963 4.059509 -6.370679 -0.4142514 -9.343155
## 5 AUSTRIA 1964 4.037689 -6.322247 -0.4453354 -9.237739
## 6 AUSTRIA 1965 4.033983 -6.294668 -0.4970607 -9.123903
## 7 AUSTRIA 1966 4.047537 -6.252545 -0.4668377 -9.019822
## 8 AUSTRIA 1967 4.052911 -6.234581 -0.5058834 -8.934403
## 9 AUSTRIA 1968 4.045507 -6.206894 -0.5224125 -8.847967
## 10 AUSTRIA 1969 4.046355 -6.153140 -0.5591105 -8.788686
x<-Gasoline$year[Gasoline$country=="AUSTRIA"]
y<-Gasoline$lcarpcap[Gasoline$country=="AUSTRIA"] plot(x,y,type="o")
数值指定颜色
在散点图中bg参数修改的是实心点的填充颜色,形状是空心的点是填充不了的。
colors( )函数可查看所有颜色名
head(colors())
## [1] "white" "aliceblue" "antiquewhite" "antiquewhite1"
## [5] "antiquewhite2" "antiquewhite3"
在实际中可以把颜色/形状/大小等参数设置成向量,向量所指定的参数就会交替出现。
点的形状:
①通过修改pch参数 , 其中15-25为实心,但21-25填充颜色可修改
plot(x,y,pch=23,bg=2)
plot(x,y,pch=18,bg=2)
②通过pch指定符号
plot(x,y,pch="A")
线的形状: lty参数:
plot()中设置:
plot(x,y,main = "主标题",sub="副标题")
单独文本设置:
- title(main= ,sub= ,xlab= ,ylab= )
plot(x,y,xlab="",ylab="")#要把xlab\ylab设置为空,否则plot会自动生成标签与title设置的重叠在一起 title(main = "主标题",sub="副标题",xlab="横坐标名称",ylab="纵坐标名称")
- text(x,y,‘添加的文本’,col= )---在任意位置添加文本 x、y为添加文本的位置坐标
plot(x,y) text(1965,-9,"我想加的文本",col=2)
- mtext(“我想加的文本”,side= ,line= )在图形留白区域加文本
side=1,2,3,4对应下,左,上,右
line文本距离图形边框的的距离
plot(x,y) mtext("我想加的文本",side=c(1,2,3,4),line = c(1,1,1,1))
- legend(x,y,legend,……)添加图例
plot(x,y,pch=23,bg=2)
z<-Gasoline$lcarpcap[Gasoline$country=="BELGIUM"] points(x,z,pch=23,bg=3)#在原图上添加数据点 legend("topleft",c("BELGIUM","AUSTRIA"),pch = 23,col=c(3,2))
- par( )函数,在该函数中设置的参数对之后的所有图形都适用
-
总结:
library(Ecdat) data(Gasoline) head(Gasoline,10)
## country year lgaspcar lincomep lrpmg lcarpcap
## 1 AUSTRIA 1960 4.173244 -6.474277 -0.3345476 -9.766840
## 2 AUSTRIA 1961 4.100989 -6.426006 -0.3513276 -9.608622
## 3 AUSTRIA 1962 4.073177 -6.407308 -0.3795177 -9.457257
## 4 AUSTRIA 1963 4.059509 -6.370679 -0.4142514 -9.343155
## 5 AUSTRIA 1964 4.037689 -6.322247 -0.4453354 -9.237739
## 6 AUSTRIA 1965 4.033983 -6.294668 -0.4970607 -9.123903
## 7 AUSTRIA 1966 4.047537 -6.252545 -0.4668377 -9.019822
## 8 AUSTRIA 1967 4.052911 -6.234581 -0.5058834 -8.934403
## 9 AUSTRIA 1968 4.045507 -6.206894 -0.5224125 -8.847967
## 10 AUSTRIA 1969 4.046355 -6.153140 -0.5591105 -8.788686
x<-Gasoline$year[Gasoline$country=="AUSTRIA"]
y<-Gasoline$lcarpcap[Gasoline$country=="AUSTRIA"]
z<-Gasoline$lcarpcap[Gasoline$country=="BELGIUM"] plot(x,y,pch=23,bg=2,xlab = "",ylab="") points(x,z,pch=23,bg=3)#在原图上添加数据点 title(main = "主标题在这儿",sub = "副标题在这儿",xlab = "横坐标标签",ylab="纵坐标标签") text(1970,-9,"我想在这儿加文本",col="red") mtext("我要在任意位置加文本",side=3,line = 1.5,col = "blue",cex=0.65) legend("topleft",c("BELGIUM","AUSTRIA"),pch = 18,col=c(3,2))#这时的pch参数要改成实心黑的哦,如果仍为23则在图例显示是空心的
-
柱状图
- plot( )函数绘制: plot()函数中,当输入变量是属性变量时,就会绘制柱状图
library(Ecdat) data("FriendFoe") head(FriendFoe)
## sex white age play round season cash sex1 white1 age1 play1 win
## 1 male yes 20 foe 1 1 1.2 male yes 32 friend 1.2
## 2 male yes 40 foe 3 1 7.7 female yes 31 foe 0.0
## 3 female no 35 foe 2 1 3.2 female no 24 foe 0.0
## 4 male yes 26 friend 1 1 1.2 male yes 40 friend 0.6
## 5 female yes 40 friend 3 1 5.7 male yes 26 foe 0.0
## 6 female yes 28 foe 2 1 3.7 female yes 23 friend 3.7
## win1
## 1 0.0
## 2 0.0
## 3 0.0
## 4 0.6
## 5 5.7
## 6 0.0
plot(FriendFoe$round)
- barplot( )绘制: barplot(x,width= ,names.arg= ,legend.text= ,beside= ,horiz= )
x可以是单变量,多变量(表格形式)
width决定柱形宽度 names.arg=x轴上每个柱子下面的标签
legend.text添加图例的文本
beside:=T柱子相邻展示,=F堆积展示
horiz:=T柱子横向,=F纵向
barplot(table(FriendFoe$sex,FriendFoe$round),names.arg = c("one","two","three"),legend.text = c("female","male"),beside = TRUE,horiz = F,col = c("red","blue"))
对于条形图/直方图/箱线图等后面专门学习用ggplot包就可以了,用plot()函数做不美观还麻烦
|
请发表评论