简体   繁体   中英

How to change the scale(interval) of Y-axis in a scatter plot in R?

I have used the command:

plot(data$Revenue, data$Profits, main="Revenue vs Profits", xlab="Revenue", ylab="Profits", xlim=c(0,500000), ylim=c(-25000,55000), cex=0.00001*data$Employees)

to get the following scatter plot:

散点图

The data is cluttered due to less intervals in the Y-axis.

How do I introduce a smaller scale on the Y-axis?

You could try rolling your own "negative handling" log or cuberoot transformer:


## create some similar data:
set.seed(102)
data <- data.frame(
    Revenue = rlnorm( n=200, m=10, s=1 ),
    Profits = -5e3 + rnorm( n=200, m=8e3, s=10e3 )
) %>% mutate( Employees = runif( n(), max=1e5 ) + Revenue*rlnorm(m=0.5, n=n()) )

## transformation:
ytrans <- function(x)sign(x)*abs(x)^(1/3)
## ytrans <- function(x)sign(x)*abs(log(x))

## plot it
plot(data$Revenue, ytrans(data$Profits), main="Revenue vs Profits", xlab="Revenue", ylab="Profits", xlim=c(0,500000), ylim=ytrans(c(-25000,55000)), cex=0.00001*data$Employees, yaxt="n")
y.labels <- c(-20e3, -5e3, -1e3, 0, 1e3, 5e3, 20e3 )
y.at <- ytrans(y.labels)
axis( 2, at=y.at, labels=y.labels )

It might look prettier with your real data.

And maybe you need to re-center it at something other than 0 to make the transformation more succesful. I'd need to see your data to tell for sure.

在此处输入图像描述

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