2014-11-20 2 views
0

ggplot을 사용하여 동일한 막대에 동일한 막대를 여러 번 플롯하려고합니다.R-ggplot에서 동일한 막대를 두 번 (또는 여러 번) 그린 방법은 무엇입니까?

내 코드 (나는 Empetrum_를 그래프에 세 번 플롯 할) :

temp10A <- ggplot(data, aes(x=Combination, y=Max.Max.Temp, fill=Combination)) 
+ geom_bar(position=position_dodge(), stat="identity") 
+ geom_errorbar(aes(ymin=Max.Max.Temp-se, ymax=Max.Max.Temp+se), width=.2, position=position_dodge(.9)) 
+ scale_x_discrete(limits=c(**"Empetrum_"**, "Calluna_", "Empetrum_Calluna" , "Pleurozium_", "Hypnum_", "Pleurozium_Hypnum", "Pleurozium_", "Calluna_","Pleurozium_Calluna", "Hypnum_", "Empetrum_"**,"Hypnum_Empetrum", "Hypnum_", "Calluna_","Hypnum_Calluna" , "Pleurozium_", **"Empetrum_"**, "Pleurozium_Empetrum")) 
+ theme(axis.text.x = element_text(angle = 45, hjust = 1)) 

enter image description here

당신이 내가 Empetrum_을 플롯하고 싶은 두 번째와 세 번째 시간을 잎 볼 수 있듯이. 내가 Hypnum_, Pleurozium_ 및 Calluna_에 대해 여러 번 그릴 때도 마찬가지입니다.

누구든지이 자동 복제가 중단되거나 다른 해결 방법을 피하는 방법을 알고 있습니까?

미리 감사드립니다.

답변

0

실제로 이것이 ggplot 내에서 가능하지 않다고 생각합니다. 그러나 원하는 결과를 얻으려면 데이터를 쉽게 조작 할 수 있습니다. 예를 들어 아래의 예에서 I rbind 데이터에 추가 행이 있습니다. 여기서 요소 수준에는 추가 공백이 있습니다. 재현 할 수있는 예제를 얻으려면 mtcars을 사용했습니다.

# original plot 
ggplot(mtcars, aes(factor(cyl))) + geom_bar() 

# function to duplicate a factor level 
duplicate.factor.levels <- function(df, f, lvl, times=1){ 
    # df: data.frame 
    # f: factor 
    # lvl: level of factor 
    # times: number of duplicates 
    df[, f] <- factor(df[, f]) 
    for (i in 1:times){ 
    df.lvl <- df[df[, f]==lvl, ] 
    lvl <- paste0(' ', lvl, ' ') # just adds spaces before and after 
    df.lvl[, f] <- lvl 
    df <- rbind(df, df.lvl) 
    } 
    return(df) 
} 
# duplicate cyl == 4 two more times 
mtcars.2 <- duplicate.factor.levels(mtcars, 'cyl', 4, 2) 
# plot (same as before with new data) 
p <- ggplot(mtcars.2, aes(factor(cyl))) + geom_bar() 
# scale (add spaces to limits...) 
p + scale_x_discrete(limits=c('4', '6', ' 4 ', '8', ' 4 ')) 
+0

대단히 감사합니다. – user2170248

관련 문제