我发现了这个惊人的 R 包ggridges
,并想将它用于图表,但是,我无法执行代码,因为我可能错误地放置了我的变量。我希望有人可以指导我获得我想要的结果。
请在 R 中查看我的下表
数据框
g<-data.frame(structure(list(session = c("s1", "s1", "s1", "s1", "s1", "s1",
"s1", "s1", "s1", "s1", "s1", "s1", "s1", "s1", "s1", "s1", "s1",
"s1", "s1", "s1", "s1", "s1", "s2", "s2", "s2", "s2", "s2", "s2",
"s2", "s2", "s2", "s2", "s2", "s2", "s2", "s2", "s2", "s2", "s2",
"s2", "s2", "s3", "s3", "s3", "s3", "s3", "s3", "s3", "s3", "s3",
"s3", "s3", "s3", "s3", "s3", "s3", "s3", "s3", "s3", "s3", "s3",
"s3", "s4", "s4", "s4", "s4", "s4", "s4", "s4", "s4", "s4", "s4",
"s4", "s4", "s4", "s4", "s4", "s4", "s4", "s5", "s5", "s5", "s6",
"s6", "s6", "s6", "s7", "s7", "s7", "s7", "s7"), interval = structure(c(1L,
1L, 2L, 2L, 3L, 3L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L,
10L, 10L, 11L, 11L, 12L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L,
5L, 6L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 12L, 1L, 2L, 2L, 3L, 3L,
4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L,
12L, 1L, 2L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L,
10L, 11L, 12L, 9L, 11L, 12L, 1L, 7L, 11L, 12L, 3L, 4L, 5L, 6L,
7L), .Label = c("120", "240", "360", "480", "600", "720", "840",
"960", "1080", "1200", "1320", "1440"), class = "factor"), person = c("person2",
"person1", "person2", "person1", "person2", "person1", "person1", "person2",
"person1", "person2", "person1", "person2", "person1", "person2", "person1",
"person2", "person1", "person2", "person1", "person2", "person1", "person1",
"person2", "person1", "person2", "person1", "person2", "person1", "person2",
"person1", "person2", "person1", "person2", "person1", "person1", "person1",
"person1", "person1", "person1", "person2", "person1", "person1", "person2",
"person1", "person2", "person1", "person1", "person2", "person1", "person2",
"person1", "person2", "person1", "person2", "person1", "person2", "person1",
"person2", "person1", "person2", "person1", "person1", "person2", "person2",
"person2", "person2", "person1", "person2", "person1", "person2", "person1",
"person2", "person1", "person2", "person1", "person1", "person2", "person2",
"person2", "person1", "person2", "person2", "person1", "person1", "person1",
"person1", "person1", "person1", "person1", "person1", "person1"), counts = c(12L,
51L, 15L, 43L, 4L, 54L, 42L, 12L, 51L, 9L, 73L, 3L, 38L, 12L,
58L, 13L, 58L, 9L, 62L, 13L, 40L, 21L, 2L, 8L, 31L, 15L, 27L,
29L, 17L, 13L, 22L, 4L, 9L, 29L, 25L, 4L, 26L, 10L, 4L, 1L, 7L,
18L, 3L, 19L, 2L, 36L, 38L, 12L, 29L, 8L, 15L, 10L, 20L, 14L,
11L, 9L, 23L, 13L, 20L, 8L, 6L, 10L, 1L, 6L, 15L, 6L, 5L, 4L,
17L, 7L, 4L, 6L, 2L, 6L, 14L, 4L, 2L, 12L, 1L, 3L, 5L, 4L, 19L,
7L, 6L, 12L, 4L, 6L, 11L, 9L, 8L)), row.names = c(NA, -91L), class = c("tbl_df",
"tbl", "data.frame")))
列
- session:是我的数据的分组上下文
- 间隔:是每个会话中的常规预定义时隙/时间戳(此处:每 120 秒)
- person : 有两个人从他们那里获取数据
- counts:是每个会话中每个人在每个时间间隔内进行的观察次数
代码
我首先尝试了以下代码ggridges
:
ggplot(
g,
aes(x = `counts`, y = `session`, fill = stat(x))
) +
geom_density_ridges_gradient(scale = 3, size = 0.3, rel_min_height = 0.01) +
scale_fill_viridis_c(name = "counts", option = "C")
输出
这个输出看起来很有希望。添加以下行将+ facet_grid(.~person)
为我提供第 1 个人和第 2 个人的图表,这正是我正在寻找的。很高兴看到颜色代表计数的频率(较深的颜色代表较少,较浅的颜色代表更多计数)
问题描述
我对代码的主要问题是,无论我如何扭曲和调整它(添加height = counts, fill= counts
),我都看不到图表,因为代码的某些部分有效而其他部分无效。尤其是,
- session:应该显示在 y 轴上(有效!)
- 间隔:应该显示在 x 轴上(有效!)
- counts:应该显示为密度曲线的高度,并且根据计数的频率具有不同的渐变颜色(不起作用!)
- 人:我想为第 1 个人和第 2 个人分别绘制图表(如果我添加则有效
+ facet_grid(.~person)
)
所需图表
虽然是用纸笔方法制作的,而且不是很优雅,但这是我想要得到的理想图表。密度曲线上的峰值(圆圈)对应counts
于每个会话和我的数据框中的每个人所指出的。
也许我的方法是错误的,并且该软件包不是我正在寻找的软件包,但如果不是,如果在 R 方面具有更高专业知识的人可以就这个问题向我提供建议,我将不胜感激。