忽闻高中一同学已经拿到了微软的实习offer,即刻以为本身的大学越发失败了。做错了选择,然后未能在大学几年里做到当真专注,导致了我此刻的失败。前几天看到搜狗王小川的一篇报道,他就是那种数学很好,然后糊口一切都很顺的人。我碰着过许多这样的同学,数学都很好,然后进修也很轻松顺利。我大学之前后果固然一直都还不错,但我却不属于他们中的那种人,我需要支付更大的尽力,才气取得和他们一样的后果。人在智商和命运上的不同,有时候真的是让人很无奈的。
照旧继承进修R Graph Cookbook第五章吧。不知道十月份谋事情的时候,R语言对我是否有点儿用处,可是此刻我已经无路可走了,唯有把科学计较和语义网这两块学好一些,但愿在接下来的几个月,做多一点对象,找到一份好的事情。
首先要先容的是画条形图,条形图的用图很广,普通的陈诉内里用的最多的或许就是条形图了。代码和图形如下:
library(RColorBrewer)
citysales < – read.csv( “citysales.csv” )
barplot(as.matrix(citysales[, 2 : 4 ]),beside = TRUE,
legend.text = citysales$City,
args.legend = list (bty = “n” ,horiz = TRUE),
col = brewer.pal( 5 , “Set1” ),
border = “white” ,ylim = c( 0 , 100 ),
ylab = “Sales Revenue (1,000’s of USD)” ,
main = “Sales Figures” )
box(bty = “l” )
|
这里用到了RColorBrewer包,是一个颜色包,Set1就是个中的一组颜色。
把上面的beside参数去掉可能改为FALSE,就可以绘制出栈形的条形图了,如下:
上面的栈还不太好,我们还可以用左边的Y轴显示百分比,让数据比拟越发得清晰。代码如下:
library(RColorBrewer)
citysales < – read.csv( “citysales.csv” )
barplot(as.matrix(citysales[, 2 : 4 ]),beside = TRUE,
legend.text = citysales$City,
args.legend = list (bty = “n” ,horiz = TRUE),
col = brewer.pal( 5 , “Set1” ),
border = “white” ,ylim = c( 0 , 100 ),
ylab = “Sales Revenue (1,000’s of USD)” ,
main = “Sales Figures” )
box(bty = “l” )
|
这里用到了RColorBrewer包,是一个颜色包,Set1就是个中的一组颜色。
把上面的beside参数去掉可能改为FALSE,就可以绘制出栈形的条形图了,如下:
上面的栈还不太好,我们还可以用左边的Y轴显示百分比,让数据比拟越发得清晰。代码如下:
png( “a.png” )
library(RColorBrewer)
citysales < – read.csv( “citysales.csv” )
barplot(as.matrix(citysales[, 2 : 4 ]), beside = TRUE,horiz = TRUE,
legend.text = citysales$City, args.legend = list (bty = “n” ),
col = brewer.pal( 5 , “Set1” ),border = “white” ,
xlim = c( 0 , 100 ), xlab = “Sales Revenue (1,000’s of USD)” ,
main = “Sales Figures” )
dev.off()
|
假如我们需要知道每一个条形的精确数量是何等该怎么办,请看下面的代码:
png( “a.png” )
library(RColorBrewer)
citysales < – read.csv( “citysales.csv” )
x < – barplot(as.matrix(citysales[, 2 : 4 ]),beside = TRUE,
legend.text = citysales$City,args.legend = list (bty = “n” ,horiz = TRUE),
col = brewer.pal( 5 , “Set1” ),border = “white” ,
ylim = c( 0 , 100 ),ylab = “Sales Revenue (1,000’s of USD)” ,
main = “Sales Figures” )
y < – as.matrix(citysales[, 2 : 4 ])
text(x,y + 2 ,labels = as.character(y))
dev.off()
|
添加误差线也是很重要的事情,下面是添加误差线的代码和图像:
png( “a.png” )
library(RColorBrewer)
citysales < – read.csv( “citysales.csv” )
sales< – t(as.matrix(citysales[, – 1 ]))
colnames(sales)< – citysales[, 1 ]
x< – barplot(sales,beside = T,legend.text = rownames(sales),
args.legend = list (bty = “n” ,horiz = T),
col = brewer.pal( 3 , “Set2” ),border = “white” ,ylim = c( 0 , 100 ),
ylab = “Sales Revenue (1,000’s of USD)” ,
main = “Sales Figures” )
arrows(x0 = x,
y0 = sales * 0.95 ,
x1 = x,
y1 = sales * 1.05 ,
angle = 90 ,
code = 3 ,
length = 0.04 ,
lwd = 0.4 )
dev.off()
|
下面说的是绘制点图,我也不是很懂,照旧直接把代码放上了吧,其它的不管了:
library(reshape)
citysales < – read.csv( “citysales.csv” )
sales < – melt(citysales)
sales$color[sales[, 2 ] = = “ProductA” ] < – “red”
sales$color[sales[, 2 ] = = “ProductB” ] < – “blue”
sales$color[sales[, 2 ] = = “ProductC” ] < – “violet”
dotchart(sales[, 3 ],labels = sales$City,groups = sales[, 2 ],
col = sales$color,pch = 19 ,
main = “Sales Figures” ,
xlab = “Sales Revenue (1,000’s of USD)” )
|
下面说说争议较量大的饼图的画法,饼图在科学规模很罕用,因为它容易发生视觉误差,在贸易规模用的倒是许多许多,下面是代码:
library(reshape)
citysales < – read.csv( “citysales.csv” )
sales < – melt(citysales)
sales$color[sales[, 2 ] = = “ProductA” ] < – “red”
sales$color[sales[, 2 ] = = “ProductB” ] < – “blue”
sales$color[sales[, 2 ] = = “ProductC” ] < – “violet”
dotchart(sales[, 3 ],labels = sales$City,groups = sales[, 2 ],
col = sales$color,pch = 19 ,
main = “Sales Figures” ,
xlab = “Sales Revenue (1,000’s of USD)” )
|
下面说说争议较量大的饼图的画法,饼图在科学规模很罕用,因为它容易发生视觉误差,在贸易规模用的倒是许多许多,下面是代码:
png( “a.png” )
browers < – read.table( “browsers.txt” ,header = TRUE)
browers < – browers[order(browers[, 2 ]),]
pielabels < – sprintf( “%s = %3.1f%s” ,browers[, 1 ],
100 * browers[, 2 ] / sum (browers[, 2 ]), “%” )
pie(browers[, 2 ],
labels = pielabels,
clockwise = TRUE,
radius = 1 ,
col = brewer.pal( 7 , “Set1” ),
border = “white” ,
cex = 0.8 ,
main = “Percentage share of internet browser usage” )
dev.off()
|
这样就更好一些了。可是百分比和名称重叠了,也欠好,下面用更好的方法:
png( “a.png” )
browers < – read.table( “browsers.txt” ,header = TRUE)
browers < – browers[order(browers[, 2 ]),]
pielabels < – sprintf( “%s = %3.1f%s” ,browers[, 1 ],
100 * browers[, 2 ] / sum (browers[, 2 ]), “%” )
pie(browers[, 2 ],
labels = NA,
clockwise = TRUE,
radius = 0.7 ,
col = brewer.pal( 7 , “Set1” ),
border = “white” ,
cex = 0.8 ,
main = “Percentage share of internet browser usage” )
legend( “bottomright” ,legend = pielabels,bty = “n” ,
fill = brewer.pal( 7 , “Set1” ))
dev.off()
|