ggpcp() with points colored differently for each variable

583 views
Skip to first unread message

FishGradGuy

unread,
Sep 3, 2011, 2:38:20 PM9/3/11
to ggplot2
I just figured out how to use ggpcp(). It does exactly what I was
looking for, but I'm thinking that there is something in the way
melt() works that I need to understand in order to label the points
the way I want to.

I have this truncated data set as an example:

dput(Parallel_Plot_Data[1:20,], control="keepNA")
list(Sample = c(1, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24), NatalSrRatio = c(0.7120146, 0.7104905,
0.7098242, 0.71003, 0.7092242, 0.7136741, 0.7127432, 0.7097907,
0.7125423, 0.7086233, 0.7128441, 0.7146446, 0.7097369, 0.7088942,
0.7098599, 0.7140441, 0.7091814, 0.7096137, 0.7095837, 0.7103869
), RearSrRatio = c(0.7140476, 0.7102578, 0.7101535, 0.710548,
0.7102841, 0.714541, 0.715124, 0.7099535, 0.7129941, 0.7099142,
0.7099159, 0.7146446, 0.7099941, 0.7097307, 0.7102079, 0.7140441,
0.709784, 0.7096137, 0.7102163, 0.7134733), OWSrRatio =
c(0.710207081,
0.709757723, 0.709349432, NA, 0.710008946, 0.70934427, NA, NA,
0.71011365, NA, 0.710591332, 0.709854152, 0.709440048, 0.710810299,
NA, 0.709876241, 0.709730724, NA, NA, 0.709991373), NatalClass = c(2,
3, 3, 3, 3, 2, 2, 3, 2, 4, 2, 2, 3, 4, 3, 2, 3, 3, 3, 3), RearClass =
c(2,
3, 3, 3, 3, 2, 2, 3, 2, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2), OWClass =
c(2,
2, 2, NA, 2, 2, NA, NA, 2, NA, 2, 2, 2, 2, NA, 2, 2, NA, NA,
2))


I put this into a parallel coordinates plot and colored the lines and
points based on the first variable:

Parallel_Plot=ggpcp(Parallel_Plot_Data[1:20,],
vars=names(Parallel_Plot_Data[2:4]), scale=FALSE) +
geom_line(aes(colour=NatalClass)) + geom_point(aes(colour=NatalClass))


The variables NatalSrRatio, RearSrRatio & OWSrRatio describe the
chemistry of fish at different parts of their lifecycle. The Natal,
Rear, and OW class indicate the location of fish at that point in
their life (natal, rearing, overwintering). The point of the plot is
to show how fish from different places move to new locations
differently as they get older. The lines are just right, I want them
to correspond to where a fish is born, to see if there are differences
in behavior between different groups.

My problem is that I want the geom_point() to draw the points at each
x-axis variable based on the classification for that variable.

For example: For sample 1014 the point for RearSrRatio should be drawn
based on RearClass and the point for OWSrRatio should be drawn based
on OWClass.

Does this require a different kind of melt? More than one? Or is there
a way to use a conditional statement to draw the points depending on
which variable they belong to in the melted dataset?

Thanks for your help in advance.

Dianne Cook

unread,
Sep 3, 2011, 2:40:20 PM9/3/11
to FishGradGuy, ggplot2
You could look at the GGally package that has a par coords function - it builds on top of ggplot2.

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

---------------------------
Di Cook
vis...@gmail.com


FishGradGuy

unread,
Sep 3, 2011, 2:53:29 PM9/3/11
to ggplot2
Is there a way to independently assign the point color based on
variable using ggparcoord()? I am looking but I don't see that it is
particularly easier to do, even though it gives you more latitude in
organizing your variables on the axis.

Jens

Dianne Cook

unread,
Sep 3, 2011, 3:00:00 PM9/3/11
to FishGradGuy, ggplot2
If you look at the last example in the help, and modify it a little:

ggparcoord(data = iris, columns = 1:4, groupColumn = 5, order = "anyClass",
+ title = "Parallel Coordinate Plot for the Iris Data",
+ alphaLines = 0.3) + geom_point(colour=I("purple"))

you can get point colour to be different from line colour.

FishGradGuy

unread,
Sep 3, 2011, 3:06:52 PM9/3/11
to ggplot2
I just loaded GGally and wasn't able to find ggparcoord() at all once
I loaded the library.

> library(GGally)
> ggparcoord(data = msleep, columns = 6:11, groupColumn = "vore", missing = "random", scale = "uniminmax")
Error: could not find function "ggparcoord"
> ?ggparcoord
No documentation for 'ggparcoord' in specified packages and libraries:
you could try '??ggparcoord'
> ??ggparcoord
No help files found matching ‘ggparcoord’ using fuzzy matching
>

Strange.

Dianne Cook

unread,
Sep 3, 2011, 3:12:42 PM9/3/11
to FishGradGuy, ggplot2
Can you check if the version you got is 0.3.0?

FishGradGuy

unread,
Sep 3, 2011, 6:36:03 PM9/3/11
to ggplot2
I'm having a heck of a time getting it loaded. Found the .3 version on
CRAN but it keeps giving an error when it installs.

Warning in rawToChar(magic[1:5]) :
truncating string with embedded nul: 'GGa\0\0'
* installing *binary* package ‘GGally’ ...
* DONE (GGally)

It says it installed it, but the help files don't seem to exist and it
won't run any of the functions. Not sure what the problem is.

FishGradGuy

unread,
Sep 5, 2011, 1:32:37 AM9/5/11
to ggplot2
OK! I finally got GGally loaded and have a plot that works. How do I
color the lines with specific colors from a list?

colors=c("#3C6D00", "#007F85", "#8D26C8", "#AE3121")

ggparcoord(data = Parallel_Plot_Data, columns = 2:4, groupColumn = 5,
order = c(2, 3, 4),
showPoints = TRUE, missing= "exclude", title = "Parallel Coordinate
Plot for the Iris Data",
alphaLines = 0.3)

I have tried several things but I can't seem to make it take my own
colors.

Jens

FishGradGuy

unread,
Sep 5, 2011, 3:02:31 AM9/5/11
to ggplot2
I got that to work as well:

Parallel_Plot = ggparcoord(data = Parallel_Plot_Data, columns = 2:4,
groupColumn = 5, order = c(2, 3, 4), showPoints = TRUE, missing=
"exclude", scale="globalminmax", alphaLines = 0.8)

Parallel_Plot +
theme_bw() +
scale_colour_manual(value = c("CWS"="#3C6D00", "LSK"="#007F85",
"USK"="#8D26C8", "CRB"="#AE3121"), breaks = c("CRB", "CWS", "LSK",
"USK"), name = "Classification", labels = c("\nClearwater,\nSalmon\n",
"\nLower-\nSnake\n", "\nUpper-\nSnake\n", "\nTucannon,\nGrande Ronde,
\nImnaha\n")) + 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"),
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 narrow the space between the y-axis and the first
datapoint, and after the last datapoint. Is there a way to do that?

FishGradGuy

unread,
Sep 5, 2011, 3:06:32 AM9/5/11
to ggplot2
Ok, got the colors to work.

Parallel_Plot = ggparcoord(data = Parallel_Plot_Data, columns = 2:4,
groupColumn = 5, order = c(2, 3, 4), showPoints = TRUE, missing=
"exclude", scale="globalminmax", alphaLines = 0.8,
mapping=aes(fill=NatalClass))

Parallel_Plot + theme_bw() +

scale_colour_manual(value = c("CWS"="#3C6D00", "LSK"="#007F85",
"USK"="#8D26C8", "CRB"="#AE3121"), breaks = c("CRB", "CWS", "LSK",
"USK"), name = "Classification", labels = c("\nClearwater,\nSalmon\n",
"\nLower-\nSnake\n", "\nUpper-\nSnake\n", "\nTucannon,\nGrande Ronde,
\nImnaha\n")) +

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"),
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() )


Is there a way to decrease the distance between the y-axis and the
first datapoint, as well as after the last datapoint? There is alot of
wasted white space there.



On Sep 4, 10:32 pm, FishGradGuy <jensenh...@gmail.com> wrote:

Brandon Hurr

unread,
Sep 5, 2011, 4:37:21 AM9/5/11
to FishGradGuy, ggplot2
Jensen, 

I can't seem to get the plot to work with your small dataset. The plot is empty. 

If you were trying to adjust the axis without removing datapoints and it is a continuous numeric axis you could use coord_cartesian 

+coord_cartesian(ylim=c(0.710, 0.713), wise=T)

You'll have to set things manually. "wise" puts in finer tick marks than just zooming in would give you. Try FALSE to see if it still fits your needs. 

Brandon

FishGradGuy

unread,
Sep 5, 2011, 11:21:08 AM9/5/11
to ggplot2
Hmm...I dunno. Here is another dput(), I might have changed something
but don't remember. Thanks for the tip about coord_cartesian(). It
works well except that the wise=T argument gives an error. The code is
below along with the error.

structure(list(Sample = structure(c(1L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L), .Label = c("1014", "14185", "14207", "14356", "14538",
"3159", "3185", "3186", "3189", "3194", "3197", "3198", "3204",
"3206", "3207", "3234", "3245", "3247", "3252", "3253", "3316",
"3331", "3337", "3346", "3354", "3371", "3372", "3380", "4002",
"4005", "4025", "4047", "4074", "4080", "4101", "4102", "4105",
"4110", "4112", "4115", "4129", "4131", "4146", "4150", "4153",
"4155", "4160", "4172", "4197", "4202", "4211", "4215", "4216",
"4234", "4238", "5036", "5038", "5044", "5068", "5071", "5072",
"5076", "5094", "5097", "5115", "5132", "5133", "5135", "5137",
"5159", "6009", "6026", "6044", "6045", "6048", "6048_1", "6052",
"6063", "6071", "6091", "6113", "6129", "6153", "m0821", "m0822",
"m0865", "M0934", "m0951", "m1015", "m1072", "m1075", "m1077",
"m1093", "m1101", "m1110", "m1120", "m1127", "m1129", "m1144",
"m1176", "m1381", "m2252", "m2265", "m2266", "m2272", "m2279",
"m2579", "m2617", "m2661", "m2675", "m2691", "m2693", "m2710",
"m2716", "m2721", "m2728", "m2731", "m2736", "m2737", "m2742"
), class = "factor"), NatalSrRatio = c(0.7120146, 0.7104905,
0.7098242, 0.71003, 0.7092242, 0.7136741, 0.7127432, 0.7097907,
0.7125423, 0.7086233, 0.7128441, 0.7146446, 0.7097369, 0.7088942,
0.7098599, 0.7140441, 0.7091814, 0.7096137, 0.7095837, 0.7103869
), RearSrRatio = c(0.7140476, 0.7102578, 0.7101535, 0.710548,
0.7102841, 0.714541, 0.715124, 0.7099535, 0.7129941, 0.7099142,
0.7099159, 0.7146446, 0.7099941, 0.7097307, 0.7102079, 0.7140441,
0.709784, 0.7096137, 0.7102163, 0.7134733), OWSrRatio =
c(0.710207081,
0.709757723, 0.709349432, NA, 0.710008946, 0.70934427, NA, NA,
0.71011365, NA, 0.710591332, 0.709854152, 0.709440048, 0.710810299,
NA, 0.709876241, 0.709730724, NA, NA, 0.709991373), NatalClass =
structure(c(2L,
3L, 3L, 3L, 3L, 2L, 2L, 3L, 2L, 4L, 2L, 2L, 3L, 4L, 3L, 2L, 3L,
3L, 3L, 3L), .Label = c("CRB", "CWS", "LSK", "USK"), class =
"factor")), .Names = c("Sample",
"NatalSrRatio", "RearSrRatio", "OWSrRatio", "NatalClass"), row.names =
c(NA,
20L), class = "data.frame")

> Parallel_Plot + theme_bw()

+ scale_colour_manual(value = c("CWS"="#3C6D00", "LSK"="#007F85",
"USK"="#8D26C8", "CRB"="#AE3121"), breaks = c("CWS", "LSK",
"USK","CRB"), name = "Classification", labels = c("\nClearwater,
\nSalmon\n", "\nLower-\nSnake\n", "\nUpper-\nSnake\n", "\nTucannon,
\nGrande Ronde,\nImnaha\n"))
+ coord_cartesian(ylim=c(0.705, 0.716), wise=T) +
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"),
+ 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() )
Error in get("new", env = CoordCartesian, inherits = TRUE)
(CoordCartesian, :
unused argument(s) (wise = TRUE)

FishGradGuy

unread,
Sep 5, 2011, 11:40:26 AM9/5/11
to ggplot2
I just used scale_y_continuous() to set my breaks manually. Not
elegant, but it gets the job done.

Thanks for your help.
> ...
>
> read more »

FishGradGuy

unread,
Sep 12, 2011, 2:34:46 PM9/12/11
to ggplot2
Ok,

So, I got ggparcoord() to work, but it is missing a critical piece.
Some of my data has NA's in the last variable. They are legitimate
missing points (those fish either died or left my study area before
that time point) but ggparcoord() only lets you exclude that data
entirely or pick from different ways to average missing data.

ggpcp() allows you to use data with NA's, but I wasn't able to color
my lines the way I wanted to.

Could someone point me to a way that I can use ggpcp() with custom
line and point colors? It was giving me fits before. It seemed that it
wasn't recognizing scale_manual arguments at all.

Jens
> > > > Parallel_Plot =ggparcoord(data = Parallel_Plot_Data, columns = 2:4,
> > > > > > > I just loaded GGally and wasn't able to findggparcoord() at all once
> > > > > > > I loaded the library.
>
> > > > > > > > library(GGally)
> > > > > > > >ggparcoord(data = msleep, columns = 6:11, groupColumn = "vore",
> > > > missing = "random", scale = "uniminmax")
>
> > > > > > > Error: could not find function "ggparcoord"> ?ggparcoord
>
> > > > > > > No documentation for 'ggparcoord' in specified packages and
> > > > libraries:
> > > > > > > you could try '??ggparcoord'> ??ggparcoord
>
> > > > > > > No help files found matching ‘ggparcoord’ using fuzzy matching
>
> > > > > > > Strange.
>
> > > > > > > On Sep 3, 11:53 am, FishGradGuy <jensenh...@gmail.com> wrote:
>
> > > > > > > > Is there a way to independently assign the point color based on
> > > > > > > > variable usingggparcoord()? I am looking but I don't see that it
> ...
>
> read more »

FishGradGuy

unread,
Sep 12, 2011, 8:20:18 PM9/12/11
to ggplot2
Ok, here is what I have come up with. The only thing that doesn't work
is that I can't seem to use an xlab or ylab argument in the ggpcp()
statement to name the axes. Any thoughts on why that doesn't work?

Parallel_Plot=ggpcp(Parallel_Plot_Data,
vars=names(Parallel_Plot_Data[2:4]), scale=FALSE)

+ geom_line(aes(colour=NatalClass), alpha=3/10, size=.6) +
geom_point(aes(colour=NatalClass), alpha=8/10)

Parallel_Plot

+ theme_bw()

+ scale_colour_manual(value = c("CWS"="#3C6D00", "LSK"="#007F85",
"USK"="#8D26C8", "CRB"="#AE3121"), breaks = c("CWS", "LSK", "USK",
"CRB"), name = "Classification", labels = c("\nClearwater,\nSalmon\n",
"\nLower-\nSnake\n", "\nUpper-\nSnake\n", "\nTucannon,\nGrande Ronde,
\nImnaha\n"))

+ 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"),
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() )



On Sep 12, 11:34 am, FishGradGuy <jensenh...@gmail.com> wrote:
> Ok,
>
> So, I gotggparcoord() to work, but it is missing a critical piece.
> Some of my data has NA's in the last variable. They are legitimate
> missing points (those fish either died or left my study area before
> that time point) butggparcoord() only lets you exclude that data
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages