How to draw this plot? Any help please?

55 views
Skip to first unread message

Christofer Bogaso

unread,
Nov 10, 2010, 4:08:27 AM11/10/10
to ggp...@googlegroups.com

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,

Brandon Hurr

unread,
Nov 10, 2010, 4:48:56 AM11/10/10
to Christofer Bogaso, ggp...@googlegroups.com
I'll start by putting your data frame in a more readable manner.

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))



--
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 Hurr

unread,
Nov 10, 2010, 5:25:36 AM11/10/10
to Christofer Bogaso, ggp...@googlegroups.com
This is probably the best I can do. 


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, 

Brandon

Christofer Bogaso

unread,
Nov 10, 2010, 8:48:14 AM11/10/10
to Brandon Hurr, ggp...@googlegroups.com

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,

Brandon Hurr

unread,
Nov 10, 2010, 9:06:50 AM11/10/10
to Christofer Bogaso, ggp...@googlegroups.com
On Wed, Nov 10, 2010 at 13:48, 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?

No idea on that one. 

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.

?scale_x_discrete(), set your breaks and labels appropriately. 

Ista Zahn

unread,
Nov 10, 2010, 11:09:28 AM11/10/10
to Christofer Bogaso, Brandon Hurr, ggp...@googlegroups.com
Hi Christofer,

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

Bogaso Christofer

unread,
Nov 10, 2010, 12:59:09 PM11/10/10
to Ista Zahn, Brandon Hurr, ggp...@googlegroups.com
Thanks Ista for your reply. Yes this what I wanted to draw. However can you
please explain me what does " position_dodge()" mean? I have seen
?position_dodge, however really could not understand the meaning of it's
working. How it is working in the context of geom_point?

Thanks,

Ista Zahn

unread,
Nov 10, 2010, 1:23:24 PM11/10/10
to Bogaso Christofer, ggplot2
When you map a factor you an aesthetic you can end up with multiple
geoms that occupy the same x and y coordinates. Position adjustments
are used to tell ggplot2 what to do in this situation. Example:

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

Christofer Bogaso

unread,
Nov 12, 2010, 4:06:10 AM11/12/10
to ggp...@googlegroups.com
Dear friends, does there any option exist to fill color however instead a solid fill (as in the current plot), I want a some gradient in filling color. I know gradient in color is possible on basis of some attributes like worst->bad->good->best, however could not find any option to put gradient in fill color.
 
Any help?
 
Thanks,
Reply all
Reply to author
Forward
0 new messages