1

I'm trying to draw a vertical stacked bar plot in an ordered way as follows. Ordered Vertical stacked bar chart

This is the code I had used.

setwd('d:/Dataset/lynda')
unitcounts<-read.csv('ucounts.csv',header=T,sep=',')

library(reshape2)
prodf<-melt(unitcounts,id.var = "Units")
attach(prodf)
library(ggplot2)
ggplot(prodf,aes(x=Units,y=value,fill=variable))+geom_bar(stat="identity")
prodf

This is the output I had got. Could someone help me to arrange the vertical bar orderly? enter image description here

Units,Person1,Person2,Person3,Person4,Person5
Whatsits,54,64,31,43,47
Doohickeys,28,47,21,28,32
Gadgets,23,34,14,19,28
Bahbooms,0,0,0,0,0
Heehaws,0,0,0,0,0
Doodads,0,0,0,0,0
Meemaws,13,20,11,15,15
Watchamacalists,22,30,15,15,19
user227710
  • 3,164
  • 18
  • 35
Veeramani Natarajan
  • 192
  • 2
  • 4
  • 11
  • Your help is really appreciated and welcome. i'm trying this simple thing from past 4 hrs. please help me – Veeramani Natarajan Jul 10 '15 at 13:00
  • Please put the data into the question body (edit button). – tonytonov Jul 10 '15 at 13:10
  • It is not clear to me what you are asking? do you want to change the ordering of the legend (from Person 5 to Person one instead of 1 to 5)? Or do you want to change the arrangement on your x-axis (called units)? – mts Jul 10 '15 at 13:19
  • i wish to change the order of x axis from low to high value – Veeramani Natarajan Jul 10 '15 at 13:21
  • for example bashooms,doodads,heehaws,Meemaws,Watchamacalists,gadgets,doohiskeys,whatsits – Veeramani Natarajan Jul 10 '15 at 13:23
  • 1
    http://stackoverflow.com/questions/3253641/how-to-change-the-order-of-a-discrete-x-scale-in-ggplot tells you all you need. also consider posting your data in a usable way, see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – mts Jul 10 '15 at 13:40
  • 1
    Low to high value of what? Of the total? Of the highest value within a single x? You could make Units a factor and reorder it. – Heroka Jul 10 '15 at 13:43
  • please look the output. The x axis was arranged in alphabetic order. i want x axis to be arranged in ascending order with respect to value of y axis. im expecting the vertical bar chart in the following order. First bar- Bahbooms, second bar - Doodads, third bar-Heehaws, 4th bar-Meemaws, 5th bar-Watchamacalists, 6th bar-Gadgets,7thbar-Doohickeys and last bar as whatsits. Height of any bar for an item (Units)=person1+person2+person3+person4+person5 and i want to arrange the height of bar in an increased manner. plz help me – Veeramani Natarajan Jul 10 '15 at 15:15

1 Answers1

0
#calculate total
unitcounts$total <- rowSums(unitcounts[,-1])


#reorder factor levels by total
unitcounts$Units <- factor(unitcounts$Units,  levels=unitcounts$Units[order(unitcounts$total)])

#include total in id.vars, doesn't need to be plotted
prodf<-melt(unitcounts,id.var = c("Units","total"))


ggplot(prodf,aes(x=Units,y=value,fill=variable))+geom_bar(stat="identity")
Heroka
  • 12,889
  • 1
  • 28
  • 38