当前位置:网站首页>R language mapping: coordinate axis setting

R language mapping: coordinate axis setting

2022-07-23 19:36:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm your friend, Quan Jun .

   To draw a pleasing statistical chart , The setting of coordinate axis is very important . stay R In the bottom drawing of language , The adjustment of coordinate axis is mainly through adjustment plot function 、axis Functions and title A series of parameters of the function complete .

plot(x,y, …)

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, …)

title(main= NULL, sub = NULL, xlab = NULL, ylab = NULL,

line = NA, outer = FALSE, …)

One 、plot Function preparation

   Before personalization axis , It usually needs to be adjusted plot Function ann、bty、xaxt、yaxt、xaxs and yaxs Parameters :

  ann take FALSE The title will not be drawn ( Including the main 、 Subtitle and axis title );

  bty Used to set the border form , The default value is ”o”, It means that all four borders are drawn , Other optional values include ”l”( The lower left )、”7″( Up right )、”c”( Up, down, left )、”u”( Left down right )、”]” ( Up, down, right ) and ”n”( nothing , That is, no border ), In many personalized drawings ,bty Set to ”n”, Later border lines use other functions ( Such as axis) Add by yourself ;

  xaxs and yaxs Used to set x Axis and y The scope of the shaft , Default value “r”, Indicates that the coordinate axis is smaller than the given drawing range ( Parameters xlim and ylim The scope given ) A little bigger , take ”i” Time indicates that the coordinate axis range is exactly the same as the given drawing range , It is also desirable ”s”、”e”、”d”;

  xaxt and yaxt take ”n” when , Axis 、 The scale line and scale value will not be drawn .

x <- seq(-4, 4, 0.01)
y <- x^2
par(mfrow = c(2, 2), mar = c(4, 4, 1, 1))
plot(x, y)   #  Not processed 
plot(x, y,  xaxs = "i", yaxs ="i")   #  The drawing border is not left blank 
plot(x, y, bty = 'l')   #  Keep only the left and bottom borders 
plot(x, y, ann = F, bty = "n", xaxt = "n", yaxt ="n")   #  Frame 、 The axes are removed 

Two 、axis Function usage

1、 Basic operation

  side Represents the coordinate axis to be operated , Value 1、2、3、4 On behalf of 、 Left 、 On 、 Right ;

  at Indicates the position of the scale line and the scale value ;

  labels Indicates the scale value ;

  las Indicates the text direction of coordinate scale value ,las=0 Indicates that the text direction is parallel to the coordinate axis ,1 Indicates that it is always horizontal ,2 Indicates that it is perpendicular to the coordinate axis ,3 It means the end is vertical .

x<- seq(-4, 4, 0.01)
y<- x^2
par(mfrow=c(1, 2), mar = c(4, 4, 1, 1))
# Text direction is not set 
plot(x,y, ann = F, xaxt = "n", yaxt = "n")  
axis(1,-4:4, -4:4)
axis(2,seq(0, 16, 4), seq(0, 16, 4))
# The text direction is horizontal 
plot(x,y, ann = F, xaxt = "n", yaxt = "n")  
axis(1,-4:4, -4:4, las = 1)
axis(2,seq(0, 16, 4), seq(0, 16, 4), las = 1)

2、 Font size

  cex.axis Indicates the font size of the scale value of the coordinate axis ,

  font.axis Font representing the scale value of the coordinate axis ,font=1 Indicates orthography ,2 Indicates bold ,3 In italics ,4 Indicates Black Italic .

x<- seq(-4, 4, 0.01)
y<- x^2
par(mfrow=c(2, 2), mar = c(4, 4, 1, 1))
# Font size is not set 
plot(x,y, ann = F, xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4)
#cex.axis = 2
plot(x,y, ann = F, xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, cex.axis = 2)
#font.axis = 2
plot(x,y, ann = F, xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, font.axis = 2)
#font.axis = 3
plot(x,y, ann = F, xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, font.axis = 3)

3、 Color

  col The color of the diagram , Use in axis The color of the coordinate axis and coordinate scale line in the function ;

  col.axis Represents the color of the scale value of the coordinate axis ;

  col.ticks Indicates the color of the scale line of the coordinate axis .

x<- seq(-4, 4, 0.01)
y<- x^2
par(mfrow=c(2, 2), mar = c(4, 4, 1, 1))
# No color set 
plot(x,y, ann = F, bty = "n", xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4)
#col = 2
plot(x,y, ann = F, bty = "n", xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, col = 2)
#col.axis = 2
plot(x,y, ann = F, bty = "n", xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, col.axis = 2)
#col.ticks = 2
plot(x,y, ann = F, bty = "n", xaxt = "n", yaxt = "n")
axis(1,-4:4, -4:4, col.ticks = 2)

4、 Location

  line Indicates the distance between the coordinate axis position and the image frame , Negative numbers will be drawn within the image border ;

  mgp The default value is c(3, 1, 0), The three numbers represent the axis title 、 The scale value and the distance between the axis and the drawing border ;

  tcl The default value is -0.5, The number indicates the length of the tick mark , A negative value means that the scale line is facing outward , It's positive and facing inward ;

  pos Indicates the position of the axis ;

  line.outer take TRUE when , The coordinate axis will be drawn at the edge of the canvas ;

  hadj It refers to the distance that the scale value is adjusted along the parallel coordinate axis ;

  padj It refers to the distance that the scale value is adjusted along the vertical coordinate axis .

x <- seq(-4, 4, 0.01)
y <- x^2
par(mfrow= c(2, 2), mar = c(4, 4, 1, 1))
#  The scale value position is not set 
plot(x, y, ann = F, xaxt = "n", yaxt ="n")
axis(1, -4:4, -4:4)
#  Use line Adjust the position of the scale value 
plot(x, y, ann = F, xaxt = "n", yaxt ="n")
axis(1, -4:4, -4:4, line = 2)
#  Use mgp Adjust the position of the scale value 
plot(x, y, ann = F, xaxt = "n", yaxt ="n")
axis(1, -4:4, -4:4, mgp = c(3, 2, 0)) 
#  Use padj Adjust the position of the scale value 
plot(x, y, ann = F, xaxt = "n", yaxt ="n")
axis(1, -4:4, -4:4, padj = 1)

5、 other

  tick take FALSE when , The coordinate axis and scale line are not drawn ;

  lty Said linear , Use in axis The function represents the coordinate axis type ;

  lwd Indicates the thickness of the line , Use in axis The thickness of the coordinate axis in the function ;

  lwd.ticks Indicates the thickness of the scale line .

3、 ... and 、title Function usage

  main、sub、xlab and ylab Respectively represent the main title 、 Subtitle 、x Axis title and y Axis title ;

  cex.lab Indicates the font size of the axis title ;

  font.lab The font that represents the axis title ;

col.lab Represents the color of the axis title ;

   The rest of the parameters and axis The usage is the same in .

x<- seq(-4, 4, 0.01)
y<- x^2
par(mfrow=c(2, 2), mar = c(4, 4, 1, 1))
# No title 
plot(x,y, ann = F, xaxt = "n", yaxt = "n")  
axis(1,-4:4, -4:4)
axis(2,seq(0, 16, 4), seq(0, 16, 4))
# Use title Set title 
plot(x,y, ann = F, xaxt = "n", yaxt = "n")  
axis(1,-4:4, -4:4, las = 1)
axis(2,seq(0, 16, 4), seq(0, 16, 4), las = 1)
title(xlab= 'x', ylab = 'y')
# Use line Adjusting position 
plot(x,y, ann = F, xaxt = "n", yaxt = "n")  
axis(1,-4:4, -4:4, las = 1)
axis(2,seq(0, 16, 4), seq(0, 16, 4), las = 1)
title(xlab= 'x', ylab = 'y', line = 2)
# Adjust the color font size 
plot(x,y, ann = F, xaxt = "n", yaxt = "n")  
axis(1,-4:4, -4:4, las = 1)
axis(2,seq(0, 16, 4), seq(0, 16, 4), las = 1)
title(xlab= 'x', ylab = 'y', col.lab = 2, font.lab = 4, cex.lab = 2)

Four 、 Scale interval

  plot Function will automatically give a reasonable scale interval , It's essentially a call to pretty function .

  pretty(x, n = 5, min.n = n %/% 3, shrink.sml = 0.75,   high.u.bias = 1.5, u5.bias = .5 + 1.5*high.u.bias,   eps.correct = 0, …)

  x It's a sequence ,pretty The function will first correct x Take a range, Then divide it into approximately n+1 An interval . If you are not satisfied with the interval automatically given by the system when drawing , You can adjust n Value , use pretty Function to customize the appropriate interval .

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/126792.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207231756257169.html