我正在使用这个数据集(在底部)来创建密度图,但是我遇到了因素问题并让它正确聚合。我希望图表看起来像这样:
ggplot(sample, aes(as.numeric(value), colour=shortname)) + geom_density()
但我希望x轴具有因子的实际标签。但是当我使用它时:
ggplot(sample, aes(value, colour=shortname)) + geom_density()
图表不会将它们聚合为shortname
变量的两个不同值。
我做错了什么?我读过关于使用scale_x_discrete()
的内容,但我认为我不应该这样做,因为我已经有了一个因素......
更新:即使我以下列方式使用scale_x_discrete
:
ggplot(sample, aes(value, colour=shortname)) + geom_density() + scale_x_discrete(breaks=1:27, labels=c("<A",LETTERS))
只是一起删除x轴标签......
提前谢谢!
sample <- structure(list(shortname = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L,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 21L, 15L, 16L, 16L, 18L, 18L, 8L, 23L, 8L, 21L, 24L, 13L,
10L, 18L, 1L, 7L, 14L, 13L, 21L, 16L, 10L, 15L, 21L, 17L, 18L,
18L, 21L, 14L, 9L, 22L, 14L, 11L, 16L, 13L, 18L, 12L, 1L, 23L,
8L, 15L, 18L, 11L, 10L, 20L, 16L, 12L, 10L, 22L, 25L, 24L, 7L,
19L, 13L, 16L, 16L, 20L, 3L, 13L, 21L, 12L, 16L, 13L, 15L, 1L,
19L, 12L, 20L, 12L, 11L, 20L, 7L, 22L, 18L, 19L, 9L, 10L, 24L,
10L, 13L, 5L, 16L, 19L, 20L, 19L, 18L, 19L, 19L, 13L, 12L, 21L,
20L, 13L, 21L, 3L, 12L, 19L, 17L, 16L, 9L, 21L, 18L, 24L, 2L,
12L, 13L, 14L, 7L, 16L, 10L, 21L, 15L, 21L, 11L, 18L, 3L, 16L,
15L, 22L, 10L, 16L, 21L, 19L, 17L, 20L, 22L, 17L, 20L, 2L, 24L,
12L, 18L, 19L, 24L, 26L, 17L, 20L, 15L, 12L, 10L, 16L, 12L, 12L,
15L, 19L, 14L, 22L, 12L, 7L, 16L, 1L, 20L, 18L, 24L, 19L, 22L,
3L, 16L, 19L, 22L, 5L, 19L, 17L, 16L, 13L, 22L, 3L, 14L, 12L,
9L, 5L, 16L, 14L, 15L, 12L, 2L, 12L, 19L, 20L, 18L, 10L, 3L,
20L, 4L, 16L, 19L, 1L, 14L, 24L, 9L, 14L, 1L, 12L, 6L, 1L, 22L,
11L, 13L, 19L, 16L, 22L, 25L, 3L, 21L, 21L, 22L, 3L, 21L, 18L,
23L, 24L, 2L, 21L, 15L, 15L, 16L, 11L, 13L, 25L, 11L, 17L, 15L,
7L, 23L, 21L, 4L, 1L, 14L, 19L, 13L, 10L, 18L, 3L, 13L, 17L,
12L, 7L, 21L, 17L, 17L, 17L, 17L, 10L, 21L, 24L, 22L, 12L, 22L,
12L, 24L, 17L, 16L, 21L, 19L, 16L, 16L, 16L, 21L, 13L, 1L, 7L,
21L, 11L, 13L, 10L, 21L, 11L, 25L, 1L, 11L, 3L, 24L, 13L, 13L,
15L, 7L, 21L, 16L, 24L, 16L, 8L, 19L, 13L, 18L, 18L, 22L, 19L,
16L, 16L, 15L, 5L, 4L, 14L, 8L, 15L, 18L, 13L, 14L, 12L, 19L,
16L, 3L, 16L, 17L, 1L, 19L, 20L, 19L, 1L, 19L, 20L, 22L, 8L,
12L, 13L, 24L, 16L, 14L, 21L, 25L, 22L, 4L, 16L, 16L, 15L, 16L,
8L, 14L, 12L, 11L, 5L, 13L, 19L, 27L, 3L, 18L, 12L, 13L, 19L,
7L, 10L, 15L, 23L, 11L, 3L, 24L, 18L, 15L, 16L, 14L, 16L, 22L,
11L, 11L, 20L, 18L, 14L, 20L, 21L, 3L, 10L, 19L, 14L, 16L, 8L,
12L, 16L, 8L, 21L, 26L, 13L, 6L, 9L, 2L, 15L, 1L, 12L, 24L, 3L,
21L, 24L, 8L, 18L, 20L, 3L, 19L, 12L, 15L, 8L, 18L, 14L, 19L,
10L, 20L, 17L, 12L, 17L, 19L, 14L, 10L, 7L, 11L, 12L, 3L, 19L,
1L, 16L, 11L, 8L, 3L, 10L, 15L, 21L, 27L, 3L, 3L, 19L, 5L, 17L,
22L, 10L, 3L, 15L, 19L, 19L, 18L, 23L, 1L, 22L, 9L, 22L, 19L,
12L, 18L, 10L, 10L, 9L, 14L, 2L, 27L, 21L, 4L, 18L, 1L, 2L, 16L,
3L, 21L, 19L, 24L, 12L, 12L, 19L, 13L, 16L, 19L, 20L, 12L, 20L,
13L, 9L, 15L, 22L, 14L, 5L, 22L, 15L, 3L, 9L, 3L, 12L, 2L, 12L,
12L, 22L, 15L, 9L, 3L, 21L, 14L, 5L, 5L, 10L, 5L, 5L, 1L, 7L,
21L, 19L, 22L, 1L, 9L, 1L, 21L, 18L, 15L, 14L, 21L, 6L, 19L,
15L, 16L, 5L, 5L, 10L, 20L, 5L, 8L, 19L, 3L, 16L, 5L, 7L, 17L,
16L, 19L, 2L, 20L, 15L, 9L, 17L, 21L, 19L, 13L, 3L, 13L, 12L,
21L, 16L, 15L, 17L, 16L, 19L, 8L, 17L, 14L, 1L, 1L, 22L, 19L,
24L, 20L, 10L, 17L, 1L, 17L, 1L, 17L, 13L, 15L, 21L, 6L, 3L,
18L, 20L, 15L, 4L, 16L, 8L, 12L, 10L, 13L, 13L, 22L, 11L, 12L,
1L, 21L, 21L, 5L, 5L, 16L, 11L, 20L, 21L, 20L, 21L, 20L, 19L,
20L, 15L, 25L, 9L, 1L, 12L, 21L, 9L, 24L, 3L, 12L, 24L, 8L, 16L,
15L, 9L, 20L, 15L, 5L, 10L, 1L, 16L, 16L, 12L, 9L, 20L, 10L,
19L, 12L, 3L, 20L, 22L, 11L, 16L, 16L, 22L, 19L, 19L, 22L, 14L,
14L, 12L, 5L, 14L, 19L, 18L, 19L, 18L, 3L, 10L, 20L, 14L, 1L,
13L, 18L, 13L, 1L, 22L, 23L, 19L, 13L, 18L, 9L, 16L, 15L, 17L,
21L, 15L, 18L, 1L, 14L, 14L, 1L, 14L, 9L, 16L, 12L, 22L, 14L,
2L, 22L, 19L, 21L, 16L, 16L, 11L, 19L, 13L, 3L, 16L, 16L, 20L,
18L, 1L, 19L, 11L, 17L, 19L, 12L, 15L, 10L, 11L, 13L, 7L, 14L,
14L, 14L, 15L, 15L, 16L, 14L, 22L, 20L, 17L, 19L, 19L, 13L, 16L,
12L, 15L, 20L, 22L, 17L, 20L, 16L, 10L, 15L, 15L, 12L, 12L, 14L,
20L, 5L, 19L, 2L, 13L, 15L, 17L, 9L, 14L, 18L, 2L, 10L, 14L,
12L, 14L, 12L, 18L, 17L, 13L, 8L, 22L, 12L, 21L, 12L, 13L, 3L,
14L, 26L, 4L, 3L, 1L, 7L, 10L, 19L, 16L, 16L, 15L, 13L, 15L,
16L, 11L, 21L, 12L, 11L, 15L, 1L, 16L, 1L, 17L, 6L, 1L, 16L,
7L, 11L, 2L, 5L, 16L, 5L, 12L, 13L, 12L, 13L, 13L, 12L, 20L,
21L, 21L, 12L, 19L, 21L, 18L, 12L, 15L, 22L, 19L, 16L, 16L, 3L,
14L, 1L, 7L, 13L, 16L, 11L, 7L, 12L, 16L, 16L, 12L, 22L, 1L,
13L, 4L, 8L, 16L, 5L, 11L, 10L, 1L, 21L, 10L, 19L, 12L, 13L,
16L, 12L, 15L, 19L, 13L, 1L, 1L, 2L, 6L, 16L, 14L, 15L, 15L,
16L, 4L, 12L, 16L, 10L, 19L, 12L, 5L, 6L, 10L, 3L, 14L, 1L, 12L,
4L, 11L, 16L, 10L, 20L, 4L, 13L, 10L, 1L, 9L, 2L, 7L, 9L, 18L,
10L, 26L, 14L, 2L, 14L, 10L, 11L, 13L, 1L, 21L, 16L, 9L, 22L,
12L, 12L, 16L, 15L, 12L, 8L, 15L, 20L, 11L, 16L, 15L, 12L, 12L,
16L, 2L, 9L, 12L, 14L, 20L, 1L, 10L, 7L, 10L, 18L, 16L, 12L,
15L, 12L, 14L, 3L, 14L, 6L, 10L, 1L, 11L, 9L, 5L, 12L, 12L, 1L,
8L, 20L, 7L, 21L, 20L, 22L, 20L, 7L, 12L, 9L, 7L, 13L, 19L, 15L,
15L, 18L, 16L, 1L, 10L, 19L, 2L, 13L, 6L, 24L, 1L, 22L, 16L,
11L, 7L, 5L, 19L, 15L, 14L, 12L, 19L, 14L, 12L, 15L, 24L, 15L,
10L, 4L, 14L, 16L, 3L, 21L, 1L, 19L, 14L, 17L, 12L, 21L, 3L,
12L, 16L, 18L, 14L, 15L, 15L, 14L, 1L, 2L, 17L, 1L, 14L, 16L,
15L, 14L, 10L, 14L, 17L, 17L, 12L, 17L, 11L, 14L, 16L, 1L, 1L,
19L, 12L, 24L, 15L, 19L, 14L, 8L, 3L, 22L, 1L, 16L, 15L, 19L,
8L, 15L, 12L, 8L, 14L, 8L, 12L, 7L, 13L, 2L, 13L, 10L, 15L, 15L,
17L, 1L, 26L, 24L, 21L, 25L, 14L, 10L, 13L, 9L, 13L, 18L, 19L,
16L, 21L, 16L, 17L, 14L, 14L, 11L, 17L, 16L, 12L, 17L, 14L, 6L,
24L, 11L, 11L, 11L, 12L, 15L, 13L, 22L, 11L, 17L, 3L, 12L, 17L,
14L, 10L, 11L, 9L, 21L, 18L, 19L, 20L, 24L, 7L, 12L, 22L, 3L,
17L, 10L, 1L, 20L, 1L, 1L, 12L, 2L, 14L, 2L, 17L, 19L, 1L, 10L,
12L, 16L, 15L, 3L, 12L, 16L, 12L, 15L, 17L, 24L, 15L, 16L, 8L,
12L, 14L, 21L, 9L, 23L, 3L, 19L, 16L, 19L, 16L, 16L, 13L, 13L,
3L, 9L, 17L, 1L, 1L, 16L, 11L, 15L, 7L, 7L, 14L, 8L, 14L, 20L,
15L, 16L, 1L, 12L, 9L, 16L), .Label = c("<A", "A", "B", "C",
"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"), class = "factor")), .Names = c("shortname",
"value"), row.names = c(NA, 1156L), class = "data.frame")
答案 0 :(得分:6)
您可以通过以下方式获得所需的行为:
ggplot(sample, aes(value, colour=shortname, group=shortname)) + geom_density()
请注意图表底部的字母标签,as.numeric
解决方案中没有这些标签: