Controlling fill and shape with scale

36 views
Skip to first unread message

FishGradGuy

unread,
Sep 1, 2011, 2:07:37 AM9/1/11
to ggplot2
I am making a plot of some data which is intended to show correctly
classified and incorrectly classified data. I want to control both
shape and fill color. Each classification has it's own shape, that
works fine. I can't figure out how to make the fill work. I believe I
should be using scale_fill_manual() but I haven't been able to make it
work. The example below uses scale_fill_hue() instead. It works that
way, but I can't seem to control fill individually.

here is the data:
SrRatio TrueClass ShapeClass
0.7120146 CWS CWSNo
0.7104905 LSK LSKNo
0.7098242 LSK LSKNo
0.71003 LSK LSKNo
0.7092242 LSK LSKNo
0.7136741 CWS CWSNo
0.7127432 CWS CWSNo
0.7097907 LSK LSKNo
0.7125423 CWS CWSNo
0.7086233 USK USKNo
0.7128441 CWS CWSNo
0.7146446 CWS CWSNo
0.7097369 LSK LSKNo
0.7088942 USK USKNo
0.7098599 LSK LSKNo
0.7140441 CWS CWSNo
0.7091814 LSK LSKNo
0.7096137 LSK LSKNo
0.7095837 LSK LSKNo
0.7103869 LSK LSKNo
0.7104134 LSK LSKNo
0.7128489 CWS CWSNo
0.7101058 LSK LSKNo
0.7119235 CWS CWSNo
0.7125469 CWS CWSNo
0.711137 LSK LSKNo
0.7140785 CWS CWSNo
0.7089743 LSK LSKNo
0.7102236 LSK LSKNo
0.7117135 CWS CWSNo
0.7066045 CRB CRBNo
0.7097781 LSK LSKNo
0.7114166 LSK LSKNo
0.7091486 LSK LSKNo
0.7122264 CWS CWSNo
0.7084838 USK USKNo
0.7090613 LSK LSKNo
0.7093043 LSK LSKNo
0.7115062 CWS CWSNo
0.7091675 LSK LSKNo
0.7115262 CWS CWSNo
0.712287 CWS CWSNo
0.7129377 CWS CWSNo
0.7129312 CWS CWSNo
0.7089072 USK USKNo
0.7089013 USK USKNo
0.709445 LSK LSKNo
0.7092016 LSK LSKNo
0.7090675 LSK LSKNo
0.7096242 LSK LSKNo
0.7059117 CRB CRBNo
0.7114976 CWS CWSNo
0.711857 CWS CWSNo
0.7122164 CWS CWSNo
0.7108419 LSK LSKNo
0.7092669 LSK LSKNo
0.7114646 LSK LSKNo
0.7101425 LSK LSKNo
0.7090543 LSK LSKNo
0.7094518 LSK LSKNo
0.7116835 CWS CWSNo
0.7086728 USK USKNo
0.7117041 CWS CWSNo
0.7136711 CWS CWSNo
0.7086875 USK USKNo
0.712007 CWS CWSNo
0.7118205 CWS CWSNo
0.7112304 LSK LSKNo
0.70923 LSK LSKNo
0.7126306 CWS CWSNo
0.70912 LSK LSKNo
0.7092698 LSK LSKNo
0.7132716 CWS CWSNo
0.7103627 LSK LSKNo
0.7141646 CWS CWSNo
0.709706 LSK LSKNo
0.712327 CWS CWSNo
0.7120865 CWS CWSNo
0.7101506 LSK LSKNo
0.7107622 LSK LSKNo
0.7085225 USK USKNo
0.708754 USK USKNo
0.7125663 CWS CWSNo
0.7095433 LSK LSKYes
0.7087801 USK USKYes
0.7094269 LSK LSKYes
0.708637 USK USKYes
0.7088433 USK USKYes
0.7094793 LSK LSKYes
0.7135932 CWS CWSYes
0.7091245 LSK LSKYesHatch
0.7092115 LSK LSKYesHatch
0.7095772 LSK LSKYesHatch
0.7095816 LSK LSKYesHatch
0.7095073 LSK LSKYesHatch
0.7094535 LSK LSKYesHatch
0.7094691 LSK LSKYesHatch
0.7094937 LSK LSKNo
0.7120234 CWS CWSNo
0.712514 CWS CWSNo
0.7090075 LSK LSKNo
0.711228 LSK LSKNo
0.7087338 USK USKNo
0.7086543 USK USKNo
0.7096089 LSK LSKNo
0.7090961 LSK LSKNo
0.712004 CWS CWSNo
0.7089092 USK USKNo
0.7119388 CWS CWSNo
0.7111812 LSK LSKNo
0.7081944 USK USKNo
0.7121921 CWS CWSNo
0.711657 CWS CWSNo
0.7145368 CWS CWSNo
0.70896 LSK LSKNo
0.7122134 CWS CWSNo
0.708887 USK USKNo
0.7126904 CWS CWSNo
0.7087955 USK USKNo
0.7106416 LSK LSKNo
0.7124934 CWS CWSNo
0.709075 LSK LSKNo
0.7112405 LSK LSKNo
0.710019 LSK LSKNo
0.7099237 LSK LSKNo
0.7120287 CWS CWSNo
0.7085654 USK USKNo
0.7091269 LSK LSKNo
0.7110535 LSK LSKNo
0.7102802 LSK LSKNo
0.7104871 LSK LSKNo
0.7113304 LSK LSKNo
0.7116265 CWS CWSNo
0.7113649 LSK LSKNo

Here is my code:

> Natal_Plot=qplot(TrueClass, SrRatio, data=NatalPlot, xlab="Natal Source Grouping", ylab= expression(bold(phantom(0)^{87}*"Sr /"*phantom(0)^{86}*Sr)), size=4, shape=ShapeClass, geom="jitter", fill=ShapeClass)

> Natal_Plot + theme_bw() + scale_shape_manual(value = c("CRBNo"=23, "CWSNo"=24, "LSKNo"=22, "USKNo"=21, "CWSYes"=24, "LSKYes"=22, "USKYes"=21, "LSKYesHatch"=22), breaks = c("CRBNo", "CWSNo", "LSKNo", "USKNo", "LSKYesHatch"), name = "Classification", labels = c("\nTucannon\nGrande Ronde\nImnaha\n", "\nClearwater\nSalmon\n", "\nLower\nSnake\n", "\nUpper\nSnake\n", "\nHatchery\nYearling\n")) +
scale_fill_hue(breaks = c("CRBNo", "CWSNo", "LSKNo", "USKNo",
"LSKYesHatch"), name = "Classification", labels = c("\nTucannon
\nGrande Ronde\nImnaha\n", "\nClearwater\nSalmon\n", "\nLower\nSnake
\n", "\nUpper\nSnake\n", "\nHatchery\nYearling\n")) +
scale_x_discrete(breaks=c("CRB", "CWS", "LSK", "USK"),
labels=c('\nTucannon\nGrande Ronde\nImnaha', '\nClearwater\nSalmon',
'\nLower\nSnake', '\nUpper\nSnake')) +
opts(panel.background=theme_rect(size=2) ,
axis.text.y=theme_text(size=14, face="bold", hjust=.9),
axis.text.x=theme_text(hjust=.5, vjust=.75, size=12, face="bold"),
panel.background=theme_rect(size=1.5), plot.title=theme_text(hjust=.8,
vjust = .8, face="bold", size=20),
axis.title.y=theme_text(face="bold", size=16, angle=90, hjust=.3),
axis.title.x=theme_text(face="bold", size=16, vjust=-0.5),
axis.ticks=theme_segment(colour = "black", size=1),
panel.grid.minor=theme_blank() )


I would like to add these colors:
cols=c("CRBNo"="998072", "CWSNo"="998072", "LSKNo"="998072",
"USKNo"="998072", "CWSYes"="000000", "LSKYes"="000000",
"USKYes"="000000", "LSKYesHatch"="000000")

It seems like it should be easy, but it's been a long day in front of
the screen.

Any help would be much appreciated.

Brandon Hurr

unread,
Sep 1, 2011, 1:03:54 PM9/1/11
to FishGradGuy, ggplot2
Jensen, 

I'm not sure what the numbers of your colors mean, but if you change "Red" and "Black" to what you want then it should work. 

Natal_Plot +
theme_bw() +
scale_shape_manual(

value = c("CRBNo"=23, "CWSNo"=24, "LSKNo"=22, "USKNo"=21, "CWSYes"=24,"LSKYes"=22, "USKYes"=21, "LSKYesHatch"=22)
, breaks = c("CRBNo","CWSNo","LSKNo","USKNo","LSKYesHatch")
, name = "Classification"
, labels = c("\nTucannon\nGrande Ronde\nImnaha\n", "\nClearwater\nSalmon\n", "\nLower\nSnake\n", "\nUpper\nSnake\n", "\nHatchery\nYearling\n")
)+
scale_fill_manual(

breaks = c("CRBNo", "CWSNo", "LSKNo", "USKNo", "LSKYesHatch")
, name = "Classification"
, labels = c("\nTucannon\nGrande Ronde\nImnaha\n", "\nClearwater\nSalmon\n", "\nLower\nSnake\n", "\nUpper\nSnake\n", "\nHatchery\nYearling\n")
, values=c("CRBNo"="Red", "CWSNo"="Red", "LSKNo"="Red", "USKNo"="Red", "CWSYes"="Black", "LSKYes"="Black","USKYes"="Black", "LSKYesHatch"="Black")
)+

scale_x_discrete(breaks=c("CRB", "CWS", "LSK", "USK"),
labels=c('\nTucannon\nGrande Ronde\nImnaha', '\nClearwater\nSalmon',
'\nLower\nSnake', '\nUpper\nSnake')) +
opts(panel.background=theme_rect(size=2) ,
axis.text.y=theme_text(size=14, face="bold", hjust=.9),
axis.text.x=theme_text(hjust=.5, vjust=.75, size=12, face="bold"),
panel.background=theme_rect(size=1.5), plot.title=theme_text(hjust=.8,
vjust = .8, face="bold", size=20),
axis.title.y=theme_text(face="bold", size=16, angle=90, hjust=.3),
axis.title.x=theme_text(face="bold", size=16, vjust=-0.5),
axis.ticks=theme_segment(colour = "black", size=1),
panel.grid.minor=theme_blank() )


Hopefully, you have't already solved this. 


B



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

FishGradGuy

unread,
Sep 1, 2011, 3:31:19 PM9/1/11
to ggplot2
Brandon,

Thanks for your help. I actually woke up this morning and realized
that my problem was very simple. I didn't have the "#" in front of my
hex color codes. It worked after that. I have two more questions.

#1 - why is the extra legend appearing with the point size rather than
the point size being reflected in the legend?
#2 - how can I make the "LSKYesHatch" plot underneath the "LSKYes"
data so that LSKYes isn't overplotted?

Here is a reproducible example of the final working plot. It is a plot
of classifications from a single variable LDFA of fish. I wanted to
denote each classification with its own color AND symbol as well as
make clear which points have a known classification. Known origin
points have a higher luminance of the same color. There is also a red
group that I wanted to highlight because they are from a hatchery.

In this case all the classifications are correct, in other plots the
TrueClass column would plot the points in the column according to
their classification, but the ShapeClass column would plot it with
colors based on it's true classification. Misclassified points would
be easily visible in the wrong column since their shape and color
would be different. I also added a PlotOrder column to so columns go
from high y-values to low y-values. I'm sure there is a simpler way to
do this, this has been more about trial and error than anything.

Here is a reduced dataset:
SrRatio TrueClass ShapeClass PlotOrder
0.7066045 CRB CRBNo 4
0.7059117 CRB CRBNo 4
0.7115262 CWS CWSNo 1
0.712287 CWS CWSNo 1
0.7120865 CWS CWSNo 1
0.7125663 CWS CWSNo 1
0.7135932 CWS CWSYes 1
0.7120234 CWS CWSNo 1
0.712514 CWS CWSNo 1
0.712004 CWS CWSNo 1
0.7119388 CWS CWSNo 1
0.7121921 CWS CWSNo 1
0.711657 CWS CWSNo 1
0.7145368 CWS CWSNo 1
0.7122134 CWS CWSNo 1
0.7126904 CWS CWSNo 1
0.7124934 CWS CWSNo 1
0.7120287 CWS CWSNo 1
0.7116265 CWS CWSNo 1
0.7104905 LSK LSKNo 2
0.7098242 LSK LSKNo 2
0.71003 LSK LSKNo 2
0.7103627 LSK LSKNo 2
0.709706 LSK LSKNo 2
0.7101506 LSK LSKNo 2
0.7107622 LSK LSKNo 2
0.7095433 LSK LSKYes 2
0.7094269 LSK LSKYes 2
0.7094793 LSK LSKYes 2
0.7091245 LSK LSKYesHatch 2
0.7092115 LSK LSKYesHatch 2
0.7095772 LSK LSKYesHatch 2
0.7095816 LSK LSKYesHatch 2
0.7095073 LSK LSKYesHatch 2
0.7094535 LSK LSKYesHatch 2
0.7094691 LSK LSKYesHatch 2
0.7113304 LSK LSKNo 2
0.7113649 LSK LSKNo 2
0.7086233 USK USKNo 3
0.7088942 USK USKNo 3
0.7084838 USK USKNo 3
0.7089072 USK USKNo 3
0.7089013 USK USKNo 3
0.7086728 USK USKNo 3
0.7086875 USK USKNo 3
0.7085225 USK USKNo 3
0.708754 USK USKNo 3
0.7087801 USK USKYes 3
0.708637 USK USKYes 3
0.7088433 USK USKYes 3
0.7087338 USK USKNo 3
0.7086543 USK USKNo 3
0.7089092 USK USKNo 3
0.7081944 USK USKNo 3
0.708887 USK USKNo 3
0.7087955 USK USKNo 3
0.7085654 USK USKNo 3

Here is the new code:
Natal_Plot=qplot(reorder(TrueClass, PlotOrder), SrRatio,
data=NatalPlot, xlab="Natal Source Grouping", ylab=
expression(bold(phantom(0)^{87}*"Sr /"*phantom(0)^{86}*Sr)), size=4.5,
shape=ShapeClass, geom="jitter", fill=ShapeClass)

Natal_Plot + theme_bw() +

scale_shape_manual(value = c("CWSNo"=24, "LSKNo"=22, "USKNo"=21,
"CWSYes"=24, "LSKYes"=22, "USKYes"=21, "CRBNo"=23, "LSKYesHatch"=22),
breaks = c("CWSNo", "LSKNo", "USKNo", "CRBNo", "LSKYesHatch"), name =
"Classification", labels = c("\nClearwater,\nSalmon\n", "\nLower-
\nSnake\n", "\nUpper-\nSnake\n", "\nTucannon,\nGrande Ronde,\nImnaha
\n", "\nKnown\nOrigin\nJuvenile\n")) +

scale_fill_manual(value = c("CWSNo"="#3C6D00", "LSKNo"="#007F85",
"USKNo"="#8D26C8", "CWSYes"="#BFF439", "LSKYesHatch"="#D40000",
"LSKYes"="#00FFFF", "USKYes"="#FFC5FF", "CRBNo"="#AE3121"), breaks =
c("CWSNo", "LSKNo", "USKNo", "CRBNo", "LSKYesHatch"), name =
"Classification", labels = c("\nClearwater,\nSalmon\n", "\nLower-
\nSnake\n", "\nUpper-\nSnake\n", "\nTucannon,\nGrande Ronde,\nImnaha
\n", "\nKnown\nOrigin\nJuvenile\n")) +

scale_x_discrete(breaks=c("CWS", "LSK", "USK", "CRB"),
labels=c('\nClearwater\nSalmon', '\nLower\nSnake', '\nUpper\nSnake',
'\nTucannon\nGrande Ronde\nImnaha')) +

opts(panel.background=theme_rect(size=1) ,
axis.text.y=theme_text(size=14, face="bold", hjust=.9),
axis.text.x=theme_text(hjust=.5, vjust=.75, size=12, face="bold"),

Brandon Hurr

unread,
Sep 1, 2011, 3:46:40 PM9/1/11
to FishGradGuy, ggplot2
Jensen, 

When you email your data make it easier on us an dput() your data. 

NatalPlot<-structure(list(SrRatio = c(0.7066045, 0.7059117, 0.7115262, 0.712287, 
0.7120865, 0.7125663, 0.7135932, 0.7120234, 0.712514, 0.712004, 
0.7119388, 0.7121921, 0.711657, 0.7145368, 0.7122134, 0.7126904, 
0.7124934, 0.7120287, 0.7116265, 0.7104905, 0.7098242, 0.71003, 
0.7103627, 0.709706, 0.7101506, 0.7107622, 0.7095433, 0.7094269, 
0.7094793, 0.7091245, 0.7092115, 0.7095772, 0.7095816, 0.7095073, 
0.7094535, 0.7094691, 0.7113304, 0.7113649, 0.7086233, 0.7088942, 
0.7084838, 0.7089072, 0.7089013, 0.7086728, 0.7086875, 0.7085225, 
0.708754, 0.7087801, 0.708637, 0.7088433, 0.7087338, 0.7086543, 
0.7089092, 0.7081944, 0.708887, 0.7087955, 0.7085654), TrueClass = c("CRB", 
"CRB", "CWS", "CWS", "CWS", "CWS", "CWS", "CWS", "CWS", "CWS", 
"CWS", "CWS", "CWS", "CWS", "CWS", "CWS", "CWS", "CWS", "CWS", 
"LSK", "LSK", "LSK", "LSK", "LSK", "LSK", "LSK", "LSK", "LSK", 
"LSK", "LSK", "LSK", "LSK", "LSK", "LSK", "LSK", "LSK", "LSK", 
"LSK", "USK", "USK", "USK", "USK", "USK", "USK", "USK", "USK", 
"USK", "USK", "USK", "USK", "USK", "USK", "USK", "USK", "USK", 
"USK", "USK"), ShapeClass = c("CRBNo", "CRBNo", "CWSNo", "CWSNo", 
"CWSNo", "CWSNo", "CWSYes", "CWSNo", "CWSNo", "CWSNo", "CWSNo", 
"CWSNo", "CWSNo", "CWSNo", "CWSNo", "CWSNo", "CWSNo", "CWSNo", 
"CWSNo", "LSKNo", "LSKNo", "LSKNo", "LSKNo", "LSKNo", "LSKNo", 
"LSKNo", "LSKYes", "LSKYes", "LSKYes", "LSKYesHatch", "LSKYesHatch", 
"LSKYesHatch", "LSKYesHatch", "LSKYesHatch", "LSKYesHatch", "LSKYesHatch", 
"LSKNo", "LSKNo", "USKNo", "USKNo", "USKNo", "USKNo", "USKNo", 
"USKNo", "USKNo", "USKNo", "USKNo", "USKYes", "USKYes", "USKYes", 
"USKNo", "USKNo", "USKNo", "USKNo", "USKNo", "USKNo", "USKNo"
), PlotOrder = c(4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L)), .Names = c("SrRatio", 
"TrueClass", "ShapeClass", "PlotOrder"), class = "data.frame", row.names = c(NA, 
-57L))

That out of the way, you want to not overplot your Red and Cyan points. You can play with the height and width adjustments for geom_jitter().  Just add width= and/or height= to your jitter call. I couldn't get it completely non-overplotting. I would then perhaps try an alpha of the points. If that's not quite right... maybe a different kind of graph would be more appropriate? I have no idea what would be more appropriate though, I'm not a statistician. 

If you want to integrate legends you need to make sure they all have the same name, breaks and such. You need to add scale_size_manual(). 

Brandon


FishGradGuy

unread,
Sep 1, 2011, 4:43:23 PM9/1/11
to ggplot2
Thanks for the tip. I will do that from now on.

Maybe overplotting was the wrong word. The cyan points are always
plotted before the red, hence, they are always overlayed by them. I
would rather have the cyan points plot last so that they are on top of
the red points.

Brandon Hurr

unread,
Sep 1, 2011, 5:01:14 PM9/1/11
to FishGradGuy, ggplot2
Try reordering the levels of the factor. I think they may be plotted in order by that. If your factor levels aren't ordered then it's probably plotting alphabetically... does that mesh with what's happening?


--

FishGradGuy

unread,
Sep 3, 2011, 2:41:07 AM9/3/11
to ggplot2
Thanks for your help. Got it to work the way I want it.
Reply all
Reply to author
Forward
0 new messages