
下面的代码包括了折线图和直方图的实现: 1、.h文件:#import #import 散点图的数据点数: 20#define num 20@interface BarChartViewController : UIViewController {@privateCPXYGraph * graph double x [ num ] ; 散点的 x 坐标 double y1 [ num ] ; 第 1 个散点图的 y 坐标double y2 [ num ]; 第 2 个散点图的 y 坐标} @end2、.m文件:#import "BarChartViewController.h"@implementation BarChartViewController-( BOOL )shouldAutorotateToInterfaceOrientation:( UIInterfaceOrientation )toInterfaceOrientation{return YES }#pragma mark -#pragma mark Initialization and teardown -( void )viewDidAppear:( BOOL )animated{ 为 CPGraph 指定主题 graph = [[ CPXYGraph alloc ] initWithFrame : CGRectZero ]CPTheme *theme = [ CPTheme themeNamed : kCPDarkGradientTheme ][ graph applyTheme :theme]; 把 self.view 由 UIView 转变为 CPGraphHostingView ,因为 UIView 无法加载 CPGraphself . view =[[ CPGraphHostingView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ]CPGraphHostingView *hostingView = ( CPGraphHostingView *) self . view [hostingView setHostedGraph : graph ]CPGraph 边框:无graph . plotAreaFrame . borderLineStyle = nil graph . plotAreaFrame . cornerRadius = 0.0f ; 绘图空间 plot space CPXYPlotSpace *plotSpace = ( CPXYPlotSpace *) graph . defaultPlotSpace ; 绘图空间大小: Y : 0-300 , x : 0-16 plotSpace. yRange = [ CPPlotRange plotRangeWithLocation : CPDecimalFromFloat ( 0.0f ) length :CPDecimalFromFloat ( 200.0f )]plotSpace. xRange = [ CPPlotRange plotRangeWithLocation : CPDecimalFromFloat ( 0.0f ) length :CPDecimalFromInt ( num )]CPGraph 四边不留白 graph . paddingLeft = 0.0f graph . paddingRight = 0.0f graph . paddingTop = 0.0f graph . paddingBottom = 0.0f ; 绘图区 4 边留白 graph . plotAreaFrame . paddingLeft = 45.0 graph . plotAreaFrame . paddingTop = 40.0 graph . plotAreaFrame . paddingRight = 5.0 graph . plotAreaFrame . paddingBottom = 80.0 ; 坐标系CPXYAxisSet *axisSet = ( CPXYAxisSet *) graph . axisSet x 轴:为坐标系的 x 轴 CPXYAxis *X = axisSet. xAxis ; 清除默认的轴标签 , 使用自定义的轴标签X. labelingPolicy = CPAxisLabelingPolicyNone ; 构造 MutableArray ,用于存放自定义的轴标签NSMutableArray *customLabels = [ NSMutableArray arrayWithCapacity : num ]; 构造一个 TextStylestatic CPTextStyle * labelTextStyle= nil labelTextStyle=[[ CPTextStyle alloc ] init ]labelTextStyle. color =[ CPColor whiteColor ]labelTextStyle. fontSize = 10.0f ; 每个数据点一个轴标签for ( int i= 0 i<num i++) {CPAxisLabel *newLabel = [[ CPAxisLabel alloc ] initWithText : [ NSString stringWithFormat : @" 第 %d 个数据点 " ,(i+ 1 )] textStyle :labelTextStyle]newLabel. tickLocation = CPDecimalFromInt (i)newLabel. offset = X. labelOffset + X. majorTickLength newLabel. rotation = M_PI / 2 [customLabels addObject :newLabel][newLabel release ]}X. axisLabels = [ NSSet setWithArray :customLabels]y 轴CPXYAxis *y = axisSet. yAxis y 轴:不显示小刻度线 y. minorTickLineStyle = nil ; 大刻度线间距: 50 单位 y. majorIntervalLength = CPDecimalFromString ( @"50" ); 坐标原点: 0 y. orthogonalCoordinateDecimal = CPDecimalFromString ( @"0" )y. titleOffset = 45.0f y. titleLocation = CPDecimalFromFloat ( 150.0f ); 第 1 个散点图:蓝色CPScatterPlot *boundLinePlot = [[[ CPScatterPlot alloc ] init ] autorelease ]id ,用于识别该散点图 boundLinePlot. identifier = @"Blue Plot" ; 线型设置 CPLineStyle * lineStyle = [[[ CPLineStyle alloc ] init ] autorelease ]lineStyle. lineWidth = 1.0f lineStyle. lineColor = [ CPColor blueColor ]boundLinePlot. dataLineStyle = lineStyle; 设置数据源 , 必须实现 CPPlotDataSource 协议 boundLinePlot. dataSource = self [ graph addPlot :boundLinePlot]; 在图形上添加一些小圆点符号(节点)CPLineStyle *symbolLineStyle = [[ CPLineStyle alloc ] init ]; 描边:黑色symbolLineStyle. lineColor = [ CPColor blackColor ]; 符号类型:椭圆CPPlotSymbol *plotSymbol = [ CPPlotSymbol ellipsePlotSymbol ]; 填充色:蓝色plotSymbol. fill = [ CPFill fillWithColor :[ CPColor blueColor ]]; 描边plotSymbol. lineStyle = symbolLineStyle; 符号大小。
2. R语言如何修改图例标签
第一步,声明三个向量amount、apple和banana,分别使用数值向量赋值,如下图所示:
第二步,调用par()函数绘制图形面板,这里设置为只读属性,如下图所示:
第三步,调用plot()函数绘制图形,设置了图形的形状、点形状、主副标题、xy轴标题和y轴数值范围等,如下图所示:
第四步,执行了plot()函数命令,可以看到一条蓝色的虚线,如下图所示:
第五步,使用lines()函数绘制另一条线,设置为绿色,pch为5,lty为4,如下图所示:
6
第六步,调用legend()函数,绘制图形图例;设置图例标题、选择线条、选择的颜色等,如下图所示:
3. 请教:如何在R中用plot画图时将刻度线标在内部
可以用matlab提供的坐标轴设置函数axis进行设置(对当前坐标轴进行设置),具体用法如下:
plot(x, y); 画图后用axis函数设置坐标轴的范围
axis([xmin xmax ymin ymax]); % 设置坐标轴在指定的区间
xmin、xmax 表示设置横坐标的最小最大值;
ymin、ymax 表示设置纵坐标的最小最大值。
也可以用下面的set函数进行设置(对指定的坐标轴进行设置),具体用法如下:
plot(x, y); 画图后用set函数设置坐标轴的属性
set(handles,'xtick',0:100:2500) % handles可以指定具体坐标轴的句柄
功能: 设置X轴坐标范围0到2500,显示间隔是100
legend(d>0&d=4)。R语言中的legend()函数用于向现有Plot添加图例。图例被定义为描述图的每个部分的图的一个区域,图例图用于以图形形式显示统计数据。
R语言常用函数整理本篇是基础篇,即R语言自带的函数。
vector:向量
numeric:数值型向量
logical:逻辑型向量
character;字符型向量
list:列表
data.frame:数据框
c:连接为向量或列表
length:求长度
subset:求子集
seq,from:to,sequence:等差序列
rep:重复
NA:缺失值
NULL:空对象
sort,order,unique,rev:排序
unlist:展平列表
attr,attributes:对象属性
mode,class,typeof:对象存储模式与类型
names:对象的名字属性
字符型向量 nchar:字符数
substr:取子串 format,formatC:把对象用格式转换为字符串
paste()、paste0()不仅可以连接多个字符串,还可以将对象自动转换为字符串再相连,另外还能处理向量。
strsplit:连接或拆分
charmatch,pmatch:字符串匹配
grep,sub,gsub:模式匹配与替换
complex,Re,Im,Mod,Arg,Conj:复数函数
factor:因子 codes:因子的编码 levels:因子的各水平的名字 nlevels:因子的水平个数 cut:把数值型对象分区间转换为因子
table:交叉频数表 split:按因子分组 aggregate:计算各数据子集的概括统计量 tapply:对“不规则”数组应用函数
dev.new() 新建画板
plot()绘制点线图,条形图,散点图.
barplot( ) 绘制条形图
dotchart( ) 绘制点图
pie( )绘制饼图.
pair( )绘制散点图阵
boxplot( )绘制箱线图
hist( )绘制直方图
scatterplot3D( )绘制3D散点图.
par()可以添加很多参数来修改图形
title( ) 添加标题
axis( ) 调整刻度
rug( ) 添加轴密度
grid( ) 添加网格线
abline( ) 添加直线
lines( ) 添加曲线
text( ) 添加标签
legend() 添加图例
+, -, *, /, ^, %%, %/%:四则运算 ceiling,floor,round,signif
1、round() #四舍五入
例:x <- c(3.1416, 15.377, 269.7)
round(x, 0) #保留整数位
round(x, 2) #保留两位小数
round(x, -1) #保留到十位
2、signif() #取有效数字(跟学过的有效数字不是一个意思)
例:略
3、trunc() #取整
floor() #向下取整
ceiling() #向上取整
例:xx <- c(3.60, 12.47, -3.60, -12.47)
trunc(xx)
floor(xx)
ceiling(xx)
max,min,pmax,pmin:最大最小值
range:最大值和最小值 sum,prod:向量元素和,积 cumsum,cumprod,cummax,cummin:累加、累乘 sort:排序 approx和approx fun:插值 diff:差分 sign:符号函数
abs,sqrt:绝对值,平方根
log, exp, log10, log2:对数与指数函数
sin,cos,tan,asin,acos,atan,atan2:三角函数
sinh,cosh,tanh,asinh,acosh,atanh:双曲函数
beta,lbeta,gamma,lgamma,digamma,trigamma,tetragamma,pentagamma,choose ,lchoose:与贝塔函数、伽玛函数、组合数有关的特殊函数
fft,mvfft,convolve:富利叶变换及卷积
polyroot:多项式求根
poly:正交多项式
spline,splinefun:样条差值
besselI,besselK,besselJ,besselY,gammaCody:Bessel函数
deriv:简单表达式的符号微分或算法微分
array:建立数组
matrix:生成矩阵
data.matrix:把数据框转换为数值型矩阵
lower.tri:矩阵的下三角部分
mat.or.vec:生成矩阵或向量
t:矩阵转置
cbind:把列合并为矩阵
rbind:把行合并为矩阵
diag:矩阵对角元素向量或生成对角矩阵
aperm:数组转置
nrow, ncol:计算数组的行数和列数
dim:对象的维向量
dimnames:对象的维名
rownames,colnames:行名或列名
%*%:矩阵乘法
crossprod:矩阵交叉乘积(内积)
outer:数组外积
kronecker:数组的Kronecker积
apply:对数组的某些维应用函数
tapply:对“不规则”数组应用函数
sweep:计算数组的概括统计量
aggregate:计算数据子集的概括统计量
scale:矩阵标准化
matplot:对矩阵各列绘图
cor:相关阵或协差阵
Contrast:对照矩阵
row:矩阵的行下标集
col:求列下标集
solve:解线性方程组或求逆
eigen:矩阵的特征值分解
svd:矩阵的奇异值分解
backsolve:解上三角或下三角方程组
chol:Choleski分解
qr:矩阵的QR分解
chol2inv:由Choleski分解求逆
><,>,<=,>=,==,!=:比较运算符 !,&,&&,|,||,xor():
逻辑运算符 logical:
生成逻辑向量 all,
any:逻辑向量都为真或存在真
ifelse():二者择一 match,
%in%:查找
unique:找出互不相同的元素
which:找到真值下标集合
duplicated:找到重复元素
optimize,uniroot,polyroot:一维优化与求根
if,else,
ifelse,
switch:
分支 for,while,repeat,break,next:
循环 apply,lapply,sapply,tapply,sweep:替代循环的函数。
function:函数定义
source:调用文件 ’
call:函数调用 .
C,.Fortran:调用C或者Fortran子程序的动态链接库。
Recall:递归调用
browser,debug,trace,traceback:程序调试
options:指定系统参数
missing:判断虚参是否有对应实参
nargs:参数个数 stop:终止函数执行
on.exit:指定退出时执行 eval,expression:表达式计算
system.time:表达式计算计时
invisible:使变量不显示
menu:选择菜单(字符列表菜单)
其它与函数有关的还有:
delay,
delete.response,
deparse,
do.call,
dput,
environment ,
formals,
format.info,
interactive,
is.finite,
is.function,
is.language,
is.recursive ,
match.arg,
match.call,
match.fun,
model.extract,
name,
parse 函数能将字符串转换为表达式expression
deparse 将表达式expression转换为字符串
eval 函数能对表达式求解
substitute,
sys.parent ,
warning,
machine
cat,print:显示对象
sink:输出转向到指定文件
dump,save,dput,write:输出对象
scan,read.table,readlines, load,dget:读入
ls,objects:显示对象列表
rm, remove:删除对象
q,quit:退出系统
.First,.Last:初始运行函数与退出运行函数。
options:系统选项
?,help,help.start,apropos:帮助功能
data:列出数据集
head()查看数据的头几行
tail()查看数据的最后几行
每一种分布有四个函数:
d―density(密度函数),p―分布函数,q―分位数函数,r―随机数函数。
比如,正态分布的这四个函数为dnorm,pnorm,qnorm,rnorm。下面我们列出各分布后缀,前面加前缀d、p、q或r就构成函数名:
norm:正态,
t:t分布,
f:F分布,
chisq:卡方(包括非中心)
unif:均匀,
exp:指数,
weibull:威布尔,
gamma:伽玛,
beta:贝塔
lnorm:对数正态,
logis:逻辑分布,
cauchy:柯西,
binom:二项分布,
geom:几何分布,
hyper:超几何,
nbinom:负二项,
pois:泊松
signrank:符号秩,
wilcox:秩和,
tukey:学生化极差
sum, mean, var, sd, min, max, range, median, IQR(四分位间距)等为统计量,
sort,order,rank与排序有关,
其它还有ave,fivenum,mad,quantile,stem等。
R中已实现的有chisq.test,prop.test,t.test。
cor,cov.wt,var:协方差阵及相关阵计算
biplot,biplot.princomp:多元数据biplot图
cancor:典则相关
princomp:主成分分析
hclust:谱系聚类
kmeans:k-均值聚类
cmdscale:经典多维标度
其它有dist,mahalanobis,cov.rob。
ts:时间序列对象
diff:计算差分
time:时间序列的采样时间
window:时间窗
lm,glm,aov:线性模型、广义线性模型、方差分析
quo()等价于quote()
enquo()等价于substitute()
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)