简体   繁体   English

如何使用R中的绘图功能更改散点图中x轴和y轴标签的字体大小和颜色?

[英]How to change the font size and color of x-axis and y-axis label in a scatterplot with plot function in R?

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)

Look at ?par for the various graphics parameters.查看?par以了解各种图形参数。

In general cex controls size, col controls colour.一般来说, cex控制大小, col控制颜色。 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.如果你想控制标签的颜色, parcol.lab ,轴注释的颜色col.axis中,颜色main文本, 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)

在此处输入图片说明

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.如果您需要更改周围框和轴线的颜色/样式,请查看?axis?box ,您会发现您将在对box和 axis 的调用box使用相同的参数名称。

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')

在此处输入图片说明

Which is seriously ugly, but shows part of what you can control这非常丑陋,但显示了您可以控制的部分内容

To track down the correct parameters you need to go first to ?plot.default , which refers you to ?par and ?axis :要追踪正确的参数,您需要首先转到?plot.default ,它指的是?par?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

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".这有助于我保持我的代码更简洁,并且我可以“全局”更改它。

Eg例如

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("") source("plotcolours.r") 调用它

in a file say called plotcolours.r you then store all the eg colour or size variables在一个名为 plotcolours.r 的文件中,您然后存储所有例如颜色或大小变量

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.由于您的变量 'darkred' 现在存储了颜色信息,您可以在实际绘图脚本中访问它。

plot(1,1,col=darkred) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM