首页 文章

无法在ggplot中重新排列传奇

提问于
浏览
1

我正在使用rmarkdown编写代码并将输出显示为html文档 . 我有一个数据框,我想从中创建一个条形图 . 现在图例太长,所以它重叠并覆盖了图形的一部分 . 我尝试了很多东西,但无法对齐标签方向,图例方向 . 我只能使用(legend.position = "none")删除图例 . 我甚至无法使用"legend.position"向左,向右,向下或向上移动图例 . 有没有办法减少标签的字体或垂直对齐? Basically something to make the legend smaller or move it somewhere where there might be more space to avoid any sort of overlapping...

有人可以帮帮我吗?

以下是代码:

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

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

1 回答

  • 0

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

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

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

相关问题