Search code examples
rplotaxisaxis-labels

How to change the range of the x-axis to spread-out the data-points in R


I am attempting to plot two columns named Canopy_index (response variable) and Lighting (explanatory variable) (data frame below) onto a scatterplot. When the observations are plotted, the data-points are erroneously plotted chiefly to the left hand side of the plot (below), which could possibly be attributed to either to incorrect settings on the x-axis or the base graphics settings need changing. Within the data frame, the values in the Lighting column generally range between 0 to <20.

Objective

The problem is that I would like to manipulate and change the range of the x-axis values (between 0 to 20.0) in order to spread the observations across the whole width of side 1 or the x-axis by increments of 0.5 (i.e. 0, 0.5, 1.0, 1.5.....20).

I have tried different methodologies but the output result is always the same. Additionally, there is a spurious outlier in the plot; however, I cannot detect this datapoint in the data.

If anyone can help, then many thanks.

Code

    plot(Canopy_index~Lighting, 
         xaxt="n", pch=16, 
         col="blue", 
         xlab="Artificial Lighting",
         ylab="Canopy Index", 
         data=Distance_light)

 axis(1, at=seq(0, 20, by=0.5), las=2)

Plot

enter image description here

Data

structure(list(Canopy_index = c(75, 85, 75, 35, 85, 95, 85, 65, 
75, 95, 75, 95, 85, 75, 85, 95, 75, 85, 85, 85, 75, 75, 85, 85, 
65, 85, 75, 85, 95, 95, 85, 55, 75, 95, 75, 95, 95, 65, 65, 55, 
95, 85, 85, 45, 85, 85, 35, 95, 85, 85, 35, 85, 45, 85, 85, 85, 
95, 85, 85, 75, 85, 35, 85, 85, 65, 65, 85, 45, 55, 95, 75, 95, 
45, 75, 75, 95, 95, 85, 75, 95, 75, 65, 85, 75, 75, 55, 75, 85, 
85, 85, 15, 75, 85, 85, 85, 95, 85, 85, 85, 75, 85, 85, 95, 65, 
75, 95, 55, 75, 85, 85, 85, 95, 55, 85, 75, 75, 85, 85, 85, 85, 
55, 75, 55, 75, 85, 75, 85, 85, 75, 85, 75, 95, 25, 95, 95, 25, 
75, 75, 85, 35, 55, 85, 65, 85, 75, 85, 85, 85, 75, 65, 85, 85, 
95, 65, 55, 95, 95, 85, 95, 85, 65, 55, 65, 55, 95, 75, 85, 85, 
35, 75, 75, 85, 65, 85, 65, 65, 95, 85, 95, 75, 75, 55, 95, 65, 
85, 65, 15, 35, 55, 95, 15, 15, 75, 65, 85, 5, 5, 35, 35, 85, 
65, 45, 35, 65, 65, 75, 65, 15, 75, 65, 45, 25, 65, 85, 45, 85, 
75, 15, 65, 45, 55, 45, 15, 45, 75, 65, 75, 65, 35, 95, 65, 35, 
35, 65, 45, 75, 35, 75, 85, 35, 55, 65, 85, 65, 65, 85, 55, 15, 
75, 65, 45, 45, 85, 55, 15, 85, 15, 95, 75, 5, 55, 15, 35, 45, 
85, 65, 65, 65, 65, 25, 85, 35, 55, 65, 75, 5, 45, 65, 15, 75, 
55, 65, 55, 35, 75, 65, 65, 85, 35, 65, 55, 75, 15, 55, 65, 75, 
55, 85, 35, 55, 55, 25, 75, 15, 55, 75, 75, 65, 55, 45, 75, 25, 
45, 95, 55, 75, 45, 25, 35, 55, 15, 15, 75, 35, 55, 55, 65, 45, 
65, 25, 55, 45, 65, 65, 25, 25, 65, 45, 95, 55, 25, 55, 85, 45, 
85, 15, 75, 65, 35, 75, 15, 55, 85, 35, 55, 45, 85, 45, 65, 55, 
75, 65, 85), Lighting = c(2.5, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 5, 20, 7.5, 20, 20, 20, 20, 5.5, 15, 20, 5, 20, 20, 20, 20, 
20, 20, 7.5, 20, 20, 7.5, 20, 2.5, 20, 15, 20, 20, 20, 20, 20, 
20, 20, 20, 15, 20, 2.5, 7.5, 15, 20, 20, 20, 20, 7.5, 20, 2.5, 
20, 20, 20, 20, 20, 20, 20, 15, 20, 20, 2.5, 20, 20, 20, 15, 
15, 20, 20, 20, 20, 15, 15, 20, 2.5, 20, 2.5, 20, 20, 20, 20, 
20, 20, 15, 20, 20, 5, 5, 20, 20, 20, 20, 20, 7.5, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 15, 7.5, 
20, 20, 20, 20, 20, 20, 2.5, 20, 20, 2.5, 20, 20, 15, 20, 20, 
20, 7.5, 15, 20, 7.5, 20, 20, 20, 20, 20, 15, 20, 20, 20, 20, 
20, 20, 20, 2.5, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
15, 7.5, 2.5, 15, 20, 20, 15, 20, 7.5, 8, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 15, 20, 20, 15, 20, 15, 20, 20, 7.5, 20, 20, 7.5, 20, 20, 
20, 20, 20, 20, 15, 20, 7.5, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 15, 15, 75, 20, 20, 20, 20, 20, 20, 2.5, 
20, 20, 2.5, 15, 20, 20, 7.5, 15, 20, 20, 7.5, 20, 20, 20, 20, 
20, 15, 20, 20, 15, 20, 15, 7.5, 2.5, 20, 7.5, 20, 20, 7.5, 20, 
20, 20, 15, 20, 20, 20, 7.5, 20, 7.5, 20, 20, 20, 7.5, 8, 20, 
20, 20, 20, 20, 20, 20, 20, 7.5, 20, 20, 20, 20, 20, 7.5, 20, 
7.5, 20, 20, 20, 20, 20, 15, 20, 20, 2.5, 20, 20, 20, 20, 20, 
20, 20, 2.5, 7.5, 20, 7.5, 20, 2.5, 20, 20, 20, 15, 20, 20, 20, 
20, 20, 20, 20, 7.5, 20, 20, 20, 20, 20, 20, 20, 20, 20, 2.5, 
7.5, 2.5, 15, 20, 20, 15, 20, 20, 20, 20, 20, 15, 20, 20, 20, 
20, 20, 20, 20, 20, 15)), .Names = c("Canopy_index", "Lighting"
 ), row.names = c(NA, -363L), class = "data.frame")

Solution

  • There is an outlier at row 230 which is why you get that graph.

    > Distance_light[Distance_light$Lighting == 75, ]
        Canopy_index Lighting
    230           95       75
    

    If you remove it, it works fine:

    plot(Distance_light$Lighting[-230], 
         Distance_light$Canopy_index[-230],
         pch=16, 
         col="blue", 
         xlab="Artificial Lighting",
         ylab="Canopy Index",
         xaxt='n')
    
    axis(1, at=seq(0, 20, by=0.5), las=2)
    

    enter image description here