Reputation: 3
I'm trying to draw several plots with R, but I'm new to it. My data looks like this:
Item Count Type
Apple 118 A
Orange 63 A
Pear 126 A
Plum 193 A
Lemon 240 A
Peas 46 B
Beans 87 B
Carrot 171 B
Onion 123 B
Poatato 35 B
Cheese 44 C
Eggs 13 C
Ham 31 C
Fish 10 C
And I want to make a different histogram for each type of item (A, B and C) plotting the count values. I managed to plot overlapping histograms:
ggplot(myfile, aes(x= Count, fill = Type))+ geom_histogram (binwidth = 10, alpha = 0.5, position = "identity")
But I wanted to know if it is possible to plot separate histograms, as many as different types exist in the data.
Upvotes: 0
Views: 752
Reputation: 6534
Is this what you are after?
data <- read.table(text="Item Count Type
Apple 118 A
Orange 63 A
Pear 126 A
Plum 193 A
Lemon 240 A
Peas 46 B
Beans 87 B
Carrot 171 B
Onion 123 B
Poatato 35 B
Cheese 44 C
Eggs 13 C
Ham 31 C
Fish 10 C",header=TRUE)
library(ggplot2)
ggplot(data, aes(x= Item, y = Count, fill = Type)) +
geom_bar(alpha = 0.5, stat = "identity") +
facet_wrap(~ Type, ncol = 1)
Upvotes: 2
Reputation: 94202
This is called 'faceting'. Try:
ggplot(myfile, aes(x= Count, fill = Type))+ geom_histogram (binwidth = 10, alpha = 0.5, position = "identity")+facet_grid(~Type)
And read the help for all the faceting functions in the ggplot
documentation.
Upvotes: 0