Search code examples
rx-axis

Is there a layer using ggplot2 in R to customise the x-axis?


I'm trying to create an x-axis that decreases then increases. It's for a lab where we cooled down shrimp and counted the pleopod beats every 2C change.

Here's one set of the data:

Temperature Starved Individual 1
20 57
18 53
16 54
14 50
12 49
10 43
8 41
6 42
4 39
6 41
8 45
10 49
12 57
14 60
16 57
18 58
20 71

This is the code I've used and the plot it creates, but I can't figure out how to make the x-axis go from 20-4-20.

 s1 <- ggplot(wide_artemia_data, aes(temperature, beats_s1)) + 
  xlab("Temperature (C)") + 
  ylab("Number of Pleopod Beats over 30 sec")

s1 + geom_point()

Plot output

This is my first time trying to learn to code and use R studio, so any help or tips would be greatly appreciated!


Solution

  • Your x-axis needs to be unique, but you can add one, and show something else:

    
    wide_artemia_data <- read.table(text=
    "Temperature    Starved Individual 1
    20  57
    18  53
    16  54
    14  50
    12  49
    10  43
    8   41
    6   42
    4   39
    6   41
    8   45
    10  49
    12  57
    14  60
    16  57
    18  58
    20  71
    ", header=TRUE, check.names=FALSE, sep="\t" )
    
    wide_artemia_data$id <- 1:nrow( wide_artemia_data )
    s1 <- ggplot(wide_artemia_data, aes(id, `Starved Individual 1`)) +
        scale_x_continuous( labels=wide_artemia_data$Temperature, breaks=wide_artemia_data$id ) +
        xlab("Temperature (C)") +
        ylab("Number of Pleopod Beats over 30 sec")
    s1 + geom_point()
    
    

    Result: enter image description here