1

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): enter image description here

Nancy
  • 3,989
  • 5
  • 31
  • 49
  • 1
    Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Axeman Jan 23 '17 at 14:40
  • Hi, sorry about that. I had been stuck on this for quite a while and searched a lot of threads and tried whatever I could find but nothing worked and now I am exhausted and frustrated. I know this isn't acceptable and am sorry for that. – sushant mehrotra Jan 23 '17 at 14:49
  • 1
    It's ok, you can edit the question. It's also quite important to clarify that you are using `plotly`, not just `ggplot`. – Axeman Jan 23 '17 at 14:52
  • Was there a feature that you wanted from ggplotly, or would it suit your needs to just use ggplot? – Nancy Jan 23 '17 at 16:41
  • just ggplot would also be fine – sushant mehrotra Jan 24 '17 at 01:57

1 Answers1

0

So it turns out that my problem can be sorted out using "width" parameter from plotly. I tried increasing the width and luckily the legend kept shifting to the right.

ggplotly( create_bar_graph(data,"disease", "product"),width = 1000)

Thanks to all who spend their valuable time having a look at my problem. I guess I posted it before trying enough. Sorry for the trouble guys.