Thanks a lot. This is really helpful.
On Thu, May 15, 2014 at 8:12 PM, Dennis Murphy <djmu...@gmail.com> wrote: > Hi: > > There are several options, but you shot yourself in the foot by using > cbind() to combine objects of different classes into a matrix, so > everything ends up being a factor because a matrix is an atomic object > with a dim attribute, and atomic objects must have a common class, so > Flows and Category are coerced to factors. What you want is to combine > your atomic objects into a data frame, since that is what ggplot() > requires: > > #------ > Date <- c("2014-04-01","2014-04-02","2014-04-03","2014-04-04","2014-04-07", > "2014-04-09","2014-04-10","2014-04-01","2014-04-02","2014-04-03", > "2014-04-04","2014-04-07","2014-04-09","2014-04-10") > > Flows <- c(479.6, 187.2, 148.6, 41.5, 123.5, 176.3, 68.3, 401.5, > -164.2, -195.5, 35.1, -224.0, -58.6, -138.9) > > Category <- c("Equity", "Equity", "Equity", "Equity", "Equity", "Equity", > "Equity", "Debt", "Debt", "Debt", "Debt", "Debt", > "Debt", "Debt") > > # Use data.frame, not cbind > DF <- data.frame(Date, Flows, Category) > > #----- > > Your ggplot() code will "work", but probably not the way you intended. > Here are a couple of versions of your initial plot: > > #------ > library(ggplot2) > > # Initial plot, stacked > ggplot(data = DF, aes(x = Date, y = Flows, fill = Category)) + > geom_bar(stat="identity") > > # Initial plot, dodged > ggplot(data = DF, aes(x = Date, y = Flows, fill = Category)) + > geom_bar(stat="identity", position = "dodge") > #------ > > > I'm guessing what you want is to 'stack' the dodged version above; if > so, you need to plot the two groups separately and create a fill > factor 'on the fly': > > #--- > # "Stacking" the dodged plot - requires separate calls > # to each subset so that zero is the anchor > > ggplot(data = DF, mapping = aes(x = Date, y = Flows)) + > geom_bar(data = subset(DF, Category == "Equity"), > stat = "identity", aes(fill = "Equity")) + > geom_bar(data = subset(DF, Category == "Debt"), > stat = "identity", aes(fill = "Debt")) + > scale_fill_manual("Category", > values = c("Equity" = "blue", "Debt" = "red")) > #--- > > > If you want to plot on the actual dates rather than the given dates, > and perhaps to change the date format since the year is common, here > is one way to do it: > > #--- > # Convert to Date format so that we can plot by actual date > DF$Date <- as.Date(as.character(DF$Date)) > > # Need scales package to change date format > library(scales) > > ggplot(data = DF, mapping = aes(x = Date, y = Flows)) + > geom_bar(data = subset(DF, Category == "Equity"), > stat = "identity", aes(fill = "Equity")) + > geom_bar(data = subset(DF, Category == "Debt"), > stat = "identity", aes(fill = "Debt")) + > scale_fill_manual("Category", > values = c("Equity" = "blue", "Debt" = "red")) + > scale_x_date(labels = date_format("%m-%d")) > #--- > > > > I hope this covers your use case. > > Dennis > > On Thu, May 15, 2014 at 4:32 AM, Vikram Bahure > <economics.vik...@gmail.com> wrote: > > Hi, > > > > I am facing issue with ggplot plotting. I have given the data and the > code > > below. I have also attached the graph below. > > > > Problem: > > On date "*2014-04-07*", we have debt and equity flow, but it does not > shows > > equity flow. Ideally what I would like is the stack for debt/equity > starts > > from X axis. > > > > *## Data* > > *Date <- > > c("2014-04-01","2014-04-02","2014-04-03","2014-04-04","2014-04-07",* > > * > > "2014-04-09","2014-04-10","2014-04-01","2014-04-02","2014-04-03",* > > * "2014-04-04","2014-04-07","2014-04-09","2014-04-10")* > > *Flows <- c(479.6,187.2, 148.6, 41.5, 123.5, 176.3, 68.3, 401.5,* > > * -164.2, -195.5, 35.1, -224.0, -58.6, -138.9)* > > *Category <- c("Equity", "Equity", "Equity", "Equity", "Equity", > "Equity",* > > * "Equity", "Debt", "Debt", "Debt", "Debt", "Debt",* > > * "Debt", "Debt")* > > *data <- cbind(Date,Flows,Category)* > > > > *## GGplot* > > *ggplot(data = fii.df, aes(x = Date, y = FII, fill = Category)) + > > geom_bar(stat="identity") * > > > > Thanks in advance. > > > > Regards > > Vikram Bahure > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.