moran scatterplot in ggplot

343 views
Skip to first unread message

Alessandra Carioli

unread,
Apr 24, 2014, 5:52:21 PM4/24/14
to ggplot2
Dear bloggers,

I was looking for a way to improve my moran’sI scatterplot and I came across this post http://docs.ggplot2.org/current/geom_point.html. What I want to do is to realize a scatterplot which has in each “quadrant" a different color (red, pink, blue and lightblue clockwise) for the border of the points and a different fill depending on the quadrant AND the significance (white=non significant, red, pink, blue and light blue if significant).
How can I control for that?

df is my data frame
sec gives the color of each sector (1=red, 2=pink, 3=blue, 4=lightblue)
loc.m.data is a 0-1 var with 1 for significant and 0 non significant

p <- ggplot(df, aes(x, y))
p + geom_point(colour=df$sec, size = 4) + geom_point(aes(colour = df$loc.m.data))

Any help on the matter will be greatly appreciated!

Ale


Ben Bond-Lamberty

unread,
Apr 24, 2014, 7:05:51 PM4/24/14
to Alessandra Carioli, ggplot2
Well for starters you're missing an aes() call, should be:

p + geom_point(aes(colour=sec), size = 4) + geom_point(aes(colour = loc.m.data))

Does this help?

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.

Dennis Murphy

unread,
Apr 25, 2014, 2:40:30 AM4/25/14
to Alessandra Carioli, ggplot2
Hi:

You can use the fill aesthetic to color the quadrants and the color
aesthetic to control the points. Since you didn't provide a
reproducible example (which includes data and not just code), the
following is a toy example to illustrate how to get different fill
colors in different quadrants. I'll leave the point problem as
homework (think about creating a factor to distinguish the four
situations you outlined). You probably want to avoid white as a point
color, but you can discover that for yourself.

# Data frame for the points
DF <- data.frame(x = rnorm(20), y = rnorm(20))

# Data frame to distinguish the rectangle boundaries
# for the quadrants; notice the variable that defines colors
DFrect <- data.frame(xmin = c(-Inf, -Inf, 0, 0),
xmax = c(0, 0, Inf, Inf),
ymin = c(0, -Inf, 0, -Inf),
ymax = c(Inf, 0, Inf, 0),
fcol = c("red", "blue", "pink", "lightblue"))

# Plot call
ggplot() +
geom_rect(data = DFrect, aes(xmin = xmin, xmax = xmax, ymin = ymin,
ymax = ymax, fill = fcol), alpha = 0.2) +
geom_point(data = DF, aes(x = x, y = y)) +
scale_fill_identity() +
xlim(-4, 4) + ylim(-4, 4)

I left the arguments to ggplot() empty since the aesthetics required
for geom_rect() are different from those for geom_point(). This
example illustrates how to use different data frames and map different
aesthetics in distinct layers.

scale_fill_identity() is used when the values of the variable are to
be passed literally. By default, it does not produce a legend.

Also note that I used alpha = 0.2 in the geom_rect() call. You want
alpha transparency in the fill color so that the grid lines are
visible in the background. If you use the default alpha = 1, the
rectangle fill will completely obscure the grid lines. As a result,
you may want to play around with different colors and alpha levels to
get something that works for you.

Dennis

Alessandra Carioli

unread,
Apr 25, 2014, 4:42:11 AM4/25/14
to Dennis Murphy, ggplot2
Thanks for your answers, though what I am looking for is something like this, but the white circles would have a different border color according to the quadrant (below I pasted the reproducible sample for the first 20 rows). The lines defining the ‘quadrants’ are the mean of x and y, hope it’s clearer now


df<-structure(list(varofint = c(0.128538244, 0.14712414, 0.145712646, 
0.160557562, 0.17206143, 0.175358761, 0.137244873, 0.165770524, 
0.167141675, 0.144745435, 0.174987536, 0.188380221, 0.241855377, 
0.152522933, 0.169505684, 0.168409125, 0.166583661, 0.151776624, 
0.174131678, 0.138582423), varlag = c(0.1316202424, 0.12668247825, 
0.162400614, 0.175400413142857, 0.182965888428571, 0.1756358724, 
0.183587295777778, 0.191959063857143, 0.167862518571429, 0.1715397966, 
0.1835250595, 0.181639449181818, 0.178611945571429, 0.181671086333333, 
0.176827375333333, 0.1839786636, 0.169840743, 0.174295818, 0.165516117545455, 
0.162470029), sec = c(2, 2, 3, 3, 1, 1, 3, 1, 1, 3, 1, 1, 1, 
3, 1, 1, 1, 3, 1, 3), sec.all = c(2, 2, 3, 3, 1, 1, 3, 1, 1, 
3, 1, 1, 1, 3, 1, 1, 1, 3, 1, 3), loc.m.data = c(2, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c(“x", 
“y", "sec", "sec.all", "loc.m.data"), row.names = c("101", 
"102", "201", "202", "203", "204", "205", "206", "207", "301", 
"302", "303", "304", "305", "306", "307", "308", "309", "310", 
"311"), class = "data.frame”)

Alessandra Carioli

unread,
Apr 25, 2014, 6:22:02 AM4/25/14
to Dennis Murphy, ggplot2
I used the suggestions and come to this, though I think it can still be improved (like erase legend number 2)

df$FILL <- factor(df$loc.m.data+1) #to avoid 0s fill factor
df$BORDER <- factor(df$sec) #border factor

p <- ggplot(df, aes(x, y))
p +geom_point(aes( colour=df$BORDER, fill=df$FILL),shape=21,size=6)+
scale_colour_manual(name="",values = c("1"="red", "2"="blue","3"="lightblue","4"="pink"))+
scale_fill_manual(name="",values = c("1"="white", "2"="red", "3"="blue","4"="lightblue","5"="pink"),labels = c("p-value>0.05", "High-High", "Low-Low","Low-High","High-Low"))+
stat_smooth(method="lm",se=F,colour="black", size=1)+
geom_vline(xintercept=mean(x),colour="black",linetype="longdash")+
geom_hline(yintercept=mean(y),colour="black",linetype="longdash")


On Apr 25, 2014, at 8:40, Dennis Murphy <djm...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages