[R] ask help for ggplot

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Fri Jun 5 11:36:33 CEST 2020


Hello,

Something like this?


g <- unique(as.character(df$gene))
i <- which(g == "Others")
g <- c(g[i], g[-i])
df$trt <- factor(df$trt,levels=unique(as.character(df$trt)))
df$gene <- factor(df$gene,levels = g)

ggplot(df, aes(x=trt,y=freq, fill = gene, group = gene)) +
   geom_bar(stat = "identity", width = 0.5,
            position = position_fill()) +
   scale_fill_manual(breaks = df$gene, values = df$cols) +
   theme(axis.text.x = element_text(angle = 45, hjust = 1,size = 4))


But this places "Others" at the top of each bar.
To move it to the bottom, instead of the code that creates 'g' run

g <- unique(as.character(df$gene))
i <- which(g == "Others")
g <- c(g[-i], g[i])


Hope this helps,

Rui Barradas


Às 05:14 de 05/06/20, Aimin Yan escreveu:
> Is there possible to generate a barplot in the following link using ggplot?
> 
> https://photos.app.goo.gl/E3MC461dKaTZfHza9
> 
> here is what I did
> 
> library(ggplot2)
> 
> df <- read.csv(text=
> "trt,gene,freq,cols
> M6,ALDH16A1,100.0000000,red
> M6,Others,0.0000000,lightgrey
> M12,ALDH16A1,64.6638015,red
> M12,GBE1,2.0074865,#4C00FF
> M12,ZNF598,1.5832525,#004CFF
> M12,CHMP6,1.3503397,#00E5FF
> M12,C20orf27,1.2033828,#00FF4D
> M12,NEGR1,0.9676972,#4DFF00
> M12,TNFAIP6,0.9122418,#E6FF00
> M12,ZSCAN25,0.7375572,#FFFF00
> M12,BCL2,0.6848745,#FFDE59
> M12,CBL,0.6765562,#FFE0B3
> M12,Others,25.2128102,lightgrey
> M18,ALDH16A1,42.4503581,red
> M18,ATF2,2.2360682,#4C00FF
> M18,DIAPH1,1.5256507,#004CFF
> M18,SESTD1,1.2053805,#00E5FF
> M18,TFCP2,1.1587958,#00FF4D
> M18,SCAPER,1.1180341,#4DFF00
> M18,CUX1,1.0306877,#E6FF00
> M18,TEX10,0.9841030,#FFFF00
> M18,C6orf89,0.9666337,#FFDE59
> M18,PTTG1IP,0.9258720,#FFE0B3
> M18,Others,46.3984161,lightgrey")
> 
> df$trt <- factor(df$trt,levels=unique(as.character(df$trt)))
> df$gene <- factor(df$gene,levels = unique(as.character(df$gene)))
> 
> ggplot(df, aes(x=trt,y=freq, fill = gene))+geom_bar(stat = "identity",
> width = 0.5,color="black") + theme(axis.text.x = element_text(angle = 45,
> hjust = 1,size = 4))
> 
> df$cols is the color I want to use to label different gene in M6, M12,M18
> as shown in Figure, and in each bar, the 'Others' of df$gene is always in
> the bottom of bar in M6,M12,M18
> 
> Thank you
> 
> Aimin
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list