I have the dataset and code below which produced the chart attached in image. My questions are:
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.
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?
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")
)