Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect points of different groups by a line using ggplot

Tags:

r

ggplot2

df<-data.frame(adjuster=c("Mary","Mary","Bob","Bob"), date=as.Date(c("2012-1-1","2012-2-1","2012-3-1","2012-4-1")), value=c(10,15,25,15))
df
  adjuster       date value
1     Mary 2012-01-01    10
2     Mary 2012-02-01    15
3      Bob 2012-03-01    25
4      Bob 2012-04-01    15

ggplot(df,aes(x=date,y=value,color=adjuster))+geom_line()+geom_point()

enter image description here

In the above graph, notice the disconnect between the February and March points. How do I connect those points with a blue line, leaving the actual March point red? In other words, Bob should be associated with the value from [Jan - Mar) and Mary from [Mar-Apr].

EDIT: Turns out my example was overly simple. The answers listed don't generalize to the case where the adjuster changes between two people on more than one occasion. For example, consider

df<-data.frame(adjuster=c("Mary","Mary","Bob","Bob","Mary"), date=as.Date(c("2012-1-1","2012-2-1","2012-3-1","2012-4-1","2012-5-1")), value=c(10,15,25,15,20))
      adjuster       date value
1     Mary 2012-01-01    10
2     Mary 2012-02-01    15
3      Bob 2012-03-01    25
4      Bob 2012-04-01    15
5     Mary 2012-05-01    20

Since I didn't mention this in my original question, I'll pick an answer that simply worked for my original data.

like image 754
Ben Avatar asked Nov 01 '13 17:11

Ben


People also ask

How do I connect scatter points in R?

A connected scatter plot represents the relationship between two variables, generally through the time. You can create this type of chart in base R with the plot function, setting type = "b" . The symbol used by default when type = "b" can be modified making use of the pch argument.

What does %>% do in Ggplot?

%>% is a pipe operator reexported from the magrittr package. Start by reading the vignette. Adding things to a ggplot changes the object that gets created. The print method of ggplot draws an appropriate plot depending upon the contents of the variable.

What is Geom_line in Ggplot?

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 does geom_point () do in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot.


1 Answers

Updated to minimise tinkering with data.frame, added the group = 1 argument

Tinkered around with your data.frame a little. You should be able to automate the tinkering around, I guess. Let me know if you aren't. Also, your ggplot command wasn't working as per the chart you've posted in the question

df<-data.frame(
  adjuster=c("Mary","Mary","Bob","Bob"), 
  date=as.Date(c("2012-1-1","2012-2-1","2012-3-1","2012-4-1")), 
  value=c(10,15,25,15)
)

library(data.table)
library(ggplot2)
dt <- data.table(df)
dt[,adjuster := as.character(adjuster)]
dt[,prevadjuster := c(NA,head(adjuster,-1))]
dt[is.na(prevadjuster),prevadjuster := adjuster]


ggplot(dt) +
geom_line(aes(x=date,y=value, color = prevadjuster, group = 1)) +
geom_line(aes(x=date,y=value, color = adjuster, group = 1)) +
geom_point(aes(x=date,y=value, color = adjuster, group = 1))
like image 60
TheComeOnMan Avatar answered Sep 30 '22 15:09

TheComeOnMan