[英]Extending plot window beyond x-axis labels in ggplot
在ggplot
,我有一個圖表,其 x 軸標簽超出了 RStudio 中的繪圖窗口,即使我嘗試導出圖片並且無論我制作的圖片有多寬。 以下是我當前使用scale_x_continuous
中的limits
的解決方案。 是否可以擴展圖片以便我可以捕獲我的最后一個 x 軸標簽(即 25021643)但不擴展線段?
重現上面的代碼:
library(ggplot2)
p <-
ggplot(NULL) +
xlab("x-axis") +
theme_bw() +
scale_x_continuous(breaks = as.integer(seq(0,25021643,(25021643/4))), limits=c(0,26021643),labels = as.integer(seq(0,25021643,(25021643/4))), expand = c(0,0)) +
scale_y_continuous(limits = c(-1, (nrow(chr5)+1)), expand = c(0,0)) +
geom_hline(yintercept = -1) +
geom_segment(aes(x = 0, y = -1, xend = 0, yend = -0.9)) +
geom_segment(aes(x = 25021643, y = -1, xend = 25021643, yend = -0.9)) +
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.border=element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_line(size=1),
axis.title.x = element_text(size=20),
axis.text.x = element_text(size=20))
p
添加plot.margin=unit(c(0,20,0,0),"mm")
似乎可以完成這項工作。
您也可以使用:
plot.margin = margin(r = 20, unit = "mm")
只是做同樣事情的另一種方法,但避免為繪圖的所有邊寫值:'t' 是頂部,'r' 是右側,'l' 是左側,'b' 是底部。 您可以寫下您希望用逗號分隔的所有或任何方面:
plot.margin = margin(r = 20, l = 30, b = 15, t = 40, unit = "mm")
或者,如果您想使用標准單位('pt'):
plot.margin = margin(r = 20)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.