R语言_legend()函数用法

Python015

R语言_legend()函数用法,第1张

x, y:用于定位图例,也可用单键词"bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" and "center"

legend:字符或表达式向量

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

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

border:当fill = 参数存在的情况下,填充色的边框

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

pch:点的类型

angle:阴影的角度

density:阴影线的密度

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

bg:bty != "n"时,图例的背景色

box.lty, box.lwd, box.col

bty = "o"时,图例框的类型,box.lty决定是否为虚线,box.lwd决定粗线,box.col :决定颜色

pt.bg:点的背景色

cex:字符大小

pt.cex:点的大小

pt.lwd:点的边缘的线宽

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

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

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

text.width:图例字体所占的宽度

text.col:图例字体的颜色

text.font:图例字体

merge:logical, if TRUE,合并点与线,但不填充图例框,默认为TRUE

trace:logicalif TRUE显示图例信息.

plot:logical. If FALSE不画出图例

ncol:图例中分类的列数

horiz:logicalif TRUE,水平放置图例

title:给图例加标题

inset:当图例用关键词设置位置后,inset = 分数,可以设置其相对位置

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

title.col:标题颜色

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

seg.len:lty 与lwd的线长,长度单位为字符宽度

参考资料: R语言中Legend 函数的参数详解

cogcreategraphiclabeltool怎么显示坐标

plot()函数默认会绘制坐标轴,当需要单独设置坐标轴时,可以先将axes参数设置为FALSE,再使用axis()进行设置,语法结构如下:

axis(side, at = NULL, labels = TRUE, tick = TRUE, line = NA,

pos = NA, outer = FALSE, font = NA, lty = "solid",

lwd = 1, lwd.ticks = lwd, col = NULL, col.ticks = NULL,

hadj = NA, padj = NA, gap.axis = NA, ...)

登录后复制

side:需要添加坐标轴的位置,1-4分别用于指定底部、左侧、上侧和右侧的坐标轴;

at:需要添加刻度线的位置;

labels:设置是否添加刻度标签;

tick:设置是否显示轴线和刻度线;

line:坐标轴线与边框的行距,同mtext()的同名参数,可忽略;

pos:指定绘制坐标轴线的坐标,可忽略;

outer:为TRUE时以outer margin作为参考;

font、lty、lwd、lwd.ticks、col、col.ticks:美化参数;

hadj、padj:分别用于设置刻度标签与坐标轴线在阅读方向的水平、垂直方向上的距离;

gap.axis:设置刻度标签的最小距离;

...:par()中的相关参数。

plot(sin, 0, 2*pi, type = "l", axes = F)

axis(1, c(0, pi/2, pi, 3*pi/2, 2*pi))

axis(2, c(0, 0.5, 1), labels = F)

axis(3, seq(0, 2*pi, pi/2), tick = F)

axis(4, c(-1, -0.5, 0), line = -2, hadj = 0, col.ticks = "red")

登录后复制

3 图例

R语言的基础绘图系统默认不带图例,需要自定义,相关函数为legend(),语法结构如下:

legend(x, y = NULL, legend, fill = NULL, col = par("col"),

border = "black", lty, lwd, pch,

angle = 45, density = NULL, bty = "o", bg = par("bg"),

box.lwd = par("lwd"), box.lty = par("lty"), box.col = par("fg"),

pt.bg = NA, cex = 1, pt.cex = cex, pt.lwd = lwd,

xjust = 0, yjust = 1, x.intersp = 1, y.intersp = 1,

adj = c(0, 0.5), text.width = NULL, text.col = par("col"),

text.font = NULL, merge = do.lines &&has.pch, trace = FALSE,

plot = TRUE, ncol = 1, horiz = FALSE, title = NULL,

inset = 0, xpd, title.col = text.col, title.adj = 0.5,

seg.len = 2)

登录后复制

先绘制一个含有两条曲线的图形:

plot(1:10, sin(1:10), type = "b", pch = 21, col = "red")

lines(1:10, cos(1:10), type = "b", pch = 22, col = "blue")

登录后复制

基本的图例样式

x和y:图例摆放的位置,可以使用坐标,也可以使用如下关键字:"bottomright"、 "bottom"、 "bottomleft"、"left"、 "topleft"、 "top"、"topright"、 "right" 和 "center",此时y缺省;

legend:图例的描述文本;

title:图例标题;

用于区分组别的参数,常见的如形状、颜色等。

plot(1:10, sin(1:10), type = "b", pch = 21, col = "red")

lines(1:10, cos(1:10), type = "b", pch = 22, col = "blue")

legend("bottomleft", legend = c("sin(x)", "cos(x)"), title = "图例",

pch = c(21, 22), lty = 1, col = c("red", "blue"), pt.bg = "white")

登录后复制

用于区分组别的参数

点要素:pch、cex、col、pt.bg、pt.cex、pt.lwd;

线要素:lty、lwd、col;

merge:控制是否将点、线作为整体,默认为TRUE;

面或箱型要素:fill(填充色)、border(边框颜色)。

plot(1, type = "n")

legend("topleft", legend = c("A", "B"),

pch = c(21, 22), lty = 1, col = c("red", "blue"))

legend("topright", legend = c("A", "B"),

pch = c(21, 22), lty = 1, col = c("red", "blue"),

merge = F)

legend("left", legend = c("A", "B"), pch = 21, pt.bg = c("red", "blue"))

legend("right", legend = c("A", "B"), lty = c(1,2), lwd = 2, col = c("red", "blue"))

legend("center", legend = c("A", "B"), fill = c("red", "blue"), border = "green")

登录后复制

设置图例的颜色、边框

bg:图例的背景色;

box.lty、box.lwd、box.col:图例边框的线条类型、宽度和颜色。

plot(1, type = "n")

legend("left", legend = c("A", "B"), fill = c("red", "blue"),

bg = "grey")

legend("right", legend = c("A", "B"), fill = c("grey", "grey"), box.col = c("red", "blue"))

legend("center", legend = c("A", "B"), fill = c("grey", "grey"), box.lty = 2, box.col = "red")

登录后复制

对齐与距离调整

xjust、yjust:图例位置与坐标的对齐方式,xjust默认为0,即左对齐;yjust默认为1,即上对齐;

x.intersp、y.intersp:分别调整图例中水平、垂直方向的间距;

adj:调整图例文本的位置,正数表示向默认位置左侧或下侧调整,负数表示向右侧或上侧调整;

inset:使用关键词设置图例位置时,控制图例边缘与plot side的位置,以plot region长宽的分数表示。

plot(1, type = "n")

legend(0.8, 1, legend = c("A", "B"), fill = c("red", "blue"), bg = "grey",

xjust = 1, yjust = 0)

legend(0.8,1, legend = c("A", "B"), fill = c("grey", "grey"), box.col = c("red", "blue"),

x.intersp = 2, y.intersp = 2)

legend("center", legend = c("A", "B"), fill = c("grey", "grey"), box.lty = 2, box.col = "red",

adj = c(-2, 2))

legend("bottom", legend = c("A", "B"), fill = c("grey", "grey"), box.lty = 2, box.col = "red",

inset = 0.05)

登录后复制

图例文本美化

text.width、text.col、text.font;

title.col、title.adj:图例标题的颜色和水平对齐方式;

ncol:图例内要素列数,默认为1;

horiz:为TRUE时水平方向排列图例要素,此时参数ncol失效。

plot(1, type = "n")

legend("center", legend = c("A", "B"), fill = c("red", "blue"), bg = "grey",

text.col = c("red", "blue"), text.font = 3)

legend("top", legend = c("A", "B"), fill = c("red", "blue"), bg = "grey", title = "图例",

title.col = "green", title.adj = 1)

legend("left", legend = c("A", "B"), fill = c("red", "blue"), bg = "grey",

ncol = 2)

legend("right", legend = c("A", "B"), fill = c("red", "blue"), bg = "grey",

horiz = T)

登录后复制

其他plot:为FALSE时图例不显示。

package com.hello

import java.util.Arrays

import java.util.Scanner

/**

*

* @author fxb

*/

public class Intset {

private int[] element

private int EndPosition

public Intset(int a[], int size) {

this.element = new int[size]

System.arraycopy(a, 0, element, 0, a.length)

this.EndPosition = size + 1

}

public int[] inputdata(int a[], int size) {

int[] t = new int[a.length + 1]

System.arraycopy(a, 0, t, 0, a.length)

t[a.length] = size

return t

}

public Intset Merge(Intset set) {

int[] t = new int[0]

for (int i = 0i <this.getElement().lengthi++) {

for (int j = 0j <set.getElement().lengthj++) {

if (this.getElement()[i] == set.getElement()[j]) {

t = inputdata(t, this.getElement()[i])

break

}

}

}

return new Intset(t, t.length)

}

public Intset Copy(Intset set) {

int[] t = new int[this.getElement().length]

System.arraycopy(this.getElement(), 0, t, 0, t.length)

for (int i = 0i <set.getElement().lengthi++) {

for (int j = 0j <this.getElement().lengthj++) {

if (set.getElement()[i] == this.getElement()[j]) {

continue

}

}

t = inputdata(t, set.getElement()[i])

}

return new Intset(t, t.length)

}

public void print() {

for (int i : element) {

System.out.print(i+" ")

}

System.out.println("\n")

}

public int[] getElement() {

return element

}

public void setElement(int[] element) {

this.element = element

}

public int getEndPosition() {

return EndPosition

}

public void setEndPosition(int EndPosition) {

this.EndPosition = EndPosition

}

public static int[] convert(String s){

char[] c = s.toCharArray()

int[] is = new int[c.length]

for (int i = 0i <c.lengthi++) {

int si = Integer.parseInt(c[i]+"")

is[i] = si

}

return is

}

public static void main(String[] args) {

Scanner reader = new Scanner(System.in)

System.out.print("请输入集合A元素个数:")

int sizea = reader.nextInt()

System.out.print("请输入集合A元素值,无需输入空格:")

String s = new Scanner(System.in).nextLine()

int[] a = convert(s)

Intset intsetA = new Intset(a,sizea)

System.out.print("请输入集合B元素个数:")

int sizeb = reader.nextInt()

System.out.print("请输入集合B元素值,无需输入空格:")

String s2 = new Scanner(System.in).nextLine()

int[] b = convert(s2)

Intset intsetB = new Intset(b,sizeb)

Intset intset = intsetA.Merge(intsetB)

System.out.print("A与B的并集为:")

intset.print()

System.out.print("A与B的交集为:")

intset = intsetA.Copy(intsetB)

intset.print()

}

}

run:

请输入集合A元素个数:3

请输入集合A元素值,无需输入空格:123

请输入集合B元素个数:3

请输入集合B元素值,无需输入空格:345

A与B的并集为:3

A与B的交集为:1 2 3 3 4 5

PS:用JAVA调试的,还有点小BUG,LZ可以试着调试下。