如何将x轴和y轴标签的字体大小和颜色改为R中的plot函数?

时间:2022-10-19 08:13:44

I used the following code to draw a scatterplot. How to increase the font size and change colour of x-axis and y-axis label?

我用下面的代码画了一个散点图。如何增加x轴和y轴标签的字体大小和改变颜色?

data=read.csv("data.csv") 
plot(data$column1,data$column2,xlab="x axis", ylab="y axis",  pch=19)

3 个解决方案

#1


27  

To track down the correct parameters you need to go first to ?plot.default, which refers you to ?par and ?axis:

为了找到正确的参数,你需要首先进行?

plot(1, 1 ,xlab="x axis", ylab="y axis",  pch=19,
           col.lab="red", cex.lab=1.5,    #  for the xlab and ylab
           col="green")                   #  for the points

#2


37  

Look at ?par for the various graphics parameters.

看看这些不同的图形参数。

In general cex controls size, col controls colour. If you want to control the colour of a label, the par is col.lab, the colour of the axis annotations col.axis, the colour of the main text, col.main etc. The names are quite intuitive, once you know where to begin.

一般cex控制尺寸,col控制颜色。如果你想要控制标签的颜色,par是col.lab,轴标注的颜色,轴,主文本的颜色,col.main等等,一旦你知道从哪里开始,这些名字就很直观。

For example

例如

x <- 1:10
y <- 1:10

plot(x , y,xlab="x axis", ylab="y axis",  pch=19, col.axis = 'blue', col.lab = 'red', cex.axis = 1.5, cex.lab = 2)

如何将x轴和y轴标签的字体大小和颜色改为R中的plot函数?

If you need to change the colour / style of the surrounding box and axis lines, then look at ?axis or ?box, and you will find that you will be using the same parameter names within calls to box and axis.

如果您需要更改周围的框和轴的颜色/样式,然后查看?轴或?box,您将发现您将使用相同的参数名称在调用到box和axis。

You have a lot of control to make things however you wish.

无论你多么希望,你都能控制自己。

eg

plot(x , y,xlab="x axis", ylab="y axis",  pch=19,  cex.lab = 2, axes = F,col.lab = 'red')
box(col = 'lightblue')
axis(1, col = 'blue', col.axis = 'purple', col.ticks = 'darkred', cex.axis = 1.5, font = 2, family = 'serif')
axis(2, col = 'maroon', col.axis = 'pink', col.ticks = 'limegreen', cex.axis = 0.9, font =3, family = 'mono')

如何将x轴和y轴标签的字体大小和颜色改为R中的plot函数?

Which is seriously ugly, but shows part of what you can control

这很难看,但是显示了你可以控制的部分?

#3


1  

Taking DWins example.

以DWins例。

What I often do, particularly when I use many, many different plots with the same colours or size information, is I store them in variables I otherwise never use. This helps me keep my code a little cleaner AND I can change it "globally".

我经常做的事情,特别是当我使用许多不同的、具有相同颜色或大小信息的图时,我将它们存储在我从未使用过的变量中。这可以帮助我保持代码更简洁,并且可以“全局地”更改它。

E.g.

如。

clab = 1.5
cmain = 2
caxis = 1.2

plot(1, 1 ,xlab="x axis", ylab="y axis",  pch=19,
           col.lab="red", cex.lab=clab,    
           col="green", main = "Testing scatterplots", cex.main =cmain, cex.axis=caxis) 

You can also write a function, doing something similar. But for a quick shot this is ideal. You can also store that kind of information in an extra script, so you don't have a messy plot script:

你也可以写一个函数,做一些类似的事情。但对于快速射击来说,这是理想的选择。你也可以在额外的脚本中存储这类信息,这样你就不会有一个混乱的脚本:

which you then call with setwd("") source("plotcolours.r")

然后调用setwd(“”)源(“plotcolor .r”)

in a file say called plotcolours.r you then store all the e.g. colour or size variables

在一个叫做plotcolor的文件中。然后存储所有的颜色或大小的变量。

clab = 1.5
cmain = 2
caxis = 1.2 

for colours could use

颜色可以使用

darkred<-rgb(113,28,47,maxColorValue=255)

as your variable 'darkred' now has the colour information stored, you can access it in your actual plotting script.

由于您的变量“暗红色”现在已经存储了颜色信息,您可以在实际的绘图脚本中访问它。

plot(1,1,col=darkred) 

#1


27  

To track down the correct parameters you need to go first to ?plot.default, which refers you to ?par and ?axis:

为了找到正确的参数,你需要首先进行?

plot(1, 1 ,xlab="x axis", ylab="y axis",  pch=19,
           col.lab="red", cex.lab=1.5,    #  for the xlab and ylab
           col="green")                   #  for the points

#2


37  

Look at ?par for the various graphics parameters.

看看这些不同的图形参数。

In general cex controls size, col controls colour. If you want to control the colour of a label, the par is col.lab, the colour of the axis annotations col.axis, the colour of the main text, col.main etc. The names are quite intuitive, once you know where to begin.

一般cex控制尺寸,col控制颜色。如果你想要控制标签的颜色,par是col.lab,轴标注的颜色,轴,主文本的颜色,col.main等等,一旦你知道从哪里开始,这些名字就很直观。

For example

例如

x <- 1:10
y <- 1:10

plot(x , y,xlab="x axis", ylab="y axis",  pch=19, col.axis = 'blue', col.lab = 'red', cex.axis = 1.5, cex.lab = 2)

如何将x轴和y轴标签的字体大小和颜色改为R中的plot函数?

If you need to change the colour / style of the surrounding box and axis lines, then look at ?axis or ?box, and you will find that you will be using the same parameter names within calls to box and axis.

如果您需要更改周围的框和轴的颜色/样式,然后查看?轴或?box,您将发现您将使用相同的参数名称在调用到box和axis。

You have a lot of control to make things however you wish.

无论你多么希望,你都能控制自己。

eg

plot(x , y,xlab="x axis", ylab="y axis",  pch=19,  cex.lab = 2, axes = F,col.lab = 'red')
box(col = 'lightblue')
axis(1, col = 'blue', col.axis = 'purple', col.ticks = 'darkred', cex.axis = 1.5, font = 2, family = 'serif')
axis(2, col = 'maroon', col.axis = 'pink', col.ticks = 'limegreen', cex.axis = 0.9, font =3, family = 'mono')

如何将x轴和y轴标签的字体大小和颜色改为R中的plot函数?

Which is seriously ugly, but shows part of what you can control

这很难看,但是显示了你可以控制的部分?

#3


1  

Taking DWins example.

以DWins例。

What I often do, particularly when I use many, many different plots with the same colours or size information, is I store them in variables I otherwise never use. This helps me keep my code a little cleaner AND I can change it "globally".

我经常做的事情,特别是当我使用许多不同的、具有相同颜色或大小信息的图时,我将它们存储在我从未使用过的变量中。这可以帮助我保持代码更简洁,并且可以“全局地”更改它。

E.g.

如。

clab = 1.5
cmain = 2
caxis = 1.2

plot(1, 1 ,xlab="x axis", ylab="y axis",  pch=19,
           col.lab="red", cex.lab=clab,    
           col="green", main = "Testing scatterplots", cex.main =cmain, cex.axis=caxis) 

You can also write a function, doing something similar. But for a quick shot this is ideal. You can also store that kind of information in an extra script, so you don't have a messy plot script:

你也可以写一个函数,做一些类似的事情。但对于快速射击来说,这是理想的选择。你也可以在额外的脚本中存储这类信息,这样你就不会有一个混乱的脚本:

which you then call with setwd("") source("plotcolours.r")

然后调用setwd(“”)源(“plotcolor .r”)

in a file say called plotcolours.r you then store all the e.g. colour or size variables

在一个叫做plotcolor的文件中。然后存储所有的颜色或大小的变量。

clab = 1.5
cmain = 2
caxis = 1.2 

for colours could use

颜色可以使用

darkred<-rgb(113,28,47,maxColorValue=255)

as your variable 'darkred' now has the colour information stored, you can access it in your actual plotting script.

由于您的变量“暗红色”现在已经存储了颜色信息,您可以在实际的绘图脚本中访问它。

plot(1,1,col=darkred)