[英]How to remove ggplot margin in R
我想從 ggplot 中刪除邊距,但再次出現圖像中的白色邊框。
我已經嘗試了各種繪圖邊距選項,但邊距仍然存在。
data <- structure(list(timestamp = structure(c(1598950800, 1598950800,
1598950800, 1598951100, 1598951100, 1598951100, 1598951400, 1598951400,
1598951400, 1598951700, 1598951700, 1598951700, 1598952000, 1598952000,
1598952000, 1598952300, 1598952300, 1598952300, 1598952600, 1598952600,
1598952600, 1598952900, 1598952900, 1598952900, 1598953200, 1598953200,
1598953200, 1598953500, 1598953500, 1598953500, 1598953800, 1598953800,
1598953800, 1598954100, 1598954100, 1598954100, 1598954400, 1598954400,
1598954400), tzone = "UTC", class = c("POSIXct", "POSIXt")),
Road_segment = c("L1", "L2", "L3", "L1",
"L2", "L3", "L1", "L3", "L2",
"L2", "L3", "L1", "L1", "L3",
"L2", "L2", "L3", "L1", "L1",
"L3", "L2", "L1", "L2", "L3",
"L3", "L2", "L1", "L2", "L3",
"L1", "L1", "L2", "L3", "L1",
"L2", "L3", "L3", "L2", "L1"
), length_mi = c(2, 1.5, 1,
2, 1.5, 1,
2, 1, 1.5,
1.5, 1, 2,
2, 1, 1.5,
1.5, 1, 2,
2, 1, 1.5,
2, 1.5, 1,
1, 1.5, 2,
1.5, 1, 2,
2, 1.5, 1,
2, 1.5, 1,
1, 1.5, 2),
avg_tdensity = c(59.9809611866698, 56.3123856677006, 57.6719458363159,
59.3508907977412, 56.4167760279965, 57.3786586335799, 56.8455221506403,
53.8045315358307, 54.8844746679392, 54.9068440308598, 52.5369343036666,
54.0965759961823, 53.7734629762189, 53.8517557464408, 53.941233198123,
57.5837111270182, 54.4867971049073, 59.4453392189613, 61.6561779209417,
55.8401435616003, 60.4730871709218, 63.5737294201861, 66.6594587608367,
56.9064165274795, 58.3753380259286, 65.83552055993, 63.5973415254911,
55.3480175773483, 56.538564781675, 54.6707229778096, 58.3641533444683,
52.1665970730931, 56.8069971367215, 60.7017517696652, 53.6516742225404,
56.3745227869244, 53.4913604549431, 52.4971665473634, 56.1781694901774
)), row.names = c(NA, -39L), class = c("tbl_df", "tbl", "data.frame"
))
library(ggplot2)
ggplot(data, aes(x = timestamp, y = length_mi,
group = Road_segment, fill = avg_tdensity)) +
geom_bar(stat = 'identity', position = position_stack(), width = 5*60) +
coord_flip() +
scale_fill_gradient(low = "yellow", high = "red", na.value = NA)+
labs(x=NULL, y=NULL)+
theme(axis.line=element_blank(),axis.text.x=element_blank(),
axis.text.y=element_blank(),axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),legend.position="none",
panel.background=element_blank(),panel.border=element_blank(),panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),plot.background=element_blank(),
panel.grid = element_blank(),
plot.margin = unit(c(0, 0, 0, 0), "points")
)
繪圖后,我使用 theme_get() 檢查了邊距,發現所有邊距均為 5.5 點。
白色邊框主要是由於 x 和 y 比例的默認擴展。 要擺脫這種使用scale_x/y_continuous(expands = c(0, 0))
。 此外,我在theme
添加了axis.ticks.length=unit(0, "pt")
以刪除將繪制刻度的空間。 否則你仍然會得到一個小的白色邊框。
library(ggplot2)
ggplot(data, aes(x = timestamp, y = length_mi,
group = Road_segment, fill = avg_tdensity)) +
geom_bar(stat = 'identity', position = position_stack(), width = 5*60) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
coord_flip() +
scale_fill_gradient(low = "yellow", high = "red")+
labs(x=NULL, y=NULL) +
theme(axis.line=element_blank(),axis.text.x=element_blank(),
axis.text.y=element_blank(),axis.ticks=element_blank(),
axis.ticks.length = unit(0, "pt"),
axis.title.x=element_blank(),
axis.title.y=element_blank(),legend.position="none",
panel.background=element_blank(),panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),plot.background=element_blank(),
panel.grid = element_blank(),
plot.margin = unit(c(0, 0, 0, 0), "points")
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.