Skip to content
This repository has been archived by the owner on Jan 12, 2019. It is now read-only.

《R Graphic Cookbook》第二章学习笔记 #11

Open
JackieMium opened this issue Mar 9, 2018 · 0 comments
Open

《R Graphic Cookbook》第二章学习笔记 #11

JackieMium opened this issue Mar 9, 2018 · 0 comments
Labels
R R related 做图 plot 基础 很基础的东西
Milestone

Comments

@JackieMium
Copy link
Owner

2017-06-25 15:20:06

设置各种元素的颜色

Setting colors for text elements: axis annotations, labels, plot titles, and legends

plot(rnorm(100), 
     main="Plot Title",
     col.axis="blue",
     col.lab="red",
     col.main="darkblue",
     col='darkgreen')

得到下图:

1

R 内置的默认颜色组:

palette()
[1] "black"   "red"     "green3"  "blue"    "cyan"    "magenta" "yellow"  "gray" 
palette(c("red","blue","green","orange"))
palette()
[1] "red"    "blue"   "green"  "orange"
# To revert back to the default palette type:
palette("default")

设置字体

字体一般通过 par() 来设置,例如:

par(family="serif",font=2)

A font is specified in two parts: a font family (such as Helvetica or Arial) and a font face within that family (such as bold or italic).
The available font families vary by operating system and graphics devices. So R provides some proxy values which are mapped on to the relevant available fonts irrespective of the system. Standard values for family are "serif", "sans", and "mono".

The font argument takes numerical values: 1 corresponds to plain text (the default), 2 to bold face, 3 to italic, and 4 to bold italic.

The fonts for axis annotations, labels, and plot main title can be set separately using the font.axis, font.lab, and font.main arguments respectively.

点和线的种类和样式

点的种类通过pch参数设置,共25种:

par(mfrow = c(5, 5))
for(i in 1:5){
  if(i < 5){
    for(j in 1:5){plot(1, pch = (i-1)*5 + j, cex = 2, col = 'black')}} # pch设置点的样式,cex设置点的大小
  else
    for(j in 1:5){plot(1, pch = (i-1)*5 + j, cex = 2, col = 'darkgreen', bg = 'red')}
}

4

线的样式和粗细分别通过ltylwd来设置:

plot(rain$Tokyo,
     ylim=c(0,250),
     main="Monthly Rainfall in major cities",
     xlab="Month of Year",
     ylab="Rainfall (mm)",
     type="l",
     lty=1,
     lwd=2)
lines(rain$NewYork,lty=2,lwd=2)
lines(rain$London,lty=3,lwd=2)
lines(rain$Berlin,lty=4,lwd=2)
legend("top",
       legend=c("Tokyo","New York","London","Berlin"),
       ncol=4,
       cex=0.8,
       bty="n",
       lty=1:4,
       lwd=2)

得到下图:

2

Line type number values correspond to types of lines:

  • 0: blank
  • 1: solid (default)
  • 2: dashed
  • 3: dotted
  • 4: dotdash
  • 5: longdash
  • 6: twodash

We can also use the character strings instead of numbers, for example, lty="dashed" instead of lty=2.

设置坐标轴标签和刻度

可以分别通过xaxpyaxp设置坐标轴的范围和间距,格式为c(min,max,n)

plot(rnorm(100),xaxp=c(0,100,10),yaxp=c(-2,2,4))

得到:

3

las参数可以设置坐标轴刻度标识与轴的方向关系:

  • 0: always parallel to the axis (default)

  • 1: always horizontal

  • 2: always perpendicular to the axis

  • 3: always vertical

@JackieMium JackieMium added R R related 做图 plot 基础 很基础的东西 labels Mar 9, 2018
@JackieMium JackieMium added this to the Migration milestone Mar 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
R R related 做图 plot 基础 很基础的东西
Projects
None yet
Development

No branches or pull requests

1 participant