Reputation: 13
Creating a barplot
from a single vector/column in a .csv
file is no problem in vanilla R:
msgs <- read.csv(file = "C:/my/csv/file.csv", header = TRUE, sep = ",")
barplot(table(msgs$Author))
This code produces following barplot
:
Though, I am having a hard time replicating this with the ggplot2
package. To do it with a qplot()
, this code works:
qplot(as.factor(msgs$Author), geom="bar")
But I need it as a ggplot()
, which I can't seem to make work. I have tried:
ggplot() +
aes(table(msgs$Author)) +
geom_bar(stat = "identity", width = .75, ill = "tomato3")
(which didn't work at all) and
ggplot(msgs, aes(x = Author, y = Timestamp)) +
geom_bar(stat = "identity", width = .75, fill = "tomato3")
The above snippet does show correct result with the bars
but the y-axis is all wrong. I don't want Timestamp to be the y-axis, but rather the amount of times every Author shows up in the vector/column.
I can't really find a way of providing ggplot()
with a single column or vector though. Can any of you help me with this?
EDIT: The data below can be used to reproduce my dataset. It is not the exact values, but the format is all that is needed.
Timestamp,Author
1534334332013,user1
1534334331252,user2
1534333113577,user2
1534333112754,user3
1529160743306,user4
1528886271012,user3
1528886269171,user5
1528886261391,user5
1526477321297,user5
1526477320773,user4
Upvotes: 1
Views: 3181
Reputation: 206197
The geom_bar
layer will count observations for you if you just leave off the y
aesthetic. For example
ggplot(msgs, aes(Author)) + geom_bar()
Tested with
msgs <- read.csv(text="Timestamp,Author
1534334332013,user1
1534334331252,user2
1534333113577,user2
1534333112754,user3
1529160743306,user4
1528886271012,user3
1528886269171,user5
1528886261391,user5
1526477321297,user5
1526477320773,user4")
Produces
Upvotes: 2
Reputation: 326
I produced similar example:
xxx <- rpois(1000, 10)
xtable <- table(xxx)
ggplot(data = NULL, aes(x = as.numeric(names(xtable)), y = as.numeric(xtable))) +
geom_bar(stat = "identity", width = .75)
This should produce you correct answer.
Upvotes: 2