我很難理解為什么legend.horizontal不旋轉我的圖例軸所以它不垂直顯示?任何幫助將不勝感激。
library(phyloseq)
library(ggplot2)
##phylum level
ps_tmp <- get_top_taxa(physeq_obj = ps.phyl, n = 10, relative = TRUE, discard_other = FALSE, other_label = "Other")
ps_tmp <- name_taxa(ps_tmp, label = "Unkown", species = T, other_label = "Other")
phyl <- fantaxtic_bar(ps_tmp, color_by = "phylum", label_by = "phylum",facet_by = "TREATMENT", other_label = "Other", order_alg = "as.is")
phyl theme(legend.direction = "horizontal", legend.position = "bottom", )

uj5u.com熱心網友回復:
離散值的圖例本身沒有正式的方向,但ggplot2其位置決定了它最適合您的資料。這就是為什么像這樣的事情legend.direction在這里不起作用。我沒有phyloseq包或訪問您的特定資料,所以我將向您展示它是如何作業的,以及如何使用可重現的示例資料集來弄亂圖例。
library(ggplot2)
set.seed(8675309)
df <- data.frame(x=LETTERS[1:8], y=sample(1:100, 8))
p <- ggplot(df, aes(x, y, fill=x)) geom_col()
p

默認情況下,ggplot 將我們的圖例放在右側并將其垂直組織為一列。當我們將圖例移動到底部時,會發生以下情況:
p theme(legend.position="bottom")

Now ggplot thinks it's best to put that legend into 4 columns, 2 rows each. As u/Tech Commodities mentioned, you can use the guides() functions to specify how the legend looks. In this case, we will specify to have 2 columns instead of 4. We only need to supply the number of columns (or rows), and ggplot figures out the rest.
p theme(legend.position="bottom")
guides(fill=guide_legend(ncol=2))

So, to get a "horizontally-arranged" legend, you just need to specify that there should be only one row:
p theme(legend.position="bottom")
guides(fill=guide_legend(nrow=1))

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/317981.html
下一篇:ShinyR:無法顯示情節
