簡體   English   中英

ggplot中的自定義條形距離

[英]custom bar distances in ggplot

使用 ggplot,您可以通過修改width來更改條形圖的條形寬度

geom_bar(stat="identity",position=position_dodge(), width = .9)

您可以使用position_dodge()統一更改條形的距離:

geom_bar(stat="identity", position=position_dodge(1) ,width = .9)

如何自定義條形之間的距離,使它們以不均勻的方式變化?

不清楚你的意思。 我假設您的意思是您有一個離散的 x 軸變量,並且您希望在每個條形之間指定自定義間距。 可以使用position_jitter來獲得隨機間距,盡管這也會影響條寬,我猜這不是你想要的。

我可能會通過使用數字 x 比例並用我的因子水平重新標記軸來處理這個問題:

library(ggplot2)

ggplot(data = data.frame(x = 1:10 + rep(c(0.1, -0.1), 5), y = sample(11:20))) + 
  geom_bar(aes(x, y, fill = factor(x)), color = "black", stat = "identity") +
  scale_x_continuous(breaks = 1:10 + rep(c(0.1, -0.1), 5), 
                     labels = LETTERS[1:10]) +
  guides(fill = guide_none())

在此處輸入圖片說明

當然,我們只能猜測你真正想要什么,因為你沒有提供一個激勵性的例子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM