2016-09-29 35 views
-2

假設我有一個數字序列:如何使用地塊編號序列GGPLOT2

> dput(firstGrade_count) 
c(4L, 346L, 319L, 105L, 74L, 5L, 124L, 2L, 10L, 35L, 6L, 206L, 
7L, 8L, 6L, 9L, 26L, 1L, 35L, 18L, 4L, 4L, 2L, 63L, 6L, 23L, 
6L, 82L, 10L, 17L, 45L, 74L, 10L, 8L, 14L, 23L, 26L, 53L, 55L, 
16L, 2L, 141L, 113L, 98L, 179L, 13L, 34L, 16L, 8L, 144L, 2L, 
141L, 26L, 9L, 125L, 201L, 32L, 452L, 179L, 30L, 4L, 141L, 5L, 
40L, 7L, 255L, 120L, 223L, 28L, 252L, 21L, 8L, 362L, 4L, 5L, 
2L, 285L, 18L, 76L, 5L, 73L, 11L, 367L, 7L, 50L, 6L, 37L, 15L, 
48L, 5L, 12L, 7L, 96L) 

我想用ggplot2所以結果將類似的東西繪製它:

barplot(firstGrade_count) 

如何定義ggplot2中的美學?

這裏是基地情節產生的情節,我上面提到的:

enter image description here

回答

1
firstGrade_count <- c(4,346,319,105,74,5,124,2,10,35,6,206,7,8,6,9,26,1,35,18,4,4,2,63,6,23,6, 
82,10,17,45,74,10,8,14,23,26,53,55,16,2,141,113,98,179,13,34,16,8,144,2,141,26,9, 
125,201,32,452,179,30,4,141,5,40,7,255,120,223,28,252,21,8,362,4,5,2,285,18,76,5,73, 
11,367,7,50,6,37,15,48,5,12,7,96) 

您可以選擇製作成數據幀這使它更易於使用和持有任何額外功能,儘管這不是必需的。

library(ggplot2) 

# Optional transformation to data.frame: 
firstGrade_count <- as.data.frame(firstGrade_count) 

# Index for x-coordinates 
firstGrade_count$index <- seq(1:nrow(firstGrade_count)) 

# Plotting 
c <- ggplot(firstGrade_count, aes(index,firstGrade_count)) 
c + geom_bar(stat = "identity") 

enter image description here

或者,

firstGrade_count <- as.data.frame(firstGrade_count) 
c <- ggplot(firstGrade_count, aes(factor(firstGrade_count))) 
c + geom_bar() 

我把它的方式顯示了每個唯一值的計數。有許多變化和其他格式,你可以添加:

enter image description here

如果你不想計數可以添加stat =選項並將其從默認到別的改變。

+1

謝謝!這是非常豐富的! –

0

不會造成任何數據幀:

ggplot() + 
geom_bar(aes(1:length(firstGrade_count),firstGrade_count), stat='identity') + 
xlab('') 

enter image description here

+0

謝謝,這正是我期待的:) –