Reputation: 157
I have assets by manager in a data frame
Date C B A E D
2011-06-30 20449251 2011906 0 0 0
2011-09-30 20766092 1754940 0 0 0
2011-12-31 15242138 1921684 0 0 0
2012-03-31 15811841 2186571 0 0 0
2012-06-30 16221813 2026042 2423039 2419517 0
2012-09-30 16155686 2261729 2563734 1160693 0
2012-12-31 16297839 2231341 2592015 1151989 0
2013-03-31 14627046 2441132 2769681 1249464 0
2013-06-30 14186185 2763985 2615053 1260893 0
2013-09-30 14039954 2780167 2698988 1264244 0
2013-12-31 13832117 3081687 2962113 1318903 0
2014-03-31 14177177 3133202 3077684 1353243 0
2014-06-30 14503900 3235089 3196623 1415319 0
2014-09-30 12561057 3227862 3048216 1413446 2073068
I then melt and plot to get a stacked area graph
library('ggplot2')
library('reshape2')
colorscheme = scale_fill_brewer(type="qual",palette = 2)
df = melt(data,id.var="Date",variable.name="Manager")
df[,3] = as.numeric(df[,3])
#Stacked Area
layout(c(1,1))
p = ggplot(df,aes(x=Date,y=value,group=Manager,fill=Manager))+
geom_area(position="fill") + colorscheme
print(p)
and this works great:
Now I want a pie chart of the last row (i.e, current date)
df1 = data[nrow(data),-1]
df1 = as.data.frame(t(df1))
colnames(df1) = "AUM"
p = ggplot(df1,aes(x=1,y=df1$AUM,fill=rownames(df1))) +
geom_bar(stat="identity") + colorscheme + coord_polar(theta="y")
plot(p)
and I get the following:
Ignoring the formatting, my question is about the color selection. The colors don't match by manager. Manager A color in the area graph is now the color for Manager C. I realize it is because the pie chart is sorted by Manager name where as the Manager order in data
isn't sorted.
I don't have control of how I receive data. Is there way to reorder data
and/or df
(data melted) so that the first graph is in manager order? Or change the way data is sent to the pie chart?
Thanks,
Upvotes: 2
Views: 781
Reputation: 59345
Rather than messing around with factor levels, wouldn't it just be easier to subset df
by the Date
from the last row in data
??
ggplot(df[df$Date==tail(data,1)$Date,],aes(x=1,y=value,fill=Manager)) +
geom_bar(stat="identity") + colorscheme + coord_polar(theta="y")
Upvotes: 1