TThe challenge is suggested by Storytelling with Data (SWD) community. https://community.storytellingwithdata.com/exercises/optimize-for-audience?page=2
library(ggtext)
## Warning: package 'ggtext' was built under R version 4.0.5
library(ggplot2)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v tibble 3.0.5 v dplyr 1.0.3
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.0
## v purrr 0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
df <- read_csv("swd_july.csv")
##
## -- Column specification --------------------------------------------------------
## cols(
## brand = col_character(),
## reason = col_character(),
## number = col_double()
## )
df
## # A tibble: 8 x 3
## brand reason number
## <chr> <chr> <dbl>
## 1 Serentiva Lack of mood swings 34
## 2 Serentiva Anxiety relief 29
## 3 Serentiva Prefer mood swings 2
## 4 Serentiva Other 5
## 5 Competitor Lack of mood swings 2
## 6 Competitor Anxiety relief 8
## 7 Competitor Prefer mood swings 8
## 8 Competitor Other 1
df$brand <- factor(df$brand, levels = c("Competitor","Serentiva"))
df$reason <- factor(df$reason, levels = c( "Other","Prefer mood swings", "Anxiety relief","Lack of mood swings"))
str(df)
## tibble [8 x 3] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ brand : Factor w/ 2 levels "Competitor","Serentiva": 2 2 2 2 1 1 1 1
## $ reason: Factor w/ 4 levels "Other","Prefer mood swings",..: 4 3 2 1 4 3 2 1
## $ number: num [1:8] 34 29 2 5 2 8 8 1
## - attr(*, "spec")=
## .. cols(
## .. brand = col_character(),
## .. reason = col_character(),
## .. number = col_double()
## .. )
ggplot(data = df, aes (x = reason, y = number,fill = brand)) +
geom_bar(stat = "identity", position = 'dodge', )+
geom_text(aes(label= number),
position=position_dodge(width=0.9),
color= "white",
vjust=0.25, hjust =1.25)+
scale_fill_manual(values = c("#C55A11","#203864")) +
labs(title = "Lack of smooth swing and Anxiety relief <br>are the main reasons for SERENTIVA preference <br>",
subtitle= "**Reasons for Preference**") +
scale_y_continuous(expand = c(0, 0),name = "Number of Patients",
limits = c(0, 40),
breaks = seq(0, 40, by= 5),
position = 'right') +
coord_flip() +
theme(plot.title = element_markdown(size=16),
plot.title.position = "plot",
plot.subtitle = element_markdown(size=14, face="bold"),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_text(color ="#777B7E", face="bold", size = 12),
axis.title.x = element_markdown(hjust = 0,size = 12, ),
axis.text.x = element_text(color ="#777B7E", face="bold", size = 12),
axis.line.x = element_line(color="grey", size = 1),
axis.ticks.x = element_line(color="#a9a9a9"),
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())
df_box <- tibble(
label = c("<span style='font-size:60pt;color:#203864'>**70.7%**</span>
<span style='font-size:20pt;color:#7F7F7F'>(n = 70)</span><br/>
<span style='font-size:20pt;color:#7F7F7F'>prefer</span>
<span style='font-size:20pt;color:#203864'>**Serentiva**</span>
<span style='font-size:20pt;color:#7F7F7F'>mainly due to
<br> **lack of mood swings** and
<br/>**better anxiety relief**</span>"),
x = 0.01,
y = 0.5,
color = c("white"),
fill = c("white"))
ggplot()+
geom_textbox(data = df_box,
aes(x , y, label = label),
box.color = "white",
fill = "white", lineheight = 2,
width = unit(400, "pt"),
hjust = 0,
box.padding = unit(c(0, 0, 0, 0), "pt")) + # control the width
xlim(0, 1) +
ylim(0, 1) +
theme(axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.title = element_blank(),
axis.line = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
plot.margin = unit(c(0,0,0,0),"cm"))
df_box2 <- tibble(
label = c("<span style='font-size:60pt;color:#C55A11'>**19.2%**</span>
<span style='font-size:20pt;color:#7F7F7F'>(n = 19)</span><br/>
<span style='font-size:20pt;color:#7F7F7F'>prefer</span>
<span style='font-size:20pt;color:#C55A11'>the **Competitor**</span> <span style='font-size:20pt;color:#7F7F7F'>mainly due to <br> **preference for mood swings** and <br/>**better anxiety relief**</span>"),
x = 0.01,
y = 0.5,
color = c("white"),
fill = c("white"))
ggplot()+
geom_textbox(data = df_box2,
aes(x , y, label = label),
box.color = "white",
fill = "white", lineheight = 2,
width = unit(400, "pt"),
hjust = 0,
box.padding = unit(c(0, 0, 0, 0), "pt")) + # control the width
xlim(0, 1) +
ylim(0, 1) +
theme(axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.title = element_blank(),
axis.line = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
plot.margin = unit(c(0,0,0,0),"cm"))
df_box1 <- tibble(
label = c("<span style='font-size:40pt;color:#203864'>**70.7%**</span>
<span style='font-size:12pt;color:#7F7F7F'>(n = 70)</span><br/>
<span style='font-size:12pt;color:#7F7F7F'>favored</span>
<span style='font-size:16pt;color:#203864'>**Serentiva**</span>
<span style='font-size:12pt;color:#7F7F7F'>mainly due to <br>
<span style='font-size:12pt;color:#203864'>**lack of mood swings**</span><br>
<span style='font-size:12pt;color:#7F7F7F'>and</span>
<span style='font-size:12pt;color:#203864'>**better anxiety relief**</span>"),
x = 0.01, y = 0.5,color = c("white"),fill = c("white"))
df_box2 <- tibble(
label = c("<span style='font-size:40pt;color:#C55A11'>**19.2%**</span>
<span style='font-size:12pt;color:#7F7F7F'>(n = 19)</span><br/>
<span style='font-size:12pt;color:#7F7F7F'>chose the</span>
<span style='font-size:16pt;color:#C55A11'>**Competitor**</span>
<span style='font-size:12pt;color:#7F7F7F'>mainly due to <br>
<span style='font-size:12pt;color:#C55A11'>**preference for mood swings**</span>
<span style='font-size:12pt;color:#7F7F7F'>and</span>
<span style='font-size:12pt;color:#C55A11'>**better anxiety relief**</span> "),
x = 0.01, y = 0.5,color = c("white"),fill = c("white"))
df_box3 <- tibble(
label = c("<span style='font-size:40pt;color:#7F7F7F'>**10.1%**</span>
<span style='font-size:12pt;color:#7F7F7F'>(n = 10) </span><br/>
<span style='font-size:12pt;color:#7F7F7F'>had no preference</span><br/>
"),
x = 0.01, y = 0.5,color = c("white"),fill = c("white"))
theme_box <- theme(axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.title = element_blank(),
axis.line = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
plot.margin = unit(c(0,0,0,0),"cm"))
p1 <- ggplot()+
geom_textbox(data = df_box1,
aes(x , y, label = label),
box.color = "white",
fill = "white", lineheight = 1.5,
width = unit(500, "pt"),
hjust = 0,
box.padding = unit(c(0, 0, 0, 0), "pt")) + # control the width
xlim(0, 1) + ylim(0, 1) +
theme_box
p2 <- ggplot()+
geom_textbox(data = df_box2,
aes(x , y, label = label),
box.color = "white",
fill = "white", lineheight = 1.5,
width = unit(500, "pt"),
hjust = 0,
box.padding = unit(c(0, 0, 0, 0), "pt")) + # control the width
xlim(0, 1) + ylim(0, 1) +
theme_box
p3 <- ggplot()+
geom_textbox(data = df_box3,
aes(x , y, label = label),
box.color = "white",
fill = "white", lineheight = 1.5,
width = unit(400, "pt"),
hjust = 0,
box.padding = unit(c(0, 0, 0, 0), "pt")) + # control the width
xlim(0, 1) + ylim(0, 1) +
theme_box
p4 <- ggplot(data = df, aes (x = reason, y = number,fill = brand)) +
geom_bar(stat = "identity", position = 'dodge', )+
geom_text(aes(label= number),
position=position_dodge(width=0.9),
color= "white",
vjust=0.25, hjust =1.25)+
scale_fill_manual(values = c("#203864","#C55A11"),
breaks = c("Serentiva", "Competitor"),
labels=c("Prefer Serentiva", "Prefer Competitor")) +
labs(title = "Participants chose <span style='font-size:16pt;color:#203864'>**Serentiva**</span> mainly due to
Lack of smooth swings and Anxiety relief",
subtitle= "Reasons for Preference") +
scale_y_continuous(expand = c(0, 0),name = "Number of participants (N=99)",
breaks = seq(0, 40, by= 5),
position = 'right') +
coord_flip() +
theme(plot.title = element_markdown(size=16),
plot.title.position = "plot",
plot.subtitle = element_markdown(size=12),
axis.title.y = element_blank(),
# axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_text(color ="#777B7E", face="bold", size = 12),
axis.title.x = element_markdown(hjust = 0,size = 12, color ="#777B7E",face="bold"),
axis.text.x = element_text(color ="#777B7E", face="bold", size = 12),
axis.line.x = element_line(color="grey", size = 1),
axis.ticks.x = element_line(color="#a9a9a9"),
legend.title = element_blank(),
#legend.text = element_text(color= c("#203864","#C55A11"), size=12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())
library(ggpubr)
## Warning: package 'ggpubr' was built under R version 4.0.5
library(cowplot)
## Warning: package 'cowplot' was built under R version 4.0.5
##
## Attaching package: 'cowplot'
## The following object is masked from 'package:ggpubr':
##
## get_legend
plot_grid(ggarrange(p1,p2,p3, ncol = 1, nrow = 3), p4, rel_widths = c(0.9, 1.8))
plot_grid(plot_grid(p1,p2,p3, ncol = 3, nrow = 1, align = "h" ), p4, nrow = 2, rel_heights = c(0.7, 2.1))
ggsave("Solution1.png", height = 7, width = 11,units = "in",dpi = 300)
theme_graph <- theme(plot.title = element_markdown(size=16),
plot.title.position = "plot",
plot.subtitle = element_markdown(size=14, face="bold"),
axis.title.y = element_blank(), # weird, why y here ?
# axis.text.y = element_blank(), # to show survey items
axis.ticks.y = element_blank(),
axis.text.y = element_text(color ="#777B7E", face="bold", size = 12),
axis.title.x = element_markdown(hjust = 0,size = 12, ),
axis.text.x = element_text(color ="#777B7E", face="bold", size = 12),
axis.line.x = element_line(color="grey", size = 1),
axis.ticks.x = element_line(color="#a9a9a9"),
legend.title = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())
ggplot(data = df %>%
filter(brand == "Competitor"),
aes(x = reason, y = number)) +
geom_bar(stat = "identity", fill = "#C55A11", width = 0.65)+
geom_text(aes(label= number),
position=position_dodge(width=0.9),
color= "white", size= 5,
vjust=0.25, hjust =1.25)+
scale_y_continuous(expand = c(0, 0),
name = "Number of participants",
breaks = seq(0, 40, by= 5),
position = 'right') +
coord_flip() +
theme_graph
ggplot(data = df %>%
filter(brand == "Serentiva"),
aes(x = reason, y = number)) +
geom_bar(stat = "identity", fill = "#203864", width = 0.65)+
geom_text(aes(label= number),
position=position_dodge(width=1),
color= "white", size= 5,
vjust=0, hjust =0) +
scale_y_continuous(expand = c(0, 0),
name = "Number of participants",
limits = c(40, 0),
breaks = seq(0, 40, by= 5),
position = 'right',
trans = 'reverse') +
scale_x_discrete( position = 'top') +
coord_flip() +
theme_graph
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
library(grid)
theme_graph2 <- theme(axis.title.y = element_blank(), # weird, why y here ?
#axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(color ="#777B7E", face="bold", size = 12),
#axis.title.x = element_blank(),
axis.text.y = element_blank(),
axis.line.x = element_line(color="grey", size = 1),
# axis.ticks.x = element_line(color="#a9a9a9"),
axis.title.x = element_markdown(hjust = 0,size = 12, color ="#777B7E",face="bold"),
legend.title = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())
g1 <- ggplot(data = df %>%
filter(brand == "Competitor") %>%
mutate(pct = prop.table(number)),
aes(x = reason, y = number)) +
geom_bar(stat = "identity", fill = "#C55A11" , width = 0.65)+
geom_text(aes(label= paste("n = ",number ,"\n",round(pct*100,digits= 1),"%", sep="")),
position=position_dodge(width=0.9),
color= "#C55A11", size= 4.5,
vjust=0.5, hjust =-0.15)+
scale_y_continuous(expand = c(0, 0),
limits = c(0, 40),
name = "Number of participants",
breaks = seq(0, 40, by= 10)) + # ,position = 'right'
theme_graph2 + coord_flip()
g2 <- ggplot(data = df %>%
filter(brand == "Serentiva") %>%
mutate(pct = prop.table(number)),
aes(x = reason, y = number)) +
geom_bar(stat = "identity", fill = "#203864", width = 0.65)+
geom_text(aes(label= paste("n = ",number,"\n",round(pct*100,digits= 1),"%", sep="")),
position=position_dodge(width=0.9),
color= "#203864", size= 4.5,
vjust=0.5, hjust =1.25)+
scale_y_continuous(expand = c(0, 0),
name = "Number of participants",
limits = c(40, 0),
breaks = seq(0, 40, by= 10),
# position = 'right',
trans = 'reverse') +
scale_x_discrete(position = 'top') +
theme_graph2 + coord_flip()
mid <- ggplot(df,aes(x=1,y=reason))+
geom_text(aes(label=reason),size = 5, color ="#777B7E" ,fontface="bold")+
labs(title= "Reason for preference")+
scale_x_continuous(expand=c(0,0),
limits=c(1,1))+
theme(plot.title = element_text(hjust=0.5),
axis.title.y = element_blank(),
axis.text= element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
#axis.text.y = element_text(color ="#777B7E", face="bold", size = 12),
# axis.ticks.x = element_blank(),
# axis.line.x = element_line(color="grey", size = 1),
panel.background=element_blank(),
panel.grid=element_blank())
ggplot(df,aes(x=1,y=reason))+
geom_text(aes(label=reason),size = 5, color ="#777B7E" ,fontface="bold")+
# labs(x= "Reason for preference")+
scale_x_continuous(expand=c(0,0),
limits=c(1,1))+
theme(#plot.title = element_text(hjust=0.5),
axis.title.y = element_blank(),
axis.text= element_blank(),
axis.ticks = element_blank(),
#axis.title.x = element_blank(),
#axis.text.y = element_text(color ="#777B7E", face="bold", size = 12),
# axis.ticks.x = element_blank(),
panel.background=element_blank(),
panel.grid=element_blank(),
plot.margin = unit(c(0,0,0,0), "mm"))
ggarrange(g2,g1, ncol = 2, nrow = 1)
gg.mid <- ggplot_gtable(ggplot_build(mid))
grid.arrange(ggplot_gtable(ggplot_build(g2)),
ggplot_gtable(ggplot_build(mid)),
ggplot_gtable(ggplot_build(g1)),
ncol=3,widths=c(3.5/9,2/9,3.5/9))
mid2 <- ggplot(df,aes(x=1,y=reason))+
geom_text(aes(label=reason),hjust =1)+
ggtitle("Reasons for preference")+
ylab("NULL")+
scale_x_continuous(expand=c(0,0),
limits=c(1,1))+
theme(axis.title=element_blank(),
panel.grid=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.background=element_blank(),
axis.text.x=element_text(color=NA),
axis.ticks.x=element_line(color=NA),
plot.margin = unit(c(0,0,0,0), "mm"))
plot_grid(mid2,g2,g1, ncol = 3, nrow = 1, rel_widths = c(1.3, 1.8,1.8))
plot_grid(
plot_grid(p1,g2, ncol = 1, nrow = 2,rel_heights = c(0.7, 2.)),
plot_grid(p3, mid, ncol = 1, nrow = 2,rel_heights = c(0.7, 2)),
plot_grid(p2,g1, ncol = 1, nrow = 2,rel_heights = c(0.7, 2.)),
ncol =3, rel_widths = c(1, 0.6,1))
plot_grid(
plot_grid(p1,g2, ncol = 1, nrow = 2,rel_heights = c(0.5, 1.5)),
plot_grid(ggplot() + theme_void(),mid, ncol = 1, nrow = 2,rel_heights = c(0.5, 1.5)),
plot_grid(p2,g1, ncol = 1, nrow = 2,rel_heights = c(0.5, 1.5)),
ncol =3, rel_widths = c(1, 0.5,1))
plot_grid(
plot_grid(p1,p3,p2, ncol = 3, nrow = 1,rel_widths = c(1, 0.5,1)),
plot_grid(g2, mid, g1, ncol = 3, nrow = 1,rel_widths= c(1, 0.5,1)),
nrow =2, rel_heights = c(0.7,1))
plot_grid(
plot_grid(p1,g2, ncol = 1, nrow = 2,rel_heights = c(0.5, 1.5)),
plot_grid(ggplot() +theme_void(),mid,ggplot() +theme_void(), ncol = 1, nrow = 3, rel_heights = c(0.4, 1.5,0.15)),
plot_grid(p2,g1, ncol = 1, nrow = 2,rel_heights = c(0.5, 1.5)),
ncol = 3, rel_widths = c(1, 0.48,1))
ggsave("Solution2.png", height = 6, width = 12,units = "in",dpi = 300)