r - Create a barplot of two tables of differing length -
i can not seem figure out how nice barplot contains data 2 tables contain different number of columns.
the tables in question (snipped data end):
> tab1 1 2 3 6 8 31 5872 1525 831 521 299 4 > tab2 1 2 3 4 22 7874 422 2 5 1
note column names , sizes different. when barplot()
on 1 of these tables comes out plot i'd (showing column names x-axis, frequencies on y-axis). but, these 2 side side.
i've gotten far creating data frame containing both variables comments , different row names in first column (with data.frame()
and merge()
), when plot x-axis seems wrong. attempting reorder columns gives me exception lengths differing.
code:
combined <- merge(data.frame(tab1), data.frame(tab2), = c('var1'), all=t) barplot(t(combined[,2:3]), names.arg = combined[,1], beside=t)
this shows plot, not labels present , value position 26 plotted after 33.
is there simple way plot working? ggplot2 solution nice.
you can put data in 1 data frame (as in example).
df<-data.frame(group=rep(c("a","b"),times=c(2,3)), values=c(23,56,345,6,7),xval=c(1,2,1,2,8)) group values xval 1 23 1 2 56 2 3 b 345 1 4 b 6 2 5 b 7 8
then ggplot()
geom_bar()
can used plot data.
ggplot(df,aes(xval,values,fill=group))+ geom_bar(stat="identity",position="dodge")
Comments
Post a Comment