I am writing a code using rmarkdown and displaying the output as an html document. I have a data frame from which I want to create a bar graph. Now the legend is too long so it overlaps and covers part of the graph. I have tried a lot of things but am unable to align the label direction, legend direction. I am only able to remove the legend using (legend.position = "none"). I cannot even move the legend left, right, bottom or top using "legend.position". Is there a way to either decrease the font of labels or align them vertically? Basically something to make the legend smaller or move it somewhere where there might be more space to avoid any sort of overlapping...
Can someone please help me?
Below is the code:
create_bar_graph <- function(ggdata, ggaxis, ggfac) {
p <- ggplot(ggdata %>% filter_(!is.na(ggaxis)),
aes_string(x = ggaxis, fill = ggaxis)) +
geom_bar() +
facet_wrap(ggfac) +
labs(
# title = "Insurance",
y = "Nb of respondent") +
# coord_flip() +
scale_fill_brewer(palette = "PuRd") +
guides(fill = guide_legend(title = NULL)) +
theme(axis.text.x =
element_blank(),legend.position = "none")
return(p)
}
caption_func <- function(text){
htmltools::tags$caption(style = 'caption-side : top;
text-align = center',
htmltools::h3(text))
}
data = data.frame(
product = rep(c("A","B"),each = 6),
disease = rep(c("a) Partial onset seizures",
"b) Partial onset seizures with secondary generalization",
"c) Primary generalized tonic-clonic seizures (PGTCS)",
"d) I don't know the type of Epilepsy I have")),3)
ggplotly( create_bar_graph(data,"disease", "product"))
The output I have is something like this (the stack height from the code above may differ from the height in image but everything else is same):