0

Snapshot of my data frame

Basically I want to display a barplot which is grouped by Methods i.e I want to display the number of people conducted the tests, the number of positive test results had found for each of the methods. Also, I want to display all the numbers and percentages as labels on the bar. I am trying to display these using ggplot2. But I am failing every time.

Any helps.

Thanks in advance

1 Answer 1

3

I'm not sure to have fully understand your question. But I will suggest you to take look on geom_text.

library(ggplot2)

ggplot(df, aes(x = methods, y = percentage)) + 
  geom_bar(stat = "identity") + 
  geom_text(aes(label = paste0(round(percentage,2), " (",positive," / ", people,")")), vjust = -0.3, size = 3.5)+
  scale_x_discrete(limits = c("NS1", "NS1+IgM", "NS1+IgG","Tourniquet")) + 
  ylim(0,100)

enter image description here

Data:

df = data.frame(methods = c("NS1", "NS1+IgM","NS1+IgG","Tourniquet"),
                people = c(542,542,541,250),
                positive = c(505,503,38,93))
df$percentage = df$positive / df$people * 100

> df
     methods people positive percentage
1        NS1    542      505   93.17343
2    NS1+IgM    542      503   92.80443
3    NS1+IgG    541       38    7.02403
4 Tourniquet    250       93   37.20000

Does it answer your question ? If not, can you clarify your question by adding the code you have tried so far in ggplot ?

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it does answer my question. I was using the following codes. >library(ggplot2) >library(reshape2) >df=data.frame(Methods=c('NS1', 'NS1+IgM', 'NS1+IgG', 'Tourniquet Test'), Positive=c(505,503,38, 93), Percent=c(93.2, 92.8,7.0,37.2)) >mm= melt(df, id.vars='Methods') >q=ggplot(mm, aes(x=Methods, y=value))+geom_bar(stat='identity')+facet_grid(.~variable)+coord_flip()+labs(x='', y='') Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.