0

I have the dataset and code below which produced the chart attached in image. My questions are:

  1. How can I remove the 75%, 50%, 25% etc from just the red portion (No Disease) of the bars in the barchart? For example, at baseline, I want the bar to show only 25% on the green portion (Disease) of the bar but nothing in the red portion.

  2. How can I add N for sample size at each visit, under the X axis? For example, at baseline and 6m, N=7, at 12m, N=8 and so on.

Any help/suggestions would be appreciated, thanks!

EDIT: Question 1 has already been addressed but I still am unable to figure out question 2, adding N=XX for sample size under each visit. Does anybody have any ideas/suggestions/bits of code that would work for my specific data?

enter image description here

library(tidyverse)

df2 <- df %>% 
  group_by(Visit, TRT) %>% 
  summarize(no_CVD = table(factor(CVD, 0:1))[1],
            CVD = table(factor(CVD, 0:1))[2],
            n = no_CVD + CVD,
            prop = CVD / n,
            lower = suppressWarnings(prop.test(CVD, n)$conf.int[1]),
            upper = suppressWarnings(prop.test(CVD, n)$conf.int[2])) %>%
  mutate(Percent = scales::percent(prop, 0.1),
         CI = paste0("(", scales::percent(lower, 0.1), " - ",
                     scales::percent(upper, 0.1), ")"),
         TRT = paste("Treatment", TRT))




df4<-df %>% filter(TRT=1) 
View(df4)

plotdata_T1 <- df4 %>%
  group_by(Visit, CVD) %>%
  summarize(n = n()) %>% 
  mutate(pct = n/sum(n),
         lbl = scales::percent(pct),
         treat=1)

View(plotdata_T1)


df5<-df %>% filter(TRT==2) 
View(df5)

plotdata_T2<- df5 %>%
  group_by(Visit, CVD) %>%
  summarize(n = n()) %>% 
  mutate(pct = n/sum(n),
         lbl = scales::percent(pct),
         treat=2)

View(plotdata_T2)

combined <- rbind(plotdata_T1, plotdata_T2)
View(combined)

df6<-combined %>% group_by(Visit) %>% summarize(nsubjects = sum(n))
View(df6)

(combined %>% group_by(treat,Visit, CVD)
  %>%  
    ggplot(aes(x=factor(Visit,
                levels=c(00, 01, 02, 03, 04),
                labels=c("BL", "6m", "12m", "18m", "24m")),
     y=pct, 
      fill=factor(CVD, 
                levels = c("0", "1"),
                 labels = c("No Disease", "Disease")))) 

  + geom_bar(position="fill", stat="identity")+facet_wrap(~treat)
  +geom_text(aes(label= lbl),position = position_fill(vjust = 0.5))
  +scale_y_continuous(labels = scales::percent_format(accuracy = 1))
  +labs(fill="Disease Status")
    +xlab("Visit Number")
  +ylab("Prevalence")
)
R. Simian
  • 187
  • 1
  • 10
  • 3
    For the label you could use an `ifelse`, i.e. try something like `geom_text(aes(label = ifelse(CVD == 1, lbl, "")), ... )` – stefan Jul 14 '22 at 14:35
  • 1
    Ad 2: https://stackoverflow.com/questions/2551921/show-frequencies-along-with-barplot-in-ggplot2 – harre Jul 14 '22 at 14:37
  • 1
    Ad 1: https://stackoverflow.com/questions/39682307/show-only-one-text-value-in-ggplot2 – harre Jul 14 '22 at 14:39
  • Thank you, stefan! the ifelse statement did the trick in terms of removing the label. – R. Simian Jul 15 '22 at 22:10
  • Harre, thank you for the links, I had come across them earlier but unfortunately they haven't helped me figure out how to add the N=XX for sample size under each visit. – R. Simian Jul 15 '22 at 22:12

0 Answers0