Lines and points does not seem to work…

27 views
Skip to first unread message

Timothée Poisot

unread,
Aug 12, 2010, 6:49:16 AM8/12/10
to ggplot2
Hi all,

I am working on a dataframe with 3 categories (factors) and one variable (numeric)

I use the following code

pl <- ggplot(result,aes(x=P,y=ddo))
pl2 <- pl + geom_hline(yintercept=0) + geom_point(aes(colour=J)) + geom_line(aes(colour=J))
pl2 <- pl2 + facet_wrap(~B) + coord_flip()
pl2 <- pl2 + xlab('Sampling site') + ylab('Phage community performance') + labs(shape='Sampling day')

and get the following output

 

Instead of what I (naively?) expected : the points within each level of "J" should be joined by lines. What did I do wrong?

Cheers,
t



----------------------------------------------------------
Timothée POISOT  Ph.D. Student
--
Institut des Sciences de l'Evolution, Université Montpellier 2, CC 065
Place Eugène Bataillon                            34095 Montpellier CEDEX 5
--
Phone / Fax        
(+33)4 67 14 40 61
E-mail           
tpo...@um2.fr
Web       http://www.timotheepoisot.fr/
----------------------------------------------------------

ONKELINX, Thierry

unread,
Aug 12, 2010, 6:55:21 AM8/12/10
to Timothée Poisot, ggplot2
Dear Timothée,
 
Is P a categorical variable? In that case geom_line() will not display lines from one category to the next. Probabiliy because lines incase a slope and a slope between categorical levels is non sense. And Hadley tries to prevent that you make non-sense plots with ggplot2.
 
Best regards,
 
Thierry
 

----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry....@inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
 

 


Van: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] Namens Timothée Poisot
Verzonden: donderdag 12 augustus 2010 12:49
Aan: ggplot2
Onderwerp: Lines and points does not seem to work…

--
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
Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.
lines.png

Brandon Hurr

unread,
Aug 12, 2010, 6:58:35 AM8/12/10
to Timothée Poisot, ggplot2
Timothée, 

Could you give or make up some data for us to play with? Makes it a heck of a lot easier to break/fix.

Brandon

Timothée Poisot

unread,
Aug 12, 2010, 7:06:53 AM8/12/10
to ONKELINX, Thierry, ggplot2
Dear Thierry,

Yes, P is categorical. I do not agree with the fact that lines joining categorical data are a non-sense, they are actually quite commonly used in ecology/evolution. What if you consider you timepoints to be categorical data, for example?

Are we sure that ggplot2 strictly forbid us to do it?

I added (faked) data to this email. If anyone has some time to play around, that would be greatly appreciated.

t

data.txt

Brandon Hurr

unread,
Aug 12, 2010, 7:29:29 AM8/12/10
to Timothée Poisot, ggplot2
The photo you attached must have been before the coord_flip() cause the output didn't look like that. 

result<-structure(list(J = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("J1", "J2"), class = "factor"), 
    P = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
    4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
    3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
    2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
    1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
    4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("1A", "1B", 
    "2A", "2B"), class = "factor"), B = structure(c(5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 
    7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 
    9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L), .Label = c("BamA", "CT5H", "CT8G", "StPI", 
    "Y1", "Y2", "Y3", "Y4", "Y5", "Y6"), class = "factor"), ddo = c(-0.32908611354443, 
    1.67897184449119, 0.301489756006467, 0.617127895252235, 1.83355936837839, 
    -0.0571023827476356, 0.441925602124204, -0.990785055278654, 
    1.11749772216934, -0.121787749970832, 0.00463588275079704, 
    -0.766613527078612, 0.171053434184869, -1.46474051816219, 
    -0.568105306397484, -1.52060653143359, 1.76111779712254, 
    -0.660190666801444, -0.348120223533889, 2.62742319978467, 
    0.275309405567307, 0.701814068263965, 0.676217414492788, 
    0.676307729676661, -1.29084119270465, -1.52204489629346, 
    0.797595544704739, -0.66163020297967, 0.17273815487803, -1.1884401480707, 
    -0.262508574205117, 0.99862069659879, 0.0586491240101911, 
    -0.143645494998287, 1.05316758557294, -0.348245370595593, 
    1.658883934416, -1.69898202091905, -0.875591010667734, 0.592269738018775, 
    0.85599001087751, -1.14897950323229, -0.340033264763403, 
    0.364211710603929, 0.059857775115429, 0.683108863384528, 
    -0.114166110811953, -1.0239512198794, 0.725261394591369, 
    -1.21791661879732, -0.0922848547800494, -0.972003622575467, 
    -1.00217690120609, -0.879471871001534, 0.513945275968933, 
    1.42871383401518, 0.190501681030157, 1.22684555442684, -1.24108856577299, 
    1.66776984457856, -0.724804457860477, 0.976713741419654, 
    0.700929568383804, 0.804751875462571, 0.993036943884618, 
    0.52454606551169, -0.192256832677795, -0.327824792016237, 
    -1.09709555444786, 0.21005090674564, -1.10090822154129, -1.43249997839996, 
    -1.40322617881021, 1.26041888550329, -0.953742434103193, 
    0.796892050782326, -0.324118330705491, -1.34801990961945, 
    1.24920608459618, -0.263931095653073)), .Names = c("J", "P", 
"B", "ddo"), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", 
"27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", 
"38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", 
"49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", 
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", 
"71", "72", "73", "74", "75", "76", "77", "78", "79", "80"))

pl <- ggplot(result,aes(x=P,y=ddo, group=J))
pl2 <- pl + geom_hline(yintercept=0) + geom_point(aes(colour=J)) + geom_line(aes(colour=J))
pl3 <- pl2 + facet_wrap(~B) 
pl4 <- pl3 + coord_flip() + xlab('Sampling site') + ylab('Phage community performance') + labs(shape='Sampling day')

pl3
ggsave("pl3.png")
pl4
ggsave("pl4.png")


Seems to work if you tell geom_line() what to group by (added "group=J" in ggplot(aes()). 

Brandon
pl3.png
pl4.png

Brandon Hurr

unread,
Aug 12, 2010, 7:32:34 AM8/12/10
to Timothée Poisot, ggplot2
Just don't ask me to explain why... cause I have no clue. 

Brandon

On Thu, Aug 12, 2010 at 12:31, Timothée Poisot <tpo...@um2.fr> wrote:
Great, thanks!

t

--
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
<pl3.png><pl4.png>

Timothée Poisot

unread,
Aug 12, 2010, 7:31:52 AM8/12/10
to Brandon Hurr, ggplot2
Great, thanks!

t

Le 12 août 2010 à 13:29, Brandon Hurr a écrit :

--
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
<pl3.png><pl4.png>

Hadley Wickham

unread,
Aug 12, 2010, 2:28:58 PM8/12/10
to Timothée Poisot, ONKELINX, Thierry, ggplot2
By default ggplot2 groups observations into geoms by the combination of all categorical variables in the plot.  This doesn't work for lines when you have categorical variables on the x axis, so you need to manually specify the group aesthetic.  Try group = 1.

Hadley

2010/8/12 Timothée Poisot <tpo...@um2.fr>
 <lines.png>



--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Jonathan Christensen

unread,
Aug 14, 2010, 9:02:06 PM8/14/10
to Brandon Hurr, Timothée Poisot, ggplot2
Brandon and Timothy,

Unless otherwise specified, ggplot2 groups by all of the included categorical variables. In this case, it's grouping by J and P (and faceting by B), so each group ends up being only one point, and no lines are drawn. Specifying that it should group only by J solves the problem and gives the intended plot; for comparison, telling it to group only by P would give you vertical lines at each level.

Jonathan

Brandon Hurr

unread,
Aug 15, 2010, 4:33:35 AM8/15/10
to Jonathan Christensen, Timothée Poisot, ggplot2
Jonathon, 

Thanks, that makes a lot of sense. 

Brandon
Reply all
Reply to author
Forward
0 new messages