简体   繁体   中英

How to adjust axis labels in a graph?

I can't understand why I can't adjust my x-axis labels.

I have been trying to use the following code but it is not working well.

x1 <- c(80, 5, 8)
x2 <- c(70, 0, 25)

plot(x1, type="o",col="red", xlab="cluster", ylab="Percent", 
     main="GDP vs Population", pch =17)
text(seq(1, 10, by=1), par("usr")[3] - 0.2, 
     labels=c("Cluster 1", "Cluster 2", "Cluster 3"), xpd=TRUE)
lines(x2, type="o", col="blue", pch =19)
legend("topright", 
       legend=c("GDP", "Population"), 
       col=c("red", "blue"), 
       pch=c(17,19), 
       bty="n", 
       pt.cex=1, 
       cex=1, 
       text.col="black", 
       horiz=F , 
       inset=c(0.1, 0.1))

That is what I have:

我的图表

I'd use mtext . First, switch off automatic x axis using xaxt="n" . Second, rebuild it at 1:3 using axis for the tick marks, and mtext for the labels.

plot(x1,type="o",col="red", xlab="cluster", ylab="Percent", 
     main="GDP vs Population", pch =17, xaxt="n")
lines(x2, type="o", col="blue", pch =19)
axis(side=1, at=1:3, labels=paste("cluster", 1:3))
legend("topright", 
       legend=c("GDP", "Population"), 
       col=c("red", "blue"), 
       pch=c(17,19), 
       bty="n", 
       pt.cex=1, 
       cex=1, 
       text.col="black", 
       horiz=F , 
       inset=c(0.1, 0.1))

在此处输入图像描述


Data:

x1 <- c(80, 5, 8)
x2 <- c(70, 0, 25)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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