[R] Stacked Barchart as relative share

Ista Zahn istazahn at gmail.com
Thu Nov 8 13:49:12 CET 2012


Hi

On Thu, Nov 8, 2012 at 6:23 AM, Geophagus <fh at retposto.net> wrote:
> Hi @ all,
> I'm looking for a solution to plot a dataframe as a stacked bar chart like
> on this picture:
> http://4.bp.blogspot.com/_79SognVSu7A/S6OtzgksPSI/AAAAAAAABrw/-IuFNewdZFE/s400/Stacked%2BBar%2BChart.png
>
> My dataframe
> example2.csv <http://r.789695.n4.nabble.com/file/n4648854/example2.csv>
>
> My questions:
> Is there a standard function to convert the values to 100 percent relate to
> realize the stacked chart

This is R, so there will be many ways to do it. I don't know what is
considered standard, but I would do

dat <- read.delim("http://r.789695.n4.nabble.com/file/n4648854/example2.csv")
dat[-1] <- apply(dat[-1], 2, function(x) (x/sum(x))*100)


 or does a library generate the complete plot
> inculding an automatically conversion?
>
> I need the states (WA, MT, ND etc.) on the x-axis and the distribution of
> the values (related to 100 %) on the y-axis (stacked) to compare the shares
> per state.

Here is one way:

library(ggplot2)
library(reshape2)
dat.m <- melt(dat, id.vars="X")
ggplot(dat.m, aes(x=variable, y=value, fill=X)) + geom_bar(stat="identity")

Best,
Ista

>
> I'm hoping for help from you. Thanks a lot.
> GeO
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Stacked-Barchart-as-relative-share-tp4648854.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.




More information about the R-help mailing list