0

Good day,

I have a data frame such as:

sample.df = data.frame(a=c(-1,1,0,-1),b=c(2,NA,1,2),c=c(0,0,1,2),d=c(-1,-2,0,0))

and I would like to produce a stacked barplot for each column showing the number of times each unique values occurs in that column (ignoring NAs).

My first thought is to create a new data frame with the possible values as the rownames (depicted here as Score) and the data values being the count of times that value occurs for that column:

Score  a  b  c  d
2      0  2  1  0
1      1  1  1  0
0      1  0  2  0
-1     2  0  0  1
-2     0  0  0  1

I've tried using ddply, table and aggregate from other examples but don't see a way to get to that structure.

My thought is that once I have that it should be straightforward to hand it to barplot to get the stacked barplot showing the number of occurrences of each value in each column.

I appreciate any guidance you can give me.

Thank you,

Dave

To close the loop on this, here is where I ended up.

I used melt as suggested by tcash21 ("Score" wasn't in the original data just attributes a, b, c and d). Also, ggplot() gave me an error about using "y" and stat="bin" so I removed the assignment of "y" in the ggplot() call. Here are the steps I ended up taking to get to the desired plot:

sample.df = data.frame(a=c(-1,1,0,-1),b=c(2,NA,1,2),c=c(0,0,1,2),d=c(-1,-2,0,0))
new.df<-melt(sample.df)
new.df<-new.df[complete.cases(new.df),]
new.df$Score<-as.factor(new.df$value)
ggplot(new.df, aes(x=variable, fill=Score)) + geom_bar()
4

1 に答える 1

2

データを ggplot の適切な長い形状にするには、melt を使用する必要があります。

new.df<-melt(sample.df, id.vars="Score")
ggplot(new.df, aes(x=Score, y=value, fill=variable)) + geom_bar()
于 2013-09-25T17:01:24.357 に答える