无法在ggplot中重新排列传奇

时间:2017-01-23 14:23:50

标签: r ggplot2 r-markdown

我正在使用rmarkdown编写代码并将输出显示为html文档。我有一个数据框,我想从中创建一个条形图。现在图例太长,所以它重叠并覆盖了图形的一部分。我尝试过很多东西,但无法对齐标签方向,图例方向。我只能使用(legend.position =“none”)删除图例。我甚至无法使用“legend.position”向左,向右,向下或向上移动图例。有没有办法减少标签的字体或垂直对齐? 基本上可以使传奇变小或将其移动到可能有更多空间的地方以避免任何重叠......

有人可以帮帮我吗?

以下是代码:

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"))

我的输出是这样的(上面代码的堆栈高度可能与图像中的高度不同,但其他一切都相同): enter image description here

1 个答案:

答案 0 :(得分:0)

事实证明我的问题可以使用plotly中的“width”参数进行整理。我试着增加宽度,幸运的是传说一直向右移动。

pln

感谢所有花费宝贵时间看看我的问题的人。我想我在尝试之前发布了它。对不起,麻烦的家伙们。