Stacked Bar chart

42 views
Skip to first unread message

Thiago Silva Teles

unread,
Dec 2, 2015, 10:40:53 AM12/2/15
to ggplot2
Dear All,
I need to do a stacked bar chart with these arguments:

 B_tot  B_turn  B_nest   scale
 0.98    0.96     0.02       Local
 0.94    0.90     0.04      Habitat

But I am not getting!!!

Can someone help me?

Thiago.

Ben Bond-Lamberty

unread,
Dec 2, 2015, 11:02:25 AM12/2/15
to Thiago Silva Teles, ggplot2
Hi Thiago,

You first need to reshape your data, and then plot it. I'm taking a
guess here at what you want, but for example:

# Your data frame
d <- structure(list(B_tot = c(0.98, 0.94), B_turn = c(0.96, 0.9),
B_nest = c(0.02, 0.04), scale = structure(c(2L, 1L), .Label = c("Habitat",
"Local"), class = "factor")), .Names = c("B_tot", "B_turn",
"B_nest", "scale"), class = "data.frame", row.names = c(NA, -2L))

# first reshape
library(tidyr)
d1 <- gather(d, B, value, -scale)

# and then plot
ggplot(d1, aes(scale, value, fill=B)) + geom_bar(stat='identity')


Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Crump, Ron

unread,
Dec 2, 2015, 11:12:20 AM12/2/15
to Thiago Silva Teles, ggplot2
Thiago,

> B_tot B_turn B_nest scale
> 0.98 0.96 0.02 Local
> 0.94 0.90 0.04 Habitat
>
>But I am not getting!!!
>
>Can someone help me?

Probably, but it would be much easier if you had described what you wanted
to do more clearly (e.g. what is on the x-axis? What is being stacked?)
and supplied your data in an easily usable form (the output from dput).

Anyway, I have assumed you didn't want the total to be part of the bars
and have made a data.frame, d, which looks like this with dput(d)

structure(list(b = structure(c(2L, 2L, 1L, 1L), .Label = c("B_nest",
"B_turn"), class = "factor"), scale = structure(c(2L, 1L, 2L,
1L), .Label = c("Habitat", "Local"), class = "factor"), value = c(0.96,
0.9, 0.02, 0.04)), .Names = c("b", "scale", "value"), row.names = c(NA,
-4L), class = "data.frame")

If you copy and paste that after "d <-" (without the quotes) on the
command line then you will have d.

Then, assuming scale (Local and Habitat) should be on the x-axis:

ggplot(d)+geom_bar(aes(x=scale,y=value,fill=b),stat="identity")

If that isn't what you want, you'll probably be able to get it from that.

Ron.

Thiago Silva Teles

unread,
Dec 2, 2015, 12:34:45 PM12/2/15
to ggplot2
Hello everybody,
I'm very gratefull to you for helping me on that problem...
Sorry about the little information on the first post, but it's exactly what Mr. Ron's thought.

Thiago.
Reply all
Reply to author
Forward
0 new messages