Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geom_line : How to connect only a few points

Tags:

r

ggplot2

I have this dataframe and this plot :

df <- data.frame(Groupe = rep(c("A","B"),4),
                 Period = gl(4,2,8,c("t0","t1","t2","t3","t4")),
                 rate = c(0.83,0.96,0.75,0.93,0.67,0.82,0.65,0.73))

ggplot(data = df, mapping = aes(y = rate, x = Period ,group = Groupe, colour=Groupe, shape=Groupe)) +
   geom_line(size=1.2) +
   geom_point(size=5) 

Plot

How could i organize my data so that the points between t1 and t2 are not connected with a line ? I'd like t0 and t1 to be connected (blue or red according to the group), t2 and t3 connected in the same way, but no lines between t1 and t2. I tried several things by looking at similar questions, but it always mess up my grouping colors :/

like image 718
BloodyNoob Avatar asked Feb 03 '16 15:02

BloodyNoob


People also ask

How do I connect dots in ggplot2?

Connecting Paired Points with lines using geom_line() In ggplot2 we can add lines connecting two data points using geom_line() function and specifying which data points to connect inside aes() using group argument. Now we get a scatter plot connecting paired data with lines.

What does geom_ line() do?

geom_line() connects them in order of the variable on the x axis. geom_step() creates a stairstep plot, highlighting exactly when changes occur. The group aesthetic determines which cases are connected together.

What is geom_ point in r?

The point geom is used to create scatterplots. The scatterplot is most useful for displaying the relationship between two continuous variables.

How do you make a dashed line in ggplot2?

Note that, line types can be also specified using numbers : 0, 1, 2, 3, 4, 5, 6. 0 is for “blank”, 1 is for “solid”, 2 is for “dashed”, ….


2 Answers

Creating a new grouping variable manually is mostly not the best way. So, a slightly different approach which requires less hardcoding:

# create new grouping variable
df$grp <- c(1,2)[df$Period %in% c("t2","t3","t4") + 1L]

# create the plot and use the interaction between 'Group' and 'grp' as group
ggplot(df, aes(x = Period, y = rate,
               group = interaction(Groupe,grp),
               colour = Groupe,
               shape = Groupe)) +
  geom_line(size=1.2) +
  geom_point(size=5)

this gives the same plot as in the other answer:

enter image description here

like image 72
Jaap Avatar answered Oct 06 '22 01:10

Jaap


The best way to handle a problem like this in ggplot is often to create an additional column in your data frame that indicates the grouping you want to work with in your data. For example, here I've added an extra column gp to your data frame:

df$gp <- c(1,2,1,2,3,4,3,4)
ggplot(data = df, aes(y = rate, x = Period, group = gp, colour=Groupe, shape=Groupe)) +
  geom_line(size=1.2) +
  geom_point(size=5)

The result is, I believe, what you are looking for:

enter image description here

If you make Period a numerical column rather than a character vector or factor, you can more easily generate a column like gp automatically rather than manually specifying it (perhaps using ifelse or cases to create it) - this would be useful if you wanted to do the same thing many times or with a large data frame.

like image 33
Empiromancer Avatar answered Oct 06 '22 02:10

Empiromancer