Dear all, I want to create a complex plot (please see the link here: "http://img230.imageshack.us/img230/6857/snapu.png") using ggplot mechanism on my data:
> asd
[,1] [,2]
x1 6.228052e-18 -0.28313269
x2 6.426101e-18 -0.28902094
y1 2.282003e-01 0.01739011
y2 2.338601e-01 0.02151179
z 3.899442e-01 0.15097369
Here, x1, x2, y1, y2, and z are variables and each of those variables has 2 values as specified with different colors. I am sure that using advanced plotting mechanism like ggplot I can surely do this, however could not succeed. Can someone offer me some help?
Thanks,
--
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
asd<-structure(list(variable = structure(1:5, .Label = c("x1", "x2",
"y1", "y2", "z"), class = "factor"), value1 = c(6.23e-18, 6.43e-18, 0.228, 0.234, 0.39), value2 = c(-0.28313269, -0.28902094, 0.01739011,
0.02151179, 0.15097369), variable2 = structure(c(1L, 1L, 2L, 2L, 3L), .Label = c("x", "y", "z"), class = "factor"), variable3 = c(1L, 2L, 1L, 2L, 1L)), .Names = c("variable", "value1", "value2", "variable2", "variable3"), class = "data.frame", row.names = c(NA, -5L)) asd2<-melt(asd[1:3]) colnames(asd2)<-c("x", "y", "z") asd2$x<-factor(asd2$x, levels=c("z", "y2", "y1", "x2", "x1")) ggplot(data=asd2)+ geom_bar(aes(x=x, y=z, fill=y), position="dodge")+ geom_hline(y=0, colour="red")+ geom_vline(x=0, colour="red")+ geom_vline(x=1.5, colour="blue")+ geom_vline(x=3.5, colour="green")+ geom_vline(x=5.5, colour="yellow")+ theme_bw()+ coord_flip()+ opts(panel.grid.major=theme_blank() , panel.grid.minor=theme_blank() , panel.background=theme_rect(colour="black"))HTH,
Thanks Brandon for your help. It is awesome!!! However here I wanted to make few more additions:
ggplot(data=asd2)+
geom_bar(aes(x=x, y=z, fill=y), position="dodge")+
geom_hline(y=0, colour="red")+
geom_point(aes(x=x, y=z, color=y), size=10, alpha=0.6) + coord_flip()
1. As you see I could not find the coordinates correctly to plot the points i.e. the x-ordinates. My intention is to put the underlying values on those circles correctly. Therefore I need to know the coordinates. How I can I get that?
2. In the x-axis (before flipping), tick-marks are named same as the variables. However is there any way to put user-defined names? Means currently it is showing x1, x2, y1, y2, and z. I want to write: x, x, y, y, and z.
Really appreciate if somebody show me the way on how can I achieve those.
Thanks,
Thanks Brandon for your help. It is awesome!!! However here I wanted to make few more additions:
ggplot(data=asd2)+geom_point(aes(x=x, y=z, color=y), size=10, alpha=0.6) + coord_flip()
geom_bar(aes(x=x, y=z, fill=y), position="dodge")+
geom_hline(y=0, colour="red")+
1. As you see I could not find the coordinates correctly to plot the points i.e. the x-ordinates. My intention is to put the underlying values on those circles correctly. Therefore I need to know the coordinates. How I can I get that?
2. In the x-axis (before flipping), tick-marks are named same as the variables. However is there any way to put user-defined names? Means currently it is showing x1, x2, y1, y2, and z. I want to write: x, x, y, y, and z.
On Wed, Nov 10, 2010 at 8:48 AM, Christofer Bogaso
<bogaso.c...@gmail.com> wrote:
> Thanks Brandon for your help. It is awesome!!! However here I wanted to make
> few more additions:
>
> ggplot(data=asd2)+
> geom_bar(aes(x=x, y=z, fill=y), position="dodge")+
> geom_hline(y=0, colour="red")+
> geom_point(aes(x=x, y=z, color=y), size=10, alpha=0.6) + coord_flip()
>
> 1. As you see I could not find the coordinates correctly to plot the points
> i.e. the x-ordinates. My intention is to put the underlying values on those
> circles correctly. Therefore I need to know the coordinates. How I can I get
> that?
If I understand correctly what you want, then no, you don't kneed to
know coordinates. Just do
geom_text(aes(x=x, y=z, label=round(z, digits=2)),
position=position_dodge(width=.9), size=3)
or so.
While I'm at it, here are two alternative versions. The first one
retains some of the original elements in your original example, the
second one ditches the bars and the dodging, both of which I think add
little or nothing.
asd<- structure(list(variable = structure(1:5, .Label = c("x1", "x2",
"y1", "y2", "z"), class = "factor"), value1 = c(6.23e-18, 6.43e-18,
0.228, 0.234, 0.39), value2 = c(-0.28313269, -0.28902094, 0.01739011,
0.02151179, 0.15097369)), .Names = c("variable", "value1", "value2"),
class = "data.frame", row.names = c(NA, -5L))
asd2<-melt(asd[1:3])
colnames(asd2)<-c("x", "y", "z")
asd2$x<-factor(asd2$x, levels=c("z", "y2", "y1", "x2", "x1"))
levels(asd2$x) <- c("z1", "y2", "y1", "x2", "x1")
asd3 <- cbind(colsplit(asd2[,1], split="", names=c("var", "x")), asd2[,-1])
asd3$x <- factor(asd3$x)
ggplot(data=asd3, aes(x=x, y=z, fill=y))+
geom_bar(position="dodge", stat="identity")+
geom_hline(y=0, colour="red")+
geom_point(aes(color=y), size=10, alpha=.5,
position=position_dodge(width=.9)) +
geom_point(aes(fill=NULL), color="black", size=10,
position=position_dodge(width=.9), shape=21) +
geom_text(aes(x=x, y=z, label=round(z, digits=2)),
position=position_dodge(width=.9), size=3) +
coord_flip() +
facet_grid(var ~ .) +
theme_bw()
ggplot(data=asd3, aes(x=x, y=z, fill=y))+
geom_hline(y=0, colour="red")+
geom_point(aes(color=y), size=10, alpha=.5) +
geom_point(aes(fill=NULL), color="black", size=10, shape=21) +
geom_text(aes(label=round(z, digits=2)), size=3) +
coord_flip() +
facet_grid(var ~ .) +
theme_bw()
-Ista
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org
Thanks,
dat <- expand.grid(x1=c("A", "B"), x2=c("C", "D"))
dat$y <- 1:nrow(dat)
p <- ggplot(dat, aes(x=x1, y=y, color=x2, fill=x2))
p + geom_bar(position="identity", alpha=.2) ## plot the bars right on
top of each other
p + geom_bar(position="stack", alpha=.2) ## stack the bars one on top
of the other
p + geom_bar(position="fill", alpha=.2) ## stack the bars one on top
of the other and standardize to equal height
ggplot(dat, aes(x=x1, y=y, color=x2, fill=x2)) +
geom_bar(position="dodge", alpha=.2) ## stack the bars one next to the
other.
## This works with different geoms, except that apparently geom_text
has no width so we have to specify manually
p + geom_text(aes(label=c("hel", "lo", "wor", "ld")), position="identity")
p + geom_text(aes(label=c("hel", "lo", "wor", "ld")), position="stack")
p + geom_text(aes(label=c("hel", "lo", "wor", "ld")),
position="dodge") ## Doesn't do what we want (because geom_text has no
width?). So we specify the width argument to position_dodge:
p + geom_text(aes(label=c("hel", "lo", "wor", "ld")),
position=position_dodge(width=1))
-ista