'R character plot

How can I get R to count the number of characters with the same name and to plot said characters in ggplot2?

Example: A classroom has the students with the following names: Rick Rick Bobby Jill Jill Jill How can I get R to graph the number of each students with the same names?

Sorry for the simple question but I'm new to R and my limited vocabulary in coding limits my ability to use google!



Solution 1:[1]

Try with this code:

names <- c("Rick", "Rick", "Bobby", "Jill", "Jill", "Jill")
table <- as.data.frame(table(names)); table

The output would look like this:

  names Freq
1 Bobby    1
2  Jill    3
3  Rick    2

You can plot the table frequency with a simple barplot:

barplot(table$Freq, names.arg=table$names)

enter image description here

Or use ggplot2 to achieve a more aesthetic plot as you wish.

Solution 2:[2]

Something like this?

names <- c("Rick", "Rick", "Bobby", "Jill", "Jill", "Jill")
dat <- data.frame(names)
ggplot(dat) + geom_bar(mapping = aes(x = names), fill = 1:3

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Rui Barradas