[英]ggplot: Adding line outline to scatterplot
I have thousands of datapoints across 10 minute time stamps over several days.几天内,我在 10 分钟的时间戳中拥有数千个数据点。 Drawing a scatterplot creates a repeating pattern, which I would like to emphasize.
绘制散点图会创建一个重复的模式,我想强调一下。
In the image above, I would like to have a line drawn, outlining the shape of this scatter.
在上图中,我想画一条线,勾勒出这个散点的形状。 More specifically, a line going through the maximum value of
value
for each hour
tick.更具体地,线经历的最大值
value
对于每个hour
刻度。 I've tried adding the likes of freqpoly
and hist
but those do not fit this plot type (where x
is the timestamps).我已经尝试添加
freqpoly
和hist
之类的freqpoly
,但那些不适合这种绘图类型(其中x
是时间戳)。 I've also tried calculating the maximum per timestamp, but I can't use this in the same pot because the original data is in tall format (with multiple entries for every time stamp).我也试过计算每个时间戳的最大值,但我不能在同一个锅中使用它,因为原始数据是高格式的(每个时间戳都有多个条目)。
Example data:示例数据:
set.seed(999)
df <- data.frame('hour' = rep(seq(ISOdatetime(2019,12,1,0,0,0), by = '10 mins', length.out = 6), 3),
'value' = rnorm(18),
'category' = rep(c('a', 'b', 'c'), 6))
ggplot(df, aes(x = hour, y = value)) +
geom_point(aes(color = category), cex = 7) +
theme_minimal()
And this is what I'd like the final product to look like ( black line added by hand ):这就是我希望最终产品的样子(手工添加的黑线):
You can do this by moving the aesthetics for the geom_point
into the actual argument.您可以通过将
geom_point
的美学移动到实际参数中来做到这一点。 Then you can add a stat_summary
to add the line as follows:然后你可以添加一个
stat_summary
来添加如下行:
set.seed(999)
library(ggplot2)
df <- data.frame('hour' = rep(seq(ISOdatetime(2019,12,1,0,0,0), by = '10 mins', length.out = 6), 3),
'value' = rnorm(18),
'category' = rep(c('a', 'b', 'c'), 6))
# Valid for ggplot2 version 3.2.1.9000
# fun.y might be needed if running an earlier version
ggplot(df) +
geom_point(aes(x = hour, y = value, color = category), cex = 7) +
theme_minimal()+
stat_summary(geom = "line", fun = max, aes(hour, value))
# Or you can simplify a little and just keep the color aesthetic in the geom_point
# Same result achieved
ggplot(df, aes(x = hour, y = value),) +
geom_point( aes(color = category), cex = 7) +
stat_summary(geom = "line", fun = max)+
theme_minimal()
This allows you to add the lines and then add the summary statistics as a new "line" geometry.这允许您添加线,然后将汇总统计添加为新的“线”几何。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.