-3

I have a dataset with 15 metrics (columns) from a csv. 1 metric is called Cancer

This is what the column in the dataset looks like

Cancer:  yes no yes no

I just would like to have a barchart (stacked and normal) showing the percentages yes and no from Cancer

Kirsten
  • 37
  • 1
  • 6

2 Answers2

1

This should do the desired stacked plot using 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)

stacked plot

  • Thanks it works. Can you also make a simple table of the same with percentages yes/no cancer? But I am making a yes/no overview from different subgroups (filter: age 50-54, agebirthfirstchild <30). So all subgroups yes/no percentages in one table – Kirsten Feb 09 '20 at 18:50
  • How to do the same when I different variables with sub categories? in them such as if i have gender contains male female , disease contains multiple sub-types etc – PesKchan Mar 02 '22 at 07:31
0

1.Create minimal reproducible example

df <- data.frame(Cancer = c("yes", "yes", rep("no", 8)))

2.A solution using ggplot2 and scales

If you only need the summaries for the plot we can use ggplot directly:

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 [%]")

enter image description here

dario
  • 6,415
  • 2
  • 12
  • 26