[R] ask help for ggplot

Aimin Yan @|m|n@@t@work @end|ng |rom gm@||@com
Fri Jun 5 21:49:57 CEST 2020


Thank you, it is very helpful.

I tried the following way to generate stacked bar plot for trt 'M6' and
'M12'

However, the label position of legend in 'M12' is not what I want,
actually in the legend I also want to keep "Others" in the bottom(like the
gene order in stacked bar)

In addition, how to  make  a stacked bar plot for 'M6','M12' and 'M18'
together with different legends('M6', 'M12', 'M18')

Thank you,

Aimin

df.1 <- df[df$trt=='M6',]

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

df.1$trt <- factor(df.1$trt,levels=unique(as.character(df$trt)))
df.1$gene <- factor(df.1$gene,levels = g)

df.1 %>% ggplot(aes(x=trt,y=freq, fill = gene, group = gene)) +
  geom_bar(stat = "identity", width = 0.5) +
  scale_fill_manual(breaks = df$gene, values = df$cols) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1,size = 4)) +
theme(legend.position="bottom")+guides(fill=guide_legend(title=df.1$trt,title.position
= "top", ncol=1, keyheight=0.35, default.unit="inch"))

df.2 <- df[df$trt=='M12',]

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

df.2$trt <- factor(df.2$trt,levels=unique(as.character(df$trt)))
df.2$gene <- factor(df.2$gene,levels = g)

df.2 %>% ggplot(aes(x=trt,y=freq, fill = gene, group = gene)) +
  geom_bar(stat = "identity", width = 0.5) +
  scale_fill_manual(breaks = df$gene, values = df$cols) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1,size = 4)) +
theme(legend.position="bottom")+guides(fill=guide_legend(title=df.2$trt,title.position
= "top", ncol=1, keyheight=0.35, default.unit="inch"))




On Fri, Jun 5, 2020 at 5:36 AM Rui Barradas <ruipbarradas using sapo.pt> wrote:

> 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.
> >
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list