I am trying to plot some data and see below the replicable example, starting from the relevant libraries
library(ggplot2)
library(tidyr)
library(scales)
library(dplyr)
and the creation of the random dataset see below:
data <- data.frame(replicate(3, sample(0:100, 100, rep=TRUE)))
data$Place <- sample(c("PlaceA", "PlaceB","PlaceC"), size = nrow(data), prob = c(0.76, 0.14, 0.10), replace = TRUE)
data$Preference <- sample(c("Strong", "Medium","Low"), size = nrow(data), replace = TRUE)
data$Risk <- sample(c("Yes","No"), size = nrow(data), replace = TRUE)
colnames(data) <- c("A","B","C","Place","Preference","Risk")
rownames(data) <- NULL
After this step I am trying to get the data along a different shape by using tidyr package
data_long <- tidyr::gather(data, key = type_col, value = categories, -c("A","B","C","Place","Preference"))
And then I wish to plot the proportions of respondents saying yes to risk by place- see below the code to achieve the visual output
data_long %>%
count(type_col, categories,Place) %>%
left_join(data_long %>% count(type_col, Place, name = "m"),by = c("type_col", "Place")) %>%
mutate(Prop = n/m) %>%
ggplot(aes(x = categories, y = Prop, fill = Place)) +
geom_col(position = position_dodge()) +
geom_text(aes(label = scales::percent(Prop)),
hjust = 0.1,
position = position_dodge(1)) +
facet_wrap(~ type_col, scales = "free_x", ncol = 3) +
scale_fill_brewer(palette = "Oranges") + #scale_x_discrete(limits = positions)+
scale_y_continuous(limits = c(0, 1), labels = scales::percent) +
xlab("") +
ylab("") +
coord_flip() +
theme(panel.background = element_rect(fill = "white"),
legend.position = "bottom",
strip.text.x = element_text(size = 15, colour = "black"),
plot.title = element_text(size = 20, face = "bold"),
axis.text = element_text(size = 12),
axis.title = element_text(size = 12))
See below the output which is correct. Yet, I do not want to show the yes and nos, but just the yes proportions. Is there an easy way to just plot the output below while retaining just one option of the facets (Yes in this case)? Thanks for the help
Maybe this:
library(tidyverse)
#Code
data_long %>%
count(type_col, categories,Place) %>%
left_join(data_long %>% count(type_col, Place, name = "m"),by = c("type_col", "Place")) %>%
mutate(Prop = n/m) %>%
filter(categories=='Yes') %>%
mutate(Place=factor(Place,levels = rev(unique(Place)),ordered = T)) %>%
ggplot(aes(x = categories, y = Prop, fill = Place)) +
geom_col(position = position_dodge()) +
geom_text(aes(label = scales::percent(Prop)),
hjust = 0.1,
position = position_dodge(1)) +
facet_wrap(~ type_col, scales = "free_x", ncol = 3) +
scale_fill_brewer(palette = "Oranges",guide = guide_legend(reverse = TRUE)) + #scale_x_discrete(limits = positions)+
scale_y_continuous(limits = c(0, 1), labels = scales::percent) +
xlab("") +
ylab("") +
coord_flip() +
theme(panel.background = element_rect(fill = "white"),
legend.position = "bottom",
strip.text.x = element_text(size = 15, colour = "black"),
plot.title = element_text(size = 20, face = "bold"),
axis.text = element_text(size = 12),
axis.title = element_text(size = 12))
Output: