《R语言实战》自学笔记16-图形图例

Python035

《R语言实战》自学笔记16-图形图例,第1张

数据准备

函数:legend(location, title, legend, ....)

参数详解:

x和y:用于定位图例,也可用关键词"bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" 和 "center";当图例用关键词设置位置后,inset = 分数,可以设置其相对位置;

legend:指定图例标签,字符或表达式向量;

fill:用特定的颜色进行填充;

col:设置图例中出现的点或线的颜色;

border:当fill = 参数存在的情况下,用于指定填充的边框颜色;

lty, lwd:图例中线的类型与宽度;

pch:点的类型;

angle:阴影的角度;

density:阴影线的密度;

cex:指定图例显示大小;

bg:指定图例的背景色;

bty:指定图例框是否画出,默认o为画出,n为不画出;

box.lty, box.lwd, box.col: 设置图例边框线型,线粗,颜色,box.lty为虚线,box.lwd决定粗线,box.col决定颜色;

pt.bg:图例中点的背景色;

pt.cex:图例中点的大小;

pt.lwd:图例中点边缘的线宽;

x.intersp:图例中文字离图片的水平距离;

y.intersp:图例中文字离图片的垂直距离;

adj:图例中字体的相对位置;

text.width:图例中字体所占的宽度,调整后图例整个宽度也跟着变化了;

text.col:图例字体的颜色;

text.font:图例字体;

merge:逻辑值,merge=TRUE,合并点与线,但不填充图例框,默认为TRUE;

trace:逻辑值,trace=TRUE显示图例信息;

plot:逻辑值,plot=FALSE不画出图例;

ncol:图例中分类的列数;

horiz:逻辑值,horiz=TRUE,水平放置图例;

title:给图例加标题;

xpd:xpd=FALSE,即不允许在作图区域外作图,改为TRUE即可,与par()参数配合使用;

title.col:标题颜色;

title.adj:图例标题的相对位置,0.5为默认,在中间。0最左,1为最右;

seg.len:指定图例中线的线长,长度单位为字符宽度。

1 图例方位

2 修饰图例

3 图例绘制在图外

4 自定义图例

有时候绘制出的图是分组图,这时候需要自定义绘制图例。

参考资料:

《R语言实战》(中文版),人民邮电出版社,2013.

R语言绘制图例(legend)的各种问题_详细综合解析, https://blog.csdn.net/xiangyong58/article/details/54579293

投必得R语言教程,第二讲 R作图-基础-图形参数设置:标题、图例、文字, https://mp.weixin.qq.com/s?__biz=MzU1Mzc3OTIwNg==&mid=2247495531&idx=1&sn=cdd80d4e950ae2b344cf188c68922fa8&chksm=fbef0602cc988f14e93b71bc6fcc2fda782d3ae9a3a67601a6c87756f4ae85bcc5d9c56d9b51&scene=21#wechat_redirect

一幅图解决R语言绘制图例的各种问题, https://blog.csdn.net/weixin_30469895/article/details/96649305

R语言绘图系列:

使用geom_label绘制标签散点图

绘制点,并通过nudge参数对标签进行x轴和y轴上的平移

使用angle参数对标签角度进行设置

geom_label可以使用fill对颜色进行填充,fontface设置字体,geom_text不能填充颜色

parse参数意思是前面传入的是一个数学表达式,size定义标签相对大小。

画一个散点图

annotate函数传入标签

添加矩形

添加短线段

2.1 guide_legend函数(主要参数:color, shape, size)

图例调整函数也属于标度函数的一类,但不可以直接使用加号来连接,必须放在函数中,作为一个参数。

guide_colorbar和guide_legend设置的是不同的图例,guide_colorbar定义色条图例,guide_legend定义普通图例。

2.2 标度函数scale

对于连续型变量,使用的参数是scale_xxx_continous(),对于分类型变量,使用的是scale_xxx_discrete()。

2.3:theme函数

在theme函数中,与图例有关的主要参数有:

标题主要有五种:主标题,副标题,角注,x轴标签和y轴标签

ggtitle()只能定义标题和副标题,默认的位置在左上角。

1. 如何将barplot图例做到绘图区外.txt

下面的代码包括了折线图和直方图的实现: 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