2017-09-18 15 views
0

사용을 막대 그래프에서 장난감 데이터 집합을 피하고 다음R : ggplot2 - 스태킹 및 <code>ggplot2</code> 패키지를 사용하여면 간단한 막대 그래프 생성

library(ggplot2) 
library(reshape2) # to convert to long format 

databas<-read.csv(data= 
        "continent,apples,bananas 
        North America,30,20 
        South America,15,34.5 
        Europe,15,19 
        Africa,5,35") 

databaslong<-melt(databas) 

# plotting as colored bars 
ggplot(databaslong, aes(x=variable, y=value, fill=variable))+ 
    geom_col()+ 
    facet_grid(.~continent) 

를 얻을 :

simple bar plot with facets

사과를 바나나 위에 놓는 방법 (또는 그 반대)? position="stack" (또는 position="dodge")의 지침은 geom_col() 또는 그 밖의 다른 곳에서 적용되지 않는 이유는 무엇입니까?

답변

3

미적 매핑에 x=variable을 지정 했으므로 변수의 각 값 (즉, 사과 & 바나나)은 x 축을 따라 고유 한 위치를 가져오고 스택 할 항목이 없습니다. 당신이 사과 & 바나나는 각 대륙 쌓아하려면

대신 x=continent을 지정할 수 있습니다

ggplot(databaslong, 
     aes(x = continent, y = value, fill = variable)) + 
    geom_col() 

enter image description here