2017-09-26 4 views
1

나는이 데이터 프레임에서 ggplot2로 막 대형 차트를 작성하여 Uniprot 및 macki 열의 데이터를 비교하려고합니다.가로 막대 그래프와 함께 ggplot2

Famille Uniprot Macki 
SN_02  26   7 
SN_03  21   22 
SN_04  16   7 
SN_05  4   0 
SN_09  10   0 
SN_10  5   0 
SN_17  6   0 
SN_19  13   4 
SN_20  3   1 
SN_31  2   0 
SN_32  5   3 
SN_33  3   0 
SN_34  1   0 
SN_37  3   0 

이 내 R 코드 :

bar <- c(26, 21, 16,4,10,5,6,13,3,2,5,3,1,3,7,22,7,0,0,0,0,4,1,0,3,0,0,0) 
lab <- c("SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17","SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37", "SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37") 
pct <- round(bar/sum(bar)*100) 
lab <- paste(lab,"%", sep = "") 
lab <- paste(lab, pct) 
lab0 <- c("SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37", "SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37") 
nlab <- length(lab) 
type <- rep("Uniprot:MACKI", each=nlab/2) 
library(ggplot2) 
theme_set(theme_bw()) 
dd <- data.frame(lab0, type, bar, pct) 
dd$lab0 <- reorder(dd$lab0,-dd$bar) 
ggplot(dd,aes(x=lab0,y=bar,fill=lab0))+geom_bar(aes(alpha=factor(bar)), stat = "identity", 
position=position_dodge(width=1))+scale_alpha_discrete(range=c(0.5,1.0))+ 
geom_text(aes(label=paste0(pct,"%"),group=interaction(lab0,type)),hjust=-0.5, position=position_dodge(width=1)) 
+ coord_flip()+expand_limits(y=20)+labs(x="",y="total") 

그리고 이것은 내가 당신이 내 코드를 디버깅 도와 줄 수

this horizontal bar plot있어 음모인가? 감사합니다

답변

1

ggplot2에 개체를 추가하는 것은 동일한 줄에 있어야합니다. +입니다. 마지막으로 한 행을 살펴보십시오. +을 입력해야합니다. 수정 코드가

bar <- c(26, 21, 16,4,10,5,6,13,3,2,5,3,1,3,7,22,7,0,0,0,0,4,1,0,3,0,0,0) 
lab <- c("SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17","SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37", "SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37") 
pct <- round(bar/sum(bar)*100) 
lab <- paste(lab,"%", sep = "") 
lab <- paste(lab, pct) 
lab0 <- c("SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37", "SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37") 
nlab <- length(lab) 
type <- rep("Uniprot:MACKI", each=nlab/2) 
library(ggplot2) 
theme_set(theme_bw()) 
dd <- data.frame(lab0, type, bar, pct) 
dd$lab0 <- reorder(dd$lab0,-dd$bar) 
ggplot(dd,aes(x=lab0,y=bar,fill=lab0))+ 
    geom_bar(aes(alpha=factor(bar)) , stat = "identity", position = position_dodge(width=1)) + 
    scale_alpha_discrete(range=c(0.5,1.0))+ 
    geom_text( 
    aes(label=paste0(pct,"%") , group=interaction(lab0,type)), 
    hjust = -0.5, 
    position = position_dodge(width=1)) + 
    coord_flip()+ 
    expand_limits(y = 20) + 
    labs(x = "",y = "total") 

를 작동 한 후 난 당신이 또한 적절한 읽기 쉬운 방식으로 코드를 작성하는 데 도움이 될 r-code-formatting-guide을 따르도록 권장합니다. 행운을 빕니다!

관련 문제