Search code examples
rplotaxes

How can I change the scale, min, and max of my x and y axes in r?


I can't seem to figure out the command for changing the x- and y-axes to go from 200-800 in increments of 100. (new to R, don't know how to provide large amounts of data)

     SAT <- read.csv(file.choose(), header = TRUE)
    SAT2 <- na.exclude(SAT)
    SAT.MV <- SAT2[,1:2]
    plot(SAT.MV$VSAT,SAT.MV$MSAT,main="Math and Verbal SAT Scores",xlab="Verbal Score",
ylab="Math Score")
    head(SAT.MV)
    SAT.MV.3means <- kmeans(SAT.MV,centers=3)
    SAT.MV.3means$centers
    SAT.MV.3means$cluster
    plot(SAT.MV[SAT.MV.3means$cluster == 1, ], col = "red", 
         xlim=c(min(SAT.MV[ ,1]),max(SAT.MV[ ,1])),
         ylim=c(min(SAT.MV[ ,2]),max(SAT.MV[ ,2])))
    points(SAT.MV[SAT.MV.3means$cluster == 2,], col = "blue")
    points(SAT.MV[SAT.MV.3means$cluster == 3,], col = "seagreen")
    points(SAT.MV.3means$centers,pch=2, col = "black")
            plot(SAT.MV[SAT.MV.3means$cluster == 1, ], col = "red", 
             xlim=c(min(SAT.MV[ ,1]),max(SAT.MV[ ,1])),
             ylim=c(min(SAT.MV[ ,2]),max(SAT.MV[ ,2])))
        points(SAT.MV[SAT.MV.3means$cluster == 2,], col = "blue")
        points(SAT.MV[SAT.MV.3means$cluster == 3,], col = "seagreen")
        points(SAT.MV.3means$centers,pch=2, col = "black")

Solution

  • Suppose I have a small data set:

    dd <- data.frame(x=c(300,700,1000),y=c(-100,200,700))
    

    I want to plot this data on a set of scales that might not match the scale of the data (that is, R's automatic rules might not work as I want).

    ## xlim, ylim set bounds: axes=FALSE turns off axes
    plot(y~x,data=dd,xlim=c(200,800),ylim=c(200,800),axes=FALSE)
    

    Now draw the axes manually:

    ax <- seq(200,800,by=100)  ## same for both axes
    axis(side=1,at=ax)         ## side=1 -> bottom
    axis(side=2,at=ax)         ## side=2 -> left
    box()  ## to draw the bounding box
    

    In this example, all but one of the points in the data set are actually excluded from the plot:

    enter image description here