I would like to improve the aesthetics of my code output:
- add dots to both lines then have the subsequent percentage value above each dot (app_1 and app_2)
- have the value associated with each blue bar in the middle of the bar (Total_Wth)
I tried geom_point but it removed my right hand side Y axis and added in a second key.
x <- c("Mar_22", "Apr_22", "May_22", "June_22", "July_22", "Aug_22")
Total_Wth <- c(88959, 100396, 117989, 130561, 150282, 148970)
App_1 <- c(0.91, 0.9, 0.91, 0.88, 0.9, 0.9)
App_2 <- c(0.09, 0.1, 0.09, 0.12, 0.10, 0.10)
df <- data.frame(x, Total_Wth, App_1, App_2)
transf_fact <- max(df$Total_Wth)/max(df$App_1)
head(df)
df %>%
mutate_at(vars(matches("App")), ~.*transf_fact) %>%
gather(y_var, val, -x) %>%
ggplot(aes(x = factor(x, df$x), y = val, fill = y_var))
geom_col(data = . %>% filter(y_var == "Total_Wth"))
geom_line(data = . %>% filter(y_var != "Total_Wth"),
aes(color = y_var, group = y_var),
size = 1.2)
scale_fill_discrete(aesthetics = c("colour", "fill"))
scale_y_continuous(sec.axis = sec_axis(trans = ~ . / transf_fact,
name = "%",
labels = scales::percent),
limits = c(0, 170000),
breaks = c(34000 * 0:5))
labs(x = "Month", y = "Total",
title = "Example Heading",
subtitle = "Example subheading")
theme_bw()
theme(legend.position = "bottom",
legend.title = element_blank(),
legend.key.size = unit(0.5, 'cm'),
legend.text = element_text(size=7),
panel.grid.major.x = element_blank(),
panel.border = element_blank(),
plot.title = element_text(hjust = 0),
plot.subtitle = element_text(size=8, hjust=0, face="italic", color="black"),
axis.text.x = element_text(size = 10, face = "bold"))
CodePudding user response:
It sounds like you are looking for something like this:
df %>%
mutate_at(vars(matches("App")), ~.*transf_fact) %>%
gather(y_var, val, -x) %>%
ggplot(aes(x = factor(x, df$x), y = val, fill = y_var))
geom_col(data = . %>% filter(y_var == "Total_Wth"))
geom_line(data = . %>% filter(y_var != "Total_Wth"),
aes(color = y_var, group = y_var), size = 1.2)
geom_point(data = . %>% filter(y_var != "Total_Wth"))
geom_text(data = . %>% filter(y_var != "Total_Wth"),
aes(label = scales::percent(val / transf_fact, 0.1)), nudge_y = 5e3)
geom_text(data = . %>% filter(y_var == "Total_Wth"),
aes(label = val), position = position_stack(vjust = 0.5))
scale_fill_discrete(aesthetics = c("colour", "fill"))
scale_y_continuous(sec.axis = sec_axis(trans = ~ . / transf_fact,
name = "%",
labels = scales::percent),
limits = c(0, 170000),
breaks = c(34000 * 0:5))
labs(x = "Month", y = "Total",
title = "Example Heading",
subtitle = "Example subheading")
guides(fill = guide_legend(override.aes = list(color = NA)))
theme_bw()
theme(legend.position = "bottom",
legend.title = element_blank(),
legend.key.size = unit(0.5, 'cm'),
legend.text = element_text(size=7),
panel.grid.major.x = element_blank(),
panel.border = element_blank(),
plot.title = element_text(hjust = 0),
plot.subtitle = element_text(size=8, hjust=0, face=3, color="black"),
axis.text.x = element_text(size = 10, face = "bold"))