FAQ: geom_line() doesn’t draw lines


Probably one of the most frequently asked question is that geom_line() does not draw lines.
This is because x variable is factor.

# sample data
d <- data.frame(expand.grid(x=letters[1:4], g=factor(1:2)), y=rnorm(8))

# Figure 1a, 1b, and 1c.
ggplot(d, aes(x=x, y=y, colour=g)) + opts(title="Figure 1a") + geom_line() + geom_point()
ggplot(d, aes(x=x, y=y, colour=g, group=g)) + opts(title="Figure 1b") + geom_line() + geom_point()
ggplot(d, aes(x=x, y=y, colour=g, group=1)) + opts(title="Figure 1c") + geom_line() + geom_point()

The figure 1a does not draw any lines, while the figure 1b works well.
The key concept is “group” in aes.
The rule is simple:

  1. geom_line() tries to connect data points that belong to same group.
  2. different levels of factor variable belong to different group.

So, by specifying group=g in aes, the lines appear in Figure 1b.

Figure 1c would help to understand more.
In Figure 1c, all data points belong to group=1, hence all data points are connected.

15 thoughts on “FAQ: geom_line() doesn’t draw lines

  1. Pingback: R: ggplot2 – Each group consist of only one observation. Do you need to adjust the group aesthetic? at Mark Needham

  2. So, how would I create a legend for plot [1b] with a red for say “group1” and blue for “group2” (also stored in ‘d’)?

    Very helpful anyway.

  3. Oh my freakin lord! I have been struggling with this in a Power BI visualization for a week and kept thinking it was a bug in Power BI 😀 Can’t believe it was THAT simple a fix. Added group = 1 to my geom_line and the darn thing appeared.
    ::Facepalm::

  4. Mirki Idzie Jakos Wyciagnac Kod Zródlowy Z
    Serial Number Adobe Photoshop CS6 – Photoshop adalah aplikasi editor picture terbaik saat ini dan sekarang sudah update terbarunya yaitu CS6 yang telah banyak sekali beredar didunia web dengan web link download complete version gratis, Nah disini saya akan berbagi untuk sobat serial number untuk digunakan mengaktivasinya agar photoshop cs6 anda menjadi full variation tanpa trial lagi. Was hoping this list will certainly provide some ideas regarding what I could play after the Witcher that has incredible story, however the minute I saw Skyrim( delighted in the game btw, but it’s just miles away from the story-telling in Witcher 3) there – I recognized it wouldn’t happen.\n\nSimilar to various other MOBAs the game is best when playing with good friends, and because it is a 3v3 experience you only require 2 various other players for a group, which every true MOBA fan will certainly recognize is an extremely attractive proposal.\n\nAttributes include in-game voice chat to make communication between you and also your allies a lot easier, an AI system to replace players who get or leave disconnected, specific sound sets for heroes to produce a special video gaming experience, a far better and also boosted match-making system to guarantee all games are a challenge, a viewer system to permit players to quickly watch other video games and also an incentives system based upon community engagement and also mentorship.\n\nThe Android-based MOD Live ships with four new, industry first applications: Pal Tracking, Mobile Phone Connectivity, a Navigating application pre-loaded with the largest database of trail maps in the world, Music Playlist Mode, and Video camera Connectivity for usage in the future with choose wireless point-of-view action electronic cameras.\n\nFIFA 19 is perhaps the most effective soccer video game currently ahead of the Pro Advancement Soccer 2019 FIFA 19 was first released on Console, Xbox and also PS4, and also currently the game is offered for you to use your Android as well as iOS gadgets.\n\nA few of the featured top football superstar players in PES 19 Apk Mod consists of Messi, Ronaldo, Mo Salah, Harry Kane, Lewandowski, Eden Risk, Kylian Mbappé, Pogba, Lukaku, Morata, Higuan, Dembele, Sanchez, Suarez, Mane, Williams, Sagio Ramos, John Stones, Ngolo Kanté, Sane, Aguero etc\n\nSo below we proceed and also choose different goals as you keep on opening you will be up advancing with that so there’s gon na be a whole lot a lot a whole lot an aircraft here there’s a great deal of playing their plants go back again one thing to remember see to it you go to your account ensure you sign in with the WB account on that means we have this game in the console you will such as a pair points in the council social reach degree 20 before you kill like the death at thinkers he fatality as soon as you get to degree 20 assistance card will certainly be right here.\n\nAndroid followers: you no more have to watch on the sidelines as your buddies play TELEVISION tennis with their apples iphone Rolocule has launched Activity Tennis Cast, an Android variation of its totally free sporting activities game that makes use of screen mirroring technology (such as Chromecast, Miracast or Samsung’s AllShare Cast) to turn your phone right into a Wii Sports -like movement controller, with your TELEVISION or a Chrome browser tab acting as the screen. mods for iphone games

  5. Just to clarify- the g in group= g code represents the variable in which the lines should be connected. For my project, I used group = ID as I wanted to see the rate of change of variable “Y” over a series of time points (variable “X”) for each ID.

Leave a comment