2014-09-28 1 views
0

빈도를 플로팅하는 방법을 묻는 질문이 몇 가지 있습니다. 제안을 구현하려고했지만 여전히 문제가 있습니다.퍼센트 빈도 찾는 방법 gg plot

var <- c(2,2,1,0,1,1,1,1,1,3,2,3,3,5,1,4,4,0,3,4,1,0,3,3,0,0, 
    1,3,2,6,2,2,2,1,0,2,3,2,0,0,0,0,3,2,2,4,3,2,2,0,4,1,0,1,3,1,4,3,1,2, 
    6,7,6,1,2,2,4,5,3,0,6,5,2,0,7,1,7,3,1,4,1,1,2,1,1,2,1,1,4,2,0,3,3,2,2,2,5,3,2,5,2,5) 

나는 다음과 같은 코드를 사용하여 히스토그램을 그려 :

df <- data.table(x = var) 

df <- df[, .N, by=x] 

df$x <- factor(df$x, levels=c(0:25)) 

p <- ggplot(df, aes(x=x, y= N)) + 
    geom_bar(
    stat="identity", width=1.0, 
    colour = "darkgreen", 
    fill = 'paleturquoise4' 
    ) 

p <- p + labs(scale_x_discrete(drop=FALSE)) 

p = p + coord_cartesian(ylim=c(0, 50)) + 
    scale_y_continuous(breaks=seq(0, 50, 2)) 

print(p) 

내가이 있지만 작동하지 않습니다 다음 사용하여 시도

나는 다음과 같은 벡터를 가지고있다.

p <- ggplot(df, aes(x=x, y= N)) + 
    geom_bar(
    aes(y = (..count..)/sum(..count..)), 
    stat="identity", width=1.0, 
    colour = "darkgreen", 
    fill = 'paleturquoise4' 
) 

답변

2

그래픽을 그리기 전에 계산을 수행 할 수 있습니다. 그러나 당신의 접근 방식을 따를 경우, 당신은 이와 같은 것을 원할 것입니다.

ggplot(df, aes(x=x)) + 
    geom_bar(aes(y = N/sum(N)), stat="identity", width=1.0, 
    colour = "dark green", fill = 'paleturquoise4') + 
    ylab("y") 

enter image description here