Search code examples
rplotscatter-plotaxes

Turn plot around in R


Currently I am doing research to chatbot interfaces and make use of eyetracking to test my prototypes.

My eyetracking device creates a csv file with a x and a y coordinate every 16 mili seconds.

I want to plot this information with:

  • The X-axis on the top
  • The Y-axis on the right (starting with zero at the top)

Currently I have the following code:

dataleft = data[c(3,4)] 
dataleft_matrix = data.matrix(dataleft)
plot(dataleft_matrix, main="Eyetracking Left Eye", xlab="X-as", ylab="Y-as")`

However, this does not create the axes as I want them to be.

Can someone help me, please?


Solution

  • I'd recommend using ggplot for this, rather than base R. Of course, you may have good reason to prefer plotting using base R, but I find ggplot easier (and faster) to use.

    library(ggplot2)
    xleft <- c(2,3,4,2,1,2,3,4,5)
    yleft <- c(2,3,4,3,2,1,6,5,3)
    leftdata <- data.frame(xleft, yleft)
    ggplot(data = leftdata) +
      geom_point(aes(x = xleft, y = yleft)) +
      scale_y_reverse(position = "right") +
      scale_x_continuous(position = "top") +
      ggtitle("Eyetracking Left Eye") +
      xlab("X-as") +
      ylab("Y-as")
    

    I think this is what you want it to look like, right?