Reputation: 87
I have a table with the results of a survey. I mean, I don't have the full survey data, only the frequency tables, Likert scale counts. Is it possible to graph this table in R?
The table looks like this:
Question 1: (response
is a factor)
response count
1 5
2 6
3 2
4 2
5 1
It's very easy to do it in Excel, but i can't in R. The only thing I could think of was to repeat the table values based on the count, but there must be a simpler way...
Upvotes: 1
Views: 11744
Reputation: 887751
If we wanted to do this without having to install any package, use the base R
methods with either a named vector in a single-line
barplot(setNames(df$count, df$response))
Or with the formula method for data.frame
barplot(count ~ response, df)
-output
df <- structure(list(response = 1:5, count = c(5L, 6L, 2L, 2L, 1L)),
class = "data.frame", row.names = c(NA,
-5L))
Upvotes: 1
Reputation: 39613
Try this ggplot2
approach. You can set your response
as x variable and count
as y variable and use geom_col()
in order to display bars. Here the code:
library(ggplot2)
#Plot
ggplot(df,aes(x=factor(response),y=count))+
geom_col(color='black',fill='cyan3')+
xlab('Response')
Output:
Some data used:
#Data
df <- structure(list(response = 1:5, count = c(5L, 6L, 2L, 2L, 1L)), class = "data.frame", row.names = c(NA,
-5L))
Upvotes: 3