Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 条形图百分比子组_R - Fatal编程技术网

R 条形图百分比子组

R 条形图百分比子组,r,R,我有一个数据集,其中包含来自csv的15个指标(列)。1个指标称为癌症 这就是数据集中的列的外观 Cancer: yes no yes no 我只想有一个条形图(堆叠和正常),显示癌症的是和否百分比 这应该使用ggplot2进行所需的堆叠打印 library(ggplot2) ggplot(df,aes(x="", fill = Cancer))+ #Do the bar plot scaled to 1 geom_bar(position = "fill") + #Chang

我有一个数据集,其中包含来自csv的15个指标(列)。1个指标称为癌症

这就是数据集中的列的外观

Cancer:  yes no yes no
我只想有一个条形图(堆叠和正常),显示癌症的是和否百分比


这应该使用
ggplot2
进行所需的堆叠打印

library(ggplot2)

ggplot(df,aes(x="", fill = Cancer))+
  #Do the bar plot scaled to 1
  geom_bar(position = "fill") +
  #Change the y axis labels as percent
  scale_y_continuous(labels = scales::percent)

谢谢,它很管用。你能不能也做一个简单的表格,列出癌症的是/否百分比?但是我从不同的小组(筛选:年龄50-54岁,年龄出生第一个孩子)做了一个是/否的概述。这一个也适用,谢谢
library(ggplot2)
library(scales)
ggplot(df, aes(x = Cancer)) +  
  geom_bar(aes(y = (..count..)/sum(..count..))) + 
  scale_y_continuous(labels=scales::percent) +
  labs(x="Cancer", y="Relative frequency [%]")
library(ggplot2)

ggplot(df,aes(x="", fill = Cancer))+
  #Do the bar plot scaled to 1
  geom_bar(position = "fill") +
  #Change the y axis labels as percent
  scale_y_continuous(labels = scales::percent)