简体   繁体   中英

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 percentage s 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:

线图

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")

在此输入图像描述

Instead of calculating a new variable, you could also use stat_summary :

ggplot(sampleDataFrame, aes(x=percentage, y=temp, colour=factor(userId))) + 
  geom_line() + 
  stat_summary(fun.y = "median", geom = "line", color = "black", size = 1.2)

which gives:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM