I just started learning R and can't figure out how to plot histogram of Gender (Male Female Other, all categorical on x axis) and the Reaction time (numeric,y axis). Also, need to show distribution of reaction times between genders. Thanks.
Asked
Active
Viewed 47 times
1 Answers
0
Using a histogram for a variable with three levels, as in your case, is difficult. Maybe a more fitting graph is a stripchart.
Let's assume you have a data frame structured roughly similarly to this one:
df <- data.frame(
Gender = c(rep("male", 33), rep("female", 33), rep("other", 34)),
Reaction_time = c(rnorm(100)^2)
)
head(df)
Gender Reaction_time
1 male 0.1041879
2 male 0.0125468
3 male 0.8123625
4 male 0.2690723
5 male 1.0718162
6 male 2.5211264
then you could plot the reaction times nicely in a stripchart:
par(mar=c(4,4,1,4)) # set the margins of the plot
stripchart(df$Reaction_time~df$Gender, # plot the two variables against each other
method="jitter", # avoid overplotting
xlab="Reaction time", # label the x-axis
ylab="Gender") # label the y-axis

Chris Ruehlemann
- 20,321
- 4
- 12
- 34