简体   繁体   English

使用 ggplot2 的簇状条形图

[英]Clustered Bar Plot Using ggplot2

我的数据框的快照

Basically I want to display a barplot which is grouped by Methods ie 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.我正在尝试使用 ggplot2 显示这些。 But I am failing every time.但我每次都失败了。

Any helps.任何帮助。

Thanks in advance提前致谢

I'm not sure to have fully understand your question.我不确定是否完全理解你的问题。 But I will suggest you to take look on geom_text .但我会建议你看看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)

在此处输入图片说明

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 ?如果没有,您能否通过在ggplot添加迄今为止尝试过的代码来澄清您的问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM