Home > Blockchain >  Scatterplot numerical y against two x groups
Scatterplot numerical y against two x groups

Time:01-11

From my data, I created the following vectors:

ratio_automatic <- c(2.031, 2.24, 2.00, 0.46, 2.75, 0.86, 2.69, 0.44)
ratio_manual <- c(1.02, 2.40, 1.53, 0.50, 1.38, 0.70, 1.69, 0.54)
method <- rep(c("Manual", "Automatic"), each = 8)

to create the following dataframe:

df <- data.frame(method, c(ratio_automatic, ratio_manual))
names(df) <- c("method", "ratio")

So I think that is then called narrow data? In summary: there are 16 observations to two variables.

I want to plot the data in a scatterplot, so you can see the ratio datapoints grouped by category (either 'manual' or 'automatic'). When I run the following code:

plot(1:nrow(df), df$ratio, xaxt="n", xlab="Method", ylab="Ratio", pch=19)
axis(1, at=1:16, labels=df$method)

I get the following graph: plot ratios against method

This is not what I want, as I want on the x-axis just 'automatic' and 'manual' and then the corresponding points above that, not each point separate like now.

is there a way to fix this?

(I'm still very new to R, so I'm sorry if this is an obvious question.)

CodePudding user response:

If you want "Automatic" and "Manual" to appear only once on the x-axis, then you shouldn't make a scatterplot. Scatterplots assumes that both your x- and y-axes are continuous. Consider making a boxplot and overlaying with jittered points, similar to what you would find in a scatterplot.

# make a boxplot
boxplot(df$ratio ~ df$method, col = "white")

# add some points
stripchart(df$ratio ~ df$method,
           method = "jitter",
           pch = 19,
           vertical = TRUE,
           add = TRUE)

enter image description here

CodePudding user response:

Maybe you want something like this where you start with an empty plot and then plot on the 1 and 2 of your factor methods and use points to add the data like this which results in a jitter plot ( you can adjust the positions of your labels in axis):

plot(1, type="n", xlim=c(0, 3), xaxt="n", ylim = c(0, 3), xlab="Method", ylab="Ratio", pch=19)
axis(1, at=1:2, labels=unique(df$method))
points(factor(df$method), df$ratio, xaxt="n", xlab="Method", ylab="Ratio", pch=19)

Output:

enter image description here

  • Related