2012-12-24 78 views
6

我使用這個數據集(在底部)來創建一個密度圖,但是我們遇到了這個因素的問題並且讓它正確聚合。我想這張圖看起來像這樣:使用ggplot2創建一個密度圖使用一個因子

ggplot(sample, aes(as.numeric(value), colour=shortname)) + geom_density() 

但是我希望x軸具有實際的因子標籤。但是,當我使用這個:

ggplot(sample, aes(value, colour=shortname)) + geom_density() 

該圖形不聚集他們進入shortname變量的兩個不同的值。

我在做什麼錯?我讀過有關使用scale_x_discrete(),但我不認爲我應該需要,因爲我已經有一個因素......

UPDATE:

ggplot(sample, aes(value, colour=shortname)) + geom_density() + scale_x_discrete(breaks=1:27, labels=c("<A",LETTERS)) 

:即使我以下列方式使用scale_x_discrete只是將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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"), value = structure(c(7L, 17L, 8L, 15L, 
18L, 17L, 14L, 19L, 20L, 17L, 17L, 12L, 16L, 21L, 2L, 21L, 19L, 
22L, 12L, 15L, 22L, 19L, 16L, 13L, 19L, 24L, 15L, 24L, 23L, 12L, 
24L, 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") 

回答

5

你可以得到所需的行爲:

ggplot(sample, aes(value, colour=shortname, group=shortname)) + geom_density() 

注意字母的標籤,在情節的底部,這是不存在與as.numeric解決方案:

enter image description here

+0

簡短而甜美。謝謝! –

+0

你能解釋一下ggplot2背後的推理嗎? 'group'映射是否會改變'stat_density'如何計算?爲什麼沒有指定'group'時有多條密度線? – Heisenberg