R語言:圖形

常用的圖形,這里給出案例:

barplot

條形圖、單向量條形圖

> attach(mtcars)
The following object is masked from package:ggplot2:

    mpg

> names(mtcars)
 [1] "mpg"   "cyl"   "disp"  "hp"    "drat"  "wt"   
 [7] "qsec"  "vs"    "am"    "gear"  "carb"  "manuf"
> barplot(table(cyl),
+         main="main",xlab="x",ylab="y")
barplot
barplot(table(cyl),horiz = TRUE)
水平的
plot(as.factor(cyl))
barplot2
plot(factor(cyl,levels = c(6,4,8)))
barplot3

堆砌與分組條形圖

counts = table(cyl,gear)
counts

   gear
cyl  3  4  5
  4  1  8  2
  6  2  4  1
  8 12  0  2

barplot(counts,
        xlab="gear",ylab="frequency",
        col = c("red","yellow","green"),
        legend=rownames(counts))
barplot4
barplot(counts,
        xlab="gear",ylab="frequency",
        col = c("red","yellow","green"),
        legend=rownames(counts),
        beside=TRUE)
barplot5

添加標(biāo)簽

legend(locator(1),title("title"),
       rownames(counts),
       lty=c(1,2),pch=c(15,17),
       col=c("red","yellow","green"))
barplot6

均值條形圖

means = aggregate(mpg,by=list(cyl),mean)
means 

 Group.1        x
1       4 26.66364
2       6 19.74286
3       8 15.10000

means = means[order(means$x,decreasing = TRUE),]
means

Group.1        x
1       4 26.66364
2       6 19.74286
3       8 15.10000

barplot(means$x,names.arg=means$Group.1)
title("Mean Rate")
lines(means$x,)
barplot7
opar = par(no.readonly=TRUE)
par(mar=c(5,8,4,2))
par(las=1)
counts = table(cyl)
barplot(counts,
        main="mtcars cyl",
        horiz=TRUE,
        cex.names=2,
        names.arg=names(table(cyl)))
par(opar)
barplot8

棘狀圖

library(vcd)
counts = table(cyl,gear)
counts

  gear
cyl  3  4  5
  4  1  8  2
  6  2  4  1
  8 12  0  2

spine(counts,main="Spinogram Example")

棘狀圖

餅圖

par(mfrow=c(2,2))
slices = c(10,12.4,16,8)
labels = c("1","2","3","4")
pie(slices,labels)

餅圖_1
pct = round(slices/sum(slices)*100)
pct

[1] 22 27 34 17
labels = paste(labels," ",pct,"%",sep="")
labels

[1] "1 22%" "2 27%" "3 34%" "4 17%"

pie(slices,labels,col=rainbow(length(pct)))
餅圖_2
library(plotrix)
pie3D(slices,labels=labels,explode=0.1)
3D_餅圖
counts = table(cyl)
counts

cyl
 4  6  8 
11  7 14 
lbls = paste(names(counts),"\n",counts,sep="")
lbls

[1] "4\n11" "6\n7"  "8\n14"
pie(counts,labels=lbls)
par(opar)
餅圖2

扇形圖

library(plotrix)
fan.plot(slices,labels=labels)
par(opar)
扇形圖

直方圖

par(mfrow=c(2,2))
#1
hist(mpg)
#2 add breaks and color
hist(mpg,
     breaks=12,
     col="red",
)
#3 add jitter, plot according to density rather than frequency
hist(mpg,
     breaks=12,
     col="red",
     freq=FALSE 
)
rug(jitter(mpg))
lines(density(mpg),col="blue",lwd=2)
#4 add norm curve and framework
x = mpg
h = hist(mpg,
         breaks=12,
         col="red", 
)
xfit = seq(min(x),max(x),length=40)
yfit = dnorm(xfit,mean=mean(x),sd=sd(x))
yfit1 = yfit*diff(h$mids[1:2])*length(x)
lines(xfit,yfit1,col="blue",lwd=2)
box()
par(opar)
hist

核密度圖

plot(density(mpg))
密度圖_1
par(mfrow=c(2,1))
d = density(mpg)
plot(d)
plot(d)
polygon(d,col="red",border="blue")
rug(mpg,col="brown")
par(opar)
密度圖_2
par(lwd=2)
library(sm)
cyl.f = factor(cyl,
               levels=c(4,6,8),
               labels=c("4cyl","6cyl","8cyl"))
sm.density.compare(mpg,cyl,
                   xlab="miles per gallon")
colfill = c(2:(1+length(levels(cyl.f))))
legend(locator(1),levels(cyl.f),fill=colfill)
legend(locator(1),levels(cyl.f),fill=rainbow(3))
密度圖_3

箱線圖

boxplot(mpg)
boxplot_1
boxplot(mpg~cyl,data=mtcars)
boxplot_2
boxplot(mpg~cyl,data=mtcars,
        varwidth=TRUE,
        notch=TRUE)
boxplot_3
cyl.f = factor(cyl,levels=c(4,6,8))
am.f  = factor(am,levels=c(0,1),labels=c("auto","stand"))
boxplot(mpg~am.f*cyl.f,
        varwidth=TRUE,
        col=c("gold","darkgreen"))
boxplot_4

小提琴圖

library(vioplot)
vioplot(mpg[cyl==4],mpg[cyl==6],mpg[cyl==8],col="gold",
        names=c("4","6","8"))
title("main",xlib="x",ylab="y")
提琴圖

點圖

dotchart(mpg,labels=rownames(mtcars),cex=0.5)
點圖_1
x = mtcars[order(mtcars$mpg),]
x

                     mpg cyl  disp  hp drat    wt  qsec
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90
Valiant             18.1   6 225.0 105 2.76 3.460 20.22
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90
                    vs am gear carb    manuf
Cadillac Fleetwood   0  0    3    4 Cadillac
Lincoln Continental  0  0    3    4  Lincoln
Camaro Z28           0  0    3    4   Camaro
Duster 360           0  0    3    4   Duster
Chrysler Imperial    0  0    3    4 Chrysler
Maserati Bora        0  1    5    8 Maserati
Merc 450SLC          0  0    3    3     Merc
AMC Javelin          0  0    3    2      AMC
Dodge Challenger     0  0    3    2    Dodge
Ford Pantera L       0  1    5    4     Ford
Merc 450SE           0  0    3    3     Merc
Merc 450SL           0  0    3    3     Merc
Merc 280C            1  0    4    4     Merc
Valiant              1  0    3    1  Valiant
Hornet Sportabout    0  0    3    2   Hornet
Merc 280             1  0    4    4     Merc
Pontiac Firebird     0  0    3    2  Pontiac
Ferrari Dino         0  1    5    6  Ferrari
Mazda RX4            0  1    4    4    Mazda
Mazda RX4 Wag        0  1    4    4    Mazda
Hornet 4 Drive       1  0    3    1   Hornet
Volvo 142E           1  1    4    2    Volvo
Toyota Corona        1  0    3    1   Toyota
Datsun 710           1  1    4    1   Datsun
Merc 230             1  0    4    2     Merc
Merc 240D            1  0    4    2     Merc
Porsche 914-2        0  1    5    2  Porsche
Fiat X1-9            1  1    4    1     Fiat
Honda Civic          1  1    4    2    Honda
Lotus Europa         1  1    5    2    Lotus
Fiat 128             1  1    4    1     Fiat
Toyota Corolla       1  1    4    1   Toyota

x$cyl = factor(x$cyl)
x

                     mpg cyl  disp  hp drat    wt  qsec
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90
Valiant             18.1   6 225.0 105 2.76 3.460 20.22
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90
                    vs am gear carb    manuf
Cadillac Fleetwood   0  0    3    4 Cadillac
Lincoln Continental  0  0    3    4  Lincoln
Camaro Z28           0  0    3    4   Camaro
Duster 360           0  0    3    4   Duster
Chrysler Imperial    0  0    3    4 Chrysler
Maserati Bora        0  1    5    8 Maserati
Merc 450SLC          0  0    3    3     Merc
AMC Javelin          0  0    3    2      AMC
Dodge Challenger     0  0    3    2    Dodge
Ford Pantera L       0  1    5    4     Ford
Merc 450SE           0  0    3    3     Merc
Merc 450SL           0  0    3    3     Merc
Merc 280C            1  0    4    4     Merc
Valiant              1  0    3    1  Valiant
Hornet Sportabout    0  0    3    2   Hornet
Merc 280             1  0    4    4     Merc
Pontiac Firebird     0  0    3    2  Pontiac
Ferrari Dino         0  1    5    6  Ferrari
Mazda RX4            0  1    4    4    Mazda
Mazda RX4 Wag        0  1    4    4    Mazda
Hornet 4 Drive       1  0    3    1   Hornet
Volvo 142E           1  1    4    2    Volvo
Toyota Corona        1  0    3    1   Toyota
Datsun 710           1  1    4    1   Datsun
Merc 230             1  0    4    2     Merc
Merc 240D            1  0    4    2     Merc
Porsche 914-2        0  1    5    2  Porsche
Fiat X1-9            1  1    4    1     Fiat
Honda Civic          1  1    4    2    Honda
Lotus Europa         1  1    5    2    Lotus
Fiat 128             1  1    4    1     Fiat
Toyota Corolla       1  1    4    1   Toyota

x$color = ifelse(x$cyl==4,"red",
                 ifelse(x$cyl==6,"blue","darkgreen"))
x

Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90
Valiant             18.1   6 225.0 105 2.76 3.460 20.22
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90
                    vs am gear carb    manuf     color
Cadillac Fleetwood   0  0    3    4 Cadillac darkgreen
Lincoln Continental  0  0    3    4  Lincoln darkgreen
Camaro Z28           0  0    3    4   Camaro darkgreen
Duster 360           0  0    3    4   Duster darkgreen
Chrysler Imperial    0  0    3    4 Chrysler darkgreen
Maserati Bora        0  1    5    8 Maserati darkgreen
Merc 450SLC          0  0    3    3     Merc darkgreen
AMC Javelin          0  0    3    2      AMC darkgreen
Dodge Challenger     0  0    3    2    Dodge darkgreen
Ford Pantera L       0  1    5    4     Ford darkgreen
Merc 450SE           0  0    3    3     Merc darkgreen
Merc 450SL           0  0    3    3     Merc darkgreen
Merc 280C            1  0    4    4     Merc      blue
Valiant              1  0    3    1  Valiant      blue
Hornet Sportabout    0  0    3    2   Hornet darkgreen
Merc 280             1  0    4    4     Merc      blue
Pontiac Firebird     0  0    3    2  Pontiac darkgreen
Ferrari Dino         0  1    5    6  Ferrari      blue
Mazda RX4            0  1    4    4    Mazda      blue
Mazda RX4 Wag        0  1    4    4    Mazda      blue
Hornet 4 Drive       1  0    3    1   Hornet      blue
Volvo 142E           1  1    4    2    Volvo       red
Toyota Corona        1  0    3    1   Toyota       red
Datsun 710           1  1    4    1   Datsun       red
Merc 230             1  0    4    2     Merc       red
Merc 240D            1  0    4    2     Merc       red
Porsche 914-2        0  1    5    2  Porsche       red
Fiat X1-9            1  1    4    1     Fiat       red
Honda Civic          1  1    4    2    Honda       red
Lotus Europa         1  1    5    2    Lotus       red
Fiat 128             1  1    4    1     Fiat       red
Toyota Corolla       1  1    4    1   Toyota       red

dotchart(x$mpg,
         labels=rownames(x),cex=0.5,
         groups=x$cyl,color=x$color,
         pch=19)
點圖_2
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末特恬,一起剝皮案震驚了整個濱河市幅疼,隨后出現(xiàn)的幾起案子凹嘲,更是在濱河造成了極大的恐慌,老刑警劉巖悔叽,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件莱衩,死亡現(xiàn)場離奇詭異,居然都是意外死亡娇澎,警方通過查閱死者的電腦和手機笨蚁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來九火,“玉大人赚窃,你說我怎么就攤上這事〔砑ぃ” “怎么了勒极?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵,是天一觀的道長虑鼎。 經(jīng)常有香客問我辱匿,道長键痛,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任匾七,我火速辦了婚禮絮短,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘昨忆。我一直安慰自己丁频,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布邑贴。 她就那樣靜靜地躺著席里,像睡著了一般。 火紅的嫁衣襯著肌膚如雪拢驾。 梳的紋絲不亂的頭發(fā)上奖磁,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天,我揣著相機與錄音繁疤,去河邊找鬼咖为。 笑死,一個胖子當(dāng)著我的面吹牛稠腊,可吹牛的內(nèi)容都是我干的躁染。 我是一名探鬼主播,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼麻养,長吁一口氣:“原來是場噩夢啊……” “哼褐啡!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起鳖昌,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤备畦,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后许昨,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體懂盐,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年糕档,在試婚紗的時候發(fā)現(xiàn)自己被綠了莉恼。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡速那,死狀恐怖俐银,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情端仰,我是刑警寧澤捶惜,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布,位于F島的核電站荔烧,受9級特大地震影響吱七,放射性物質(zhì)發(fā)生泄漏汽久。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一踊餐、第九天 我趴在偏房一處隱蔽的房頂上張望景醇。 院中可真熱鬧,春花似錦吝岭、人聲如沸三痰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽酒觅。三九已至,卻和暖如春微峰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背抒钱。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工蜓肆, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人谋币。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓仗扬,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蕾额。 傳聞我的和親對象是個殘疾皇子早芭,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內(nèi)容