Change manually the shapes of points

3,058 views
Skip to first unread message

María Piquer-Rodríguez

unread,
Dec 4, 2013, 8:36:25 AM12/4/13
to ggp...@googlegroups.com
Dear all,
I would like to change manually the shape of my point plot. This script below worked before but it does not anymore-It does plot the manual color but not the shapes.
I would like to plot a square, circle, triangle, square, circle, triangle, and so on. Any suggestion would be very appreciated. Thanks in advance, Maria

ggplot(graph_prox, aes(log_Ha_conv, PROX_MN))+
  geom_point( aes(colour=factor (Scenarios)),size = 4)+
  scale_color_manual(values=c("#006633", "#009900", "#00FF66",
                              "#CC9900", "#FFCC00","#FFFF66",
                              "#003399", "#0066CC", "#0099CC",
                              "#9900CC", "#CC00FF", "#FF00FF"))+
  scale_shape_manual(values = c(15, 16, 17, 15,16,17,15,16,17,15,16,17))

Dennis Murphy

unread,
Dec 4, 2013, 2:00:54 PM12/4/13
to María Piquer-Rodríguez, ggplot2
Hi:

You did not provide a reproducible example, so the one offered below
is a guess at what your intended example contains. From appearances,
the length of the two vectors fed as values in the scale functions are
the same length as the data points, so one option is to put those
values into the data frame itself prior to plotting. In my example
below, the colors appear to be unique, so I did not assign a factor to
it, even though you appear to have done so in your examples. OTOH,
since the shape values were repeated, it made sense to define a
corresponding factor. This way I could illustrate both
scale_identity() and scale_manual() and let you decide which one you
prefer.

library(ggplot2)

# Reproducible example (mod the random seed)
DF <- data.frame(x = rnorm(12), y = rnorm(12),
shpfac = factor(1:3, levels = LETTERS[1:3]),
col = c("#006633", "#009900", "#00FF66",
"#CC9900", "#FFCC00","#FFFF66",
"#003399", "#0066CC", "#0099CC",
"#9900CC", "#CC00FF", "#FF00FF"),
shp = c(15, 16, 17, 15,16,17,15,16,17,15,16,17))


ggplot(DF, aes(x = x, y = y, color = col, shape = shpfac)) +
geom_point() +
scale_color_identity() +
scale_shape_manual(values = DF$shp)

By default, scale_identity() applies the aesthetic mapping in the plot
but does not create a legend guide. To include it, you need to add
guide = "legend" as an argument to the scale_color_identity call, but
the legend text values will be the hex codes. scale_identity()
directly maps the levels of the factor to the values of the
corresponding aesthetic.

Looking at your example again:

<snip>

> ggplot(graph_prox, aes(log_Ha_conv, PROX_MN))+
> geom_point( aes(colour=factor (Scenarios)),size = 4)+
> scale_color_manual(values=c("#006633", "#009900", "#00FF66",
> "#CC9900", "#FFCC00","#FFFF66",
> "#003399", "#0066CC", "#0099CC",
> "#9900CC", "#CC00FF", "#FF00FF"))+
> scale_shape_manual(values = c(15, 16, 17, 15,16,17,15,16,17,15,16,17))

you have PROX_MN defined in the initial aes(), but did not assign an
aesthetic to it. Since there is no explicit mention of what is to be
associated with the shape aesthetic, I'm not surprised that ggplot2
ignored the scale_shape_manual() code and failed to plot distinct
shapes. If your intention was to map PROX_MN to shape and _assuming it
is a factor with three levels_, try something like this:

ggplot(graph_prox, aes(log_Ha_conv, shape = PROX_MN)) +
geom_point( aes(colour=factor (Scenarios)),size = 4) +
scale_color_manual(values=c("#006633", "#009900", "#00FF66",
"#CC9900", "#FFCC00","#FFFF66",
"#003399", "#0066CC", "#0099CC",
"#9900CC", "#CC00FF", "#FF00FF")) +
scale_shape_manual(values = c(15, 16, 17))

If that doesn't work or the toy example above doesn't help, please
provide a reproducible example for the list to diagnose.

Dennis

María Piquer-Rodríguez

unread,
Dec 4, 2013, 6:13:50 PM12/4/13
to Dennis Murphy, ggplot2
Thanks very much Dennis, and apologies for not providing with a reproducible code. I am quite new to ggplot and R but I will try to be more clear.

I am working with a csv table (graph_prox- example attached) and my x=log_Ha_conv and y=PROX_MN. What I would like to plot are the shapes of my column named Scenarios (these are 4 sets of 3, eg. scenario1A, scenario1B, scenario1C, scenario 2A, scenario 2B, scenario 2C, etc.). I would like all A to be a square, all B to be a circle and all C to be a triangle. And all 1 to be red, all 2 to be blue, etc.

I adapted your suggestion(below) and it worked, except that I get a double legend, one for the color and one for the shapes.

graph_prox=read.csv("csv_test.csv", header=T, stringsAsFactors=F, sep=";")
graph_prox$log_Ha_conv=log10(graph_prox$Ha_converted)

ggplot(graph_prox, aes(x=log_Ha_conv, y=PROX_MN, shape=Scenarios))+
  geom_point( aes(colour=factor (Scenarios)),size = 4)+
  scale_color_manual( values=c("#006633", "#009900", "#00FF66",
                              "#CC9900", "#FFCC00","#FFFF66",
                              "#003399", "#0066CC", "#0099CC",
                              "#9900CC", "#CC00FF", "#FF00FF"))+
  scale_shape_manual(values = c(15, 16, 17, 15,16,17,15,16,17,15,16,17))

 I tried using the same name for scale_color_manual and scale_shape_manual, but still got the double legend- see figure attached.
  
ggplot(graph_prox, aes(x=log_Ha_conv, y=PROX_MN, shape=Scenarios))+
  geom_point( aes(colour=factor (Scenarios)),size = 6)+
  scale_color_manual(name = "Scenarios",
                     labels = c("Sc1G_np", "Sc1G_pbig", "Sc1G_psmall", "Sc2GY_np", "Sc2GY_30km_np", "Sc2GY_pbig", "Sc2GY_psmall", "Sc3GYhist_np", "Sc3GYhist_pbig", "Sc3GYhist_psmall", "Sc4GYhistd_np" , "Sc4GYhistd_pbig", "Sc4GYhistd_psmall"),
    values=c("#006633", "#009900", "#00FF66",
                              "#CC9900", "#FFCC00","#FFFF66",
                              "#003399", "#0066CC", "#0099CC",
                              "#9900CC", "#CC00FF", "#FF00FF"))+
scale_shape_manual(name = "Scenarios",
                     labels = c("Sc1G_np","Sc1G_pbig","Sc1G_psmall", 
                                "Sc2GY_np","Sc2GY_pbig", "Sc2GY_psmall",
                                "Sc3GYhist_np", "Sc3GYhist_pbig","Sc3GYhist_psmall","Sc4GYhistd_np","Sc4GYhistd_pbig", "Sc4GYhistd_psmall"),
                     values = c(15, 16, 17, 15,16,17,15,16,17,15,16,17))

I hope I could clarify my problem a bit better. Any suggestion is very welcome. Thanks in advance,
Maria


2013/12/4 Dennis Murphy <djm...@gmail.com>
Rplot_double_leg.png
test_prox.csv
Reply all
Reply to author
Forward
0 new messages