1

我有一些移动应用程序用户的数据(不同电池电量的温度)。我想绘制每个用户的数据(都在一个单线图中)以及所有用户temp的相似percentages 的中位数(在同一个图中,使用较粗的线突出显示它)。我可以使用 ggplot2 绘制除中位数以外的所有线条。这是我的虚拟数据文件(如果需要,我可以更改数据组织/结构或对数据进行分组):

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

这是我目前的做法:

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)

结果如下:

线图

4

2 回答 2

2

你可以试试

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

在此处输入图像描述

于 2016-01-10T11:48:38.157 回答
2

除了计算新变量,您还可以使用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)

这使:

在此处输入图像描述

于 2016-01-10T14:53:54.987 回答