user3749549
user3749549

Reputation: 389

How to construct a histogram from a table?

I have the following data:

v <- c('a','a','h','b','h','a','j','h','a')
t <- table(v)

I wish to create a histogram in ggplot2 from this table. How can I do this without converting the table to a data frame or a vector?

Upvotes: 0

Views: 2467

Answers (2)

talat
talat

Reputation: 70256

You could also use a dplyr chain:

require(dplyr)

t %>% as.data.frame %>% ggplot(aes(x = v, y = Freq)) + geom_histogram(stat = "identity") 

Inside the chain, t is converted to a data.frame object, but t will not be changed and there won't be a new data.frame object after the process. Perhaps that's also acceptable for your problem.

Upvotes: 4

Didzis Elferts
Didzis Elferts

Reputation: 98429

You can't do this directly from table object but you can use vector v to make barplot with function qplot() and geom="bar" and number of observation will be calculated automatically.

qplot(v,geom="bar")

Upvotes: 4

Related Questions