0

I would like to make a bar plot with percent format. here is my data set:

https://drive.google.com/file/d/1xpRqQwzKFuirpKYKcoi1qVYSaiA-D5WX/view?usp=sharing

load('test.Robj')

Here is my part of data looks like:

                     res.1.2 branch
AAACCTGCACCAGGCT       0      1
AAACCTGGTCATATGC       7      4
AAACCTGGTTAGTGGG      15     NA
AAACCTGTCCACGCAG       1     NA
AAACCTGTCCACGTTC      17      2
AAACGGGCACCGAATT       0      1

I tried to use this code to plot:

ggplot(test,aes(x = branch, y =factor(1),fill = res.1.2)) + 
  geom_bar(position = "fill",stat = "identity")+
  scale_y_discrete(labels =scales::percent)

I want to make my y axis as percent of counts of res.1.2 in total(stacked bar chart, or similar to a pie chart), quite similar to this issue but I got this: enter image description here

Any suggestion?

Uwe
  • 41,420
  • 11
  • 90
  • 134
MichaelXu
  • 13
  • 2
  • 1
    Possible duplicate of [Show % instead of counts in charts of categorical variables](https://stackoverflow.com/questions/3695497/show-instead-of-counts-in-charts-of-categorical-variables) – Rich Pauloo Mar 26 '18 at 04:17
  • I tried scale_y_continuous or scale_y_discrete, but it seems not to be working on character format. – MichaelXu Mar 26 '18 at 04:21
  • This: `y =factor(1)` looks suspicious. (The graph doesn't look like what I would expect from that data so I'm not making any efforts at coding.) – IRTFM Mar 26 '18 at 05:04
  • Please [edit] your question and post the result of `dput(test)`. Without, it will be difficult to track down the issue. Thank you. – Uwe Mar 26 '18 at 05:10
  • @Uwe, you are amazing! why did you delete your post? it works for me ! Really appreciate your help! It seems as.integer(res.1.2) helps, can you explain that a little? – MichaelXu Mar 26 '18 at 06:02
  • @MichaelXu I deleted my post because I was not sure if I had understood your question correctly. Now, I have undeleted my answer and have added an alternative solution. – Uwe Mar 26 '18 at 06:13

1 Answers1

0

If I understand correctly, the OP wants to plot the values of res.1.2 which are of type character. So, res.1.2 needs to be coerced to integer for plotting:

# load OP's data
load('test.Robj')
# create plot
library(ggplot2)
ggplot(test,aes(x = branch, y = as.integer(res.1.2), fill = res.1.2)) + 
  geom_bar(position = "fill",stat = "identity") +
  scale_y_continuous(labels = scales::percent)

enter image description here

However, if the OP intends to show the number of occurrences of each value of res.1.2 as share of total within each branch, the code is as follows:

# load OP's data
load('test.Robj')
# create plot
library(ggplot2)
ggplot(test, aes(x = branch, fill = res.1.2)) +  
  geom_bar(position = "fill") + 
  scale_y_continuous(labels = scales::percent)

enter image description here

The chart shows the counts of res.1.2 as percentage for each branch

Uwe
  • 41,420
  • 11
  • 90
  • 134