I'm creating 100% stacked bars, as shown here
(http://www.bertelsen.ca/R/ggplot2-group/example 1.jpg
<http://www.bertelsen.ca/R/ggplot2-group/example%201.jpg>) I've had to
fuzz a few things out because the data is proprietary.
What I would like to do, is stack 2010 and 2011 bars on top of each
other. Kind of like this
(http://www.bertelsen.ca/R/ggplot2-group/example 2.jpg
<http://www.bertelsen.ca/R/ggplot2-group/example%202.jpg>) but rotated
90% and with labels accompanying each bar segment with the rel
proportion of the total bar.
Here is the code from my best attempt (produces example 2, above).
Unfortunately, I can only provide the structure of my data.frame, I
can't provide actual numbers:
str(df.tmp)
'data.frame': 240 obs. of 7 variables:
$ variable: Factor w/ 30 levels "1","2","3","4",..: 1 1 1 1 2 2 2 2 29
29 ... # A company name
$ value : Factor w/ 4 levels "1","2","3","4": 1 2 3 4 1 2 3 4 1 2
... # The respondents selection
$ frequ : num 67 125 74 15 42 116 137 47 4 20 ... # Frequency of
respondent selection
$ cfreq : num 0.238 0.683 0.947 1 0.123 ... # Cumulative frequency
for bar positioning
$ pos : num 0.1192 0.4609 0.8149 0.9733 0.0614 ... # Again, bar
positioning
$ freq : num 0.2384 0.4448 0.2633 0.0534 0.1228 ... #
$ year : Factor w/ 2 levels "2010","2011": 1 1 1 1 1 1 1 1 1 1 ...
My code:
panel.1 <- list(opts(panel.background=theme_blank(),
panel.grid.minor=theme_blank(), panel.grid.major=theme_blank(),
axis.text.x=theme_blank(), legend.position="right",
axis.ticks=theme_blank())
plot.tmp <- ggplot(df.tmp, aes(year,frequ, fill=value)) +
geom_bar(stat="identity", position="fill") +
scale_y_continuous("", formatter="percent") +
geom_bar(position="dodge") +
labs(x="",y="", fill="") +
panel.1 +
opts(title="Work Damnit!") +
facet_grid(. ~ variable)
Now this works, but adding labels (as follows) to the plot breaks the
output with an error message.
+ geom_text(data=df.tmp, size=3 ,aes(variable,pos, label=ifelse(freq*100
> 4,paste(round(freq*100,digits=0),"%",sep=""),"")))
Help is much appreciated!
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
Brandon Bertelsen Office: 647-722-0481 Cell: 647-287-1009
ggplot(df.tmp, aes(factor(year),frequ, fill=variable)) +
geom_bar(stat="identity", position=c("stack")) +
facet_grid(. ~ value)+
geom_text(data=df.tmp, size=3 ,aes(factor(year), freq, label=ifelse(freq*100 > 4,paste(round(freq*100,digits=0),"%",sep=""),"")))
When I switch the two, the locations of the text annotations don't work right, perhaps they need to be calculated differently?
Good luck,
B