使用ggplot2的簇状图

问题描述 投票:0回答:1

Snapshot of my data frame

基本上,我想显示按方法分组的条形图,即,我想显示进行测试的人数,每种方法的阳性测试结果数。另外,我想在栏上将所有数字和百分比显示为标签。我正在尝试使用ggplot2显示这些。但是我每次都失败。

任何帮助。

提前感谢

r ggplot2 bar-chart
1个回答
0
投票

我不确定您是否完全了解您的问题。但我建议您看看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

数据:] >>

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

它回答了您的问题吗?如果不是,是否可以通过在ggplot中添加您到目前为止尝试过的代码来澄清问题?

© www.soinside.com 2019 - 2024. All rights reserved.