2017-02-22 1 views
1

Image of sample data두 변수 측면 사이드 바 음모에 의해 측면을 만들려고 노력

을 ggplot하지만 바는 서로의 뒤에 것 같다 있지만 나란히 할 수 없습니다 는 이미지를 볼 수

위해
afg <- read.table(header=TRUE, 
text="FG biomass stdev Year 
1 287.6 237.5 2015 
1 254.2 220.6 2016 
2 309.9 126.3 2015 
2 307.6 139.5 2016 
3 339.6 175.5 2015 
3 349.3 160.6 2016") 

library(ggplot2) 
ggplot(afg,aes(afg$FG,afg$biomass,fill=afg$Year)) + 
geom_bar(stat="identity",position=position_dodge(0.9),color="black") 

library(reshape2) 
afg.long <- melt(afg$Year,id="year") 
ggplot(afg.long,aes(afg$FG,afg$biomass,fill=afg$Year)) + 
geom_bar(stat="identity",position = "dodge") 
+0

내가 내 데이터는 당신이 그림을 클릭하면 내가이 튀어 나올 것 같아요 –

+0

을 설정하는 방법의 사진을 추가 한, 올바르게 내 처음 –

답변

2

enter image description here

는 범주 형 변수로 취급하는 열 Year 필요가 factor 유형으로 변환 할 수 있습니다. 또한 $을 사용하는 변수 선택은 aes() 함수 내에서 절대로 사용해서는 안됩니다. 나는이 일을하고 있지 않다 경우

library(ggplot2) 

p <- ggplot(afg, aes(x=FG, y=biomass, fill=factor(Year))) + 
    geom_bar(stat="identity", position="dodge") 

ggsave("dodged_barplot.png", plot=p, height=4, width=6, dpi=150) 


# Note that 'Year' is type integer. 

str(afg) 
# 'data.frame': 6 obs. of 4 variables: 
# $ FG  : int 1 1 2 2 3 3 
# $ biomass: num 288 254 310 308 340 ... 
# $ stdev : num 238 221 126 140 176 ... 
# $ Year : int 2015 2016 2015 2016 2015 2016 

enter image description here

+0

미안 고마워, 그것을 얻었다 !!! –