ggplot:向散点图添加线条轮廓
ggplot: Adding line outline to scatterplot
我在几天内有数千个跨越 10 分钟时间戳的数据点。
绘制散点图会产生重复模式,我想强调这一点。
在上图中,我想画一条线,勾勒出散点的形状。更具体地说,一条线通过每个 hour
刻度的最大值 value
。
我试过添加 freqpoly
和 hist
之类的东西,但它们不适合这种绘图类型(其中 x
是时间戳)。我也试过计算每个时间戳的最大值,但我不能在同一个容器中使用它,因为原始数据是高格式的(每个时间戳都有多个条目)。
示例数据:
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()
这就是我希望最终产品的样子(手工添加的黑线):
您可以通过将 geom_point
的美感转移到实际论点中来做到这一点。然后你可以添加一个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()
这允许您添加线条,然后将汇总统计添加为新的 "line" 几何图形。
我在几天内有数千个跨越 10 分钟时间戳的数据点。
绘制散点图会产生重复模式,我想强调这一点。
hour
刻度的最大值 value
。
我试过添加 freqpoly
和 hist
之类的东西,但它们不适合这种绘图类型(其中 x
是时间戳)。我也试过计算每个时间戳的最大值,但我不能在同一个容器中使用它,因为原始数据是高格式的(每个时间戳都有多个条目)。
示例数据:
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()
这就是我希望最终产品的样子(手工添加的黑线):
您可以通过将 geom_point
的美感转移到实际论点中来做到这一点。然后你可以添加一个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()
这允许您添加线条,然后将汇总统计添加为新的 "line" 几何图形。