Search code examples
rplotggplot2line-plot

Plotting median alongside multiple lines in a line plot in R


I have some data (temperatre per different battery levels) for users of a mobile app. I would like to plot the data for each user (all in a single line plot) as well as the median of temp for similar percentages for all users (in the same graph, highlighting it using a thicker line). I can plot all lines except the median using ggplot2. Here is my dummy data file (I can change the data organization/structure or group my data if I need to):

userId, percentage, temp
11, 2, 32
11, 3, 32
11, 4, 33
11, 5, 33
11, 7, 34
11, 10, 30
12, 2, 30
12, 3, 30
12, 4, 30
12, 5, 30
12, 7, 34
12, 10, 32

Here is how I do it at the moment:

library(ggplot2)
sampleDataFrame <- read.table(file.choose(), sep=",", header=T)
sampleDataFrame$userId <- factor(sampleDataFrame$userId)
p1 <- ggplot(sampleDataFrame, aes(x=percentage, y=temp, colour=userId)) + geom_line()
print(p1)

Here is the result:

line plot


Solution

  • You could try

    # compute means per percentage-group:
    sampleDataFrame$means <- with(sampleDataFrame, ave(temp, percentage, FUN=mean)) 
    # plot
    ggplot(sampleDataFrame, aes(x=percentage, y=temp, colour=userId)) + 
      geom_line() + 
      geom_line(aes(y=means), size=2, color="black")
    

    enter image description here