ggplot2 - Creating stacked barplots in R using different variables -
i novice r user, hence question. refer solution on creating stacked barplots r programming: creating stacked bar graph, variable colors each stacked bar.
my issue different. have 4 column data. last column summed total of first 3 column. want plot bar charts following information 1) summed total value (ie 4th column), 2) each bar split relative contributions of each of 3 column.
i hoping help.
regards, bernard
if understood rightly, may trick
the following code works example df dataframe
df <- b c sum 1 9 8 18 3 6 2 11 1 5 4 10 23 4 5 32 5 12 3 20 2 24 1 27 1 2 4 7
as don't want plot counter of variables, actual value in dataframe, need use goem_bar(stat="identity") method on ggplot2. data manipulation necessary too. , don't need sum column, ggplot sum you.
df <- df[,-ncol(df)] #drop last column (assumed sum one) df$event <- seq.int(nrow(df)) #create column indicate values happaned on same column each variable df <- melt(df, id='event') #reshape dataframe make readable gpglot px = ggplot(df, aes(x = event, y = value, fill = variable)) + geom_bar(stat = "identity") print (px)
this code generates plot bellow
Comments
Post a Comment