希望在我的由 ggplot 創建的箱形圖中標記單個框。正如這篇文章中的答案所建議的那樣[在 ggplot boxplot 中標記單個框}(https://stackoverflow.com/questions/48029549/labeling-individual-boxes-in-a-ggplot-boxplot)我正在使用 stat_summary 函式這樣做。
論壇帖子中的示例代碼:
ggplot(mtcars, aes(factor(cyl), mpg))
geom_boxplot()
stat_summary(geom = 'text', label = letters[1:3], fun.y = max, vjust = -1)
但是,當我嘗試為自己的情節重新創建此標簽時,標簽出現故障。每個盒子的標簽沒有被標記為“a”、“b”、“c”等。從左到右,它們出現在圖上,但出現了“a”、“b”、“c”標簽按軸標簽的字母順序。有什么簡單的方法可以糾正這個問題嗎?
我的代碼示例:
maxn_topo_plot<- ggplot(wide.df2, aes(x=topo, y=sum_maxn))
geom_boxplot(show.legend = FALSE) theme_pubclean(base_size = 18)
xlab("Island Geomorpholgy") ylab("Shark MaxN")
scale_x_discrete(limits=c("open atoll","closed atoll","near atoll",
"high barrier", "high rocky", "high fringing "))
theme(axis.text.x = element_text(angle = 12))
stat_summary(geom = 'text',
label = c("a","b","c","d", "e", "g"),
fun = max, vjust = -1, size= 6)
maxn_topo_plot
uj5u.com熱心網友回復:
發生這種情況是因為 label() 尊重變數“topo”的因子水平。請注意, abc .. 的順序是按照字母順序排列的。
當您使用 scale_x_discrete 更改 X 軸上的順序時,您不會更改因子水平順序。
就像我在你的另一個問題中所建議的那樣。更改 X 的因子水平,而不是更改 scale_x_discrete。
maxn_topo_plot<- ggplot(wide.df2, aes(x= factor(topo,
levels = c("open atoll",
"closed atoll",
"near atoll",
"high barrier",
"high rocky",
"high fringing")), y=sum_maxn))
geom_boxplot(show.legend = FALSE)
xlab("Island Geomorpholgy")
ylab("Shark MaxN")
theme_pubclean(base_size = 20)
theme(axis.text.x = element_text(angle = 12))
stat_summary(geom = 'text',
label = c("a","b","c","d", "e", "g"),
fun = max, vjust = -1, size= 6)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/476656.html