Plotting Categorical Variable (x) against Continuous Variable (y) in R

Would anybody please be able to help with the R code needed to plot my data? I have been trying for hours and searching all over the web, I am really struggling.

This is an example of my data placed into a table/matrix, in R Studio:

 Percentage Correct
4 71.88
20 65.80
40 63.92
60 63.47

The 4,20,40and60 are categorical variables - they represent different levels of categorical interference.

Percentage Correct is the percentage of images that participants got correct for each interference level.

So for example, 63.47 means that when the interference category was 60, the average percentage of correct responses across all participants was 63.47%.

Below I have attached a couple of images of graphs that I am trying to achieve. I don't necessarily want mine to be that complex, but just like the basic style.

I am struggling to actually plot anything at all at the minute without getting any errors.

Any help would be hugely appreciated, thank you very much!

Example Graph 1

Example Graph 2

6

1 Answer

Here is a possible start using ggplot2:

# Your sample data
df <- read.table(text = "x y
4 71.88
20 65.80
40 63.92
60 63.47", header = T);
library(ggplot2);
ggplot(df, aes(as.factor(x), y)) + geom_point() + labs(y = "Percentage correct", x = "Categorical variable");

enter image description here

If you turn x into a factor it will be treated as a categorical variable.


Or as a barchart (as suggested by @neilfws):

library(ggplot2);
ggplot(df, aes(as.factor(x), y)) + geom_bar(stat = "identity") + labs(y = "Percentage correct", x = "Categorical variable");

enter image description here

9

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like