-1

I was recently working with some output and I can't seem to plot it informatively. The output looks like the following:

180,A,71
180,C,61
180,G,68
180,U,78
182,A,70
182,C,34
182,G,123
182,U,51

I would like to plot this data so i have on the x axis the first column, and on the y axis bars which are filled according to four different types(column 2) and their frequencies (column 3). So on y axis would be frequency of all types on one value from first column, but that bar would be divided according to size of types.

I hope the question was clear and thanks for any help.

sdgaw erzswer
  • 2,182
  • 2
  • 26
  • 45
  • 2
    What is your question, exactly? What did you already try yourself? – Heroka Jan 05 '16 at 14:14
  • I've tried aggregate(y~x,data=data), and I don't know how to create a plot with bars composed of fractions. – sdgaw erzswer Jan 05 '16 at 14:17
  • 2
    Possible duplicate of [Stacked bar plot in r with summarized data](http://stackoverflow.com/questions/16630069/stacked-bar-plot-in-r-with-summarized-data). Your data is already in the right format though. – Heroka Jan 05 '16 at 14:18
  • Is it really? Thank you I will try this method and I shall report If it works. – sdgaw erzswer Jan 05 '16 at 14:19
  • (Assuming the column names are V1, V2 and V3, you could try `library(ggplot2) p1 <- ggplot(dat, aes(x=V1,y=V3,group=V2,fill=V2))+ geom_bar(stat="identity",position="stack")` – Heroka Jan 05 '16 at 14:20

1 Answers1

1

How's this?

df <- data.frame(X=rep(c(180,182), each=4), Group=rep(c("A","C","G","U"),2),
             Y=c(71,61,68,78,70,34,123,51))

# Calculating percentages (just using base)
groupSum <- tapply(df$X, df$Group, sum)
df$Label <- paste0(round(100 * df$Y / groupSum[df$Group], 1), "%")

# Go for the plot
library(ggplot2)

ggplot(data=df, aes(x=X, y=Y,fill=Group)) + 
    geom_bar(position="dodge", stat="identity") + 
    scale_x_continuous(breaks=unique(df$X))

The last part only labels the x values actually used. enter image description here

And this is what @Haroka's plot would look like (with percentages now added as per request -- also see here):

ggplot(data=df, aes(x=X, y=Y,fill=Group)) +
  geom_bar(position="stack", stat="identity") +
  scale_x_continuous(breaks=unique(df$X)) +
  geom_text(aes(label = Label), size=12, hjust=0.5, vjust=3, position="stack")

enter image description here

Community
  • 1
  • 1
dougmet
  • 635
  • 4
  • 19
  • Simultaneous answering with @Heroka – dougmet Jan 05 '16 at 14:27
  • Thanks, I've tried it and it works, one more thing: how do I put on x only the values in the data and not the values inbetween? – sdgaw erzswer Jan 05 '16 at 14:36
  • I use scale functions for this: ` + scale_x_continuous(breaks=unique(df$X))` The unique isn't strictly necessary but more expandable. Will update the answer. – dougmet Jan 05 '16 at 14:42
  • Thanks again, I've managed to obtain the first plot before, but you explained it perfectly. – sdgaw erzswer Jan 05 '16 at 15:32
  • One more thing, is it possible to plot percentage fractions within different colours? Sorry for late reply, It just hit me I would greatly benefit from that. – sdgaw erzswer Jan 05 '16 at 20:41
  • Sure. For that you need another geom. This time text. I've updated the answer again. – dougmet Jan 06 '16 at 09:21