Is it possible to do this?

9 views
Skip to first unread message

LSelzer

unread,
Aug 16, 2009, 9:07:59 PM8/16/09
to ggplot2
Hi to all of you,
I was thinking of doing a bar graph but I didn't know how to do it.
The graph I'm thinking about it's a bargraph with stacked bars for one
variable AND dodged for a different one. I don't know if I'm
explaining myself correctly here, but I can upload a drawing if it's
necessary.

Thank you.

hadley wickham

unread,
Aug 17, 2009, 8:51:33 AM8/17/09
to LSelzer, ggplot2
Like this?

ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
geom_bar(subset = .(cyl == 4)) +
geom_bar(subset = .(cyl != 4), position = "dodge")

It doesn't seem like it's going to be a terribly effective display to me.

Hadley
--
http://had.co.nz/

Luciano Selzer

unread,
Aug 17, 2009, 11:45:17 AM8/17/09
to hadley wickham, ggplot2
Hi Hadley,
thanks for your quick reply, but no, the graph you made it's not what I was thinking about. And I agree it wouldn't be a good way to display the data.
I was thinking something like the example drawing I did. It's just a 5 minute sketch so it isn't tidy. I think this way is going to be efective. You see, I have a var that's the sum two others, so var1=var2+var3. And the experiment was a two factorial one, so it makes sense assinging one factor to the x axis and different strips to the other.

2009/8/17 hadley wickham <h.wi...@gmail.com>



--
Luciano
example.png

Mike Lawrence

unread,
Aug 17, 2009, 2:31:11 PM8/17/09
to Luciano Selzer, hadley wickham, ggplot2
As noted earlier on this list, best practice is to include an example
data set. Here's a hacky way of creating one, then code to create the
graph you illustrated:

seed = 1
temp = expand.grid(
A = c('A1','A2')
, B = c('B1','B2')
, C = c('C1','C2')
)
temp$temp = sample(1:8)

my_data = NULL
for(i in 1:length(temp$temp)){
for(j in 1:temp$temp[i]){
my_data = rbind(my_data,temp[i,1:3])
}
}

ggplot(
data = my_data
, mapping = aes(
x = A
, fill = B
)
)+
geom_bar()+facet_grid(.~C)
--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tr.im/mikes_public_calendar

~ Certainty is folly... I think. ~
Reply all
Reply to author
Forward
0 new messages