My level of R expertise is 25% into "R for Everyone", which is insufficient for my first real-world task:
48 people were asked to memorize and recall 10 digits. Now the results should be presented in a bar plot with the number of responses on the X axis and the number of memorized digits - on Y.
Here's what I did by now:
responses <- c(10,10,10,10,10,10,10,10,10,10,3,3,3,3,3,3,3,3,3,3,3,5,5,5,5,5,5,5,2,2,2,2,2,2,6,6,6,6,8,8,8,8,4,4,4,7,7,9)
# 10 people recalled 10 digits, 11 people - 3 digits, 7 people 5 digits, etc
I tried plot(responses)
. The resulting graphic was entirely off my goal.
I tried qplot
and barplot
, but I don't know how to switch axes (not to make the graph horizontal, but to swap X and Y).
Then I thought that I should treat responses
as a factor. But ggplot2 doesn't know how to deal with data of class factor
. Also, I don't know how to formulate "show factor levels on the X axis and the depth of each level on Y".
Then I converted responses
to a single-column data.frame
, which, coupled with ggplot2, delivered almost palatable results:
df <- data.frame(responses)
ggplot(df, aes(df$responses)) + geom_bar(binwidth=.5,fill="#56B4E9") + xlab("Digits memorized") + ylab("# of responses") + ggtitle("10 digit memorization experiment results") + scale_x_continuous( breaks = round( seq( min(df$responses), max(df$responses), by = 1 ), 1) ) + theme_economist() + scale_y_continuous(breaks = c(0:11)) + theme(axis.title=element_text(size=14))
Why this graph is not what I need:
In the code above, I would gladly replace ggplot(df, aes(df$responses))
with ggplot(df, aes(df$something_else))
, but the responses
column is all I have.
I tried. What you see above is a shadow of what I have been trying for the last 3 days. Unfortunately, most of my trying boils down to following my own tracks into the same dead ends.
I searched. What I found were 2 categories of solutions: either for multiple columns/variables, which I don't understand how to apply to my one-column data.frame; or the ones assuming more knowledge.
I would be grateful for any hint.
Thank you for your patience.
You can either the base graphics function barplot()
barplot(table(responses), horiz=T, xlab="Count",ylab="Digits Mem")
or ggplot with coord_flip()
ggplot(data.frame(resp=factor(responses)), aes(resp)) +
geom_bar() + coord_flip() +
ylab("Count") + xlab("Digits Mem")