Reputation: 205
The data I have been given already has the total counts for the categories. I am trying to make a histogram of the number of houses for each com and age where Houses is the total count for that category.
Com<-c( "Newport", "Newport", "Newport", "Newport", "Newport", "Newport", "Topeka", "Topeka", "Topeka", "Topeka", "Topeka", "Topeka", "Missoula", "Missoula", "Missoula", "Missoula", "Missoula", "Missoula" )
Age<-c( "1970s", "1960s", "1950s", "1940s", "1940_earlier", "1990s", "1970s", "1960s", "1950s", "1940s", "1940_earlier", "1990s", "1970s", "1960s", "1950s", "1940s", "1940_earlier", "1990s" )
Houses<-c( 11, 6, 3, 0, 0, 21, 44, 0, 3, 3, 25, 20, 0, 51, 236, 192, 312, 299 )
df=data.frame(Com,Age,Houses)
So df is the data
histogram( ~ Age | Com, data=df)
I have also tried
install.packages("ggplot2")
library(ggplot2)
g <- ggplot(df$counts, aes(df$Age))
g + geom_bar()
Also
barplot(prop.table(table(df$Age)))
And lastly
p <- ggplot(data = df, aes(x=Age))
p <- p + geom_histogram(aes(weights=Houses, fill=Com))
p <- p + scale_fill_brewer(palette="Set3")
p <- p + facet_wrap( ~ Com, ncol=1)
p
Here is my R version information:
R.Version()
$platform
[1] "x86_64-w64-mingw32"
$arch
[1] "x86_64"
$os
[1] "mingw32"
$system
[1] "x86_64, mingw32"
$status
[1] ""
$major
[1] "3"
$minor
[1] "3.0"
$year
[1] "2016"
$month
[1] "05"
$day
[1] "03"
$`svn rev`
[1] "70573"
$language
[1] "R"
$version.string
[1] "R version 3.3.0 (2016-05-03)"
$nickname
[1] "Supposedly Educational"
Upvotes: 2
Views: 1258
Reputation: 263499
The lattice function is barchart:
library(lattice)
barchart( Houses ~ Age ,group=Com, data=df)
The base barplot solution requires that the data be in a matrix, table or xtabs object. By defatul you get a stacke bar plot but if you wnat the same as the lattice code above you add the beside argument:
barplot(xtabs(Houses~Com+Age, data=df), beside=TRUE)
Upvotes: 1
Reputation: 227041
When using geom_bar()
in ggplot2
with totals that have already been computed (rather than needing to count or sum cases), you need to specify stat="identity"
. How about
g0 <- ggplot(df,aes(Age,Houses))+
geom_bar(stat="identity")+
facet_wrap(~Com)
print(g0)
? or
ggplot(df,aes(Age,Houses))+
geom_bar(stat="identity")+
coord_flip()+
facet_wrap(~Com,ncol=1)
Upvotes: 3