r - 如何将轴刻度线的限制调整为与 ggplot 中的边框相同?

标签 r ggplot2 margins

我有下图,我希望对其进行格式化,以便每个轴上的刻度线是由粗黑色边框界定的区域的上部和下部范围。

ggplot(WPND, aes(x = Year)) + 
geom_line(aes(y=FOX_LS), colour="black", size=1.2, linetype=1) + 
geom_line(aes(y=FOX_HS), colour="red", size=1.2, linetype=1) +
theme_light() +
theme(axis.text.x = element_text(color= "black", size=11, angle=90, hjust = 
-2, vjust = 0.5)) +
theme(axis.text.y = element_text(color = "black", size = 11)) +
ylab(expression("BAI (cm "*{}^2*")")) +
scale_x_continuous(breaks = seq(1960, 2015, by=5)) +
scale_y_continuous(limits = c(0,60), breaks = seq(0, 60, b=10)) +
theme(panel.border = element_rect(color = "black", fill = NA, size = 1.5),
panel.grid.minor = element_blank(),
panel.grid.major = element_line(color = 'grey', linetype= 'dashed'),
axis.ticks.length = unit(-0.25, "cm"),
axis.ticks = element_line(color = "black", size = 1),
axis.text.x = element_text(margin = unit(c(0.5,0.5,0.5,0.5), "cm")),
axis.text.y = element_text(margin = unit(c(0.5,0.5,0.5,0.5), "cm")))`

我在 ggplot 中制作的图

figure I've made in ggplot

我想要刻度线/边距的外观(Excel 中制作的图)

how I want the tick marks/margins to look (figure made in excel)

最佳答案

您必须将 expand = c(0,0) 参数添加到 xy 尺度:

... +
scale_x_continuous(breaks = seq(1960, 2015, by=5), expand = c(0,0)) +
scale_y_continuous(limits = c(0,60), breaks = seq(0, 60, b=10), expand = c(0,0)) +
...

关于r - 如何将轴刻度线的限制调整为与 ggplot 中的边框相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44284877/

相关文章:

r - ggplot 根据数据框中的值重新排序堆叠条形图

r - igraph轴xlim ylim图错误

r - 如何显示两列相交的日期?

r - 在 R 中逐步回归中提取最终系数

r - 如何强制 ggplot2 图中的所有方面具有相同的 x :y scale ratio, 但允许比例本身变化?

r - 是否有 R 函数可以调整条形图轴的比例?

r - 如何将 geom_box 中的填充 alpha 设置为因子中的观察数

css - 是什么导致元素之间的边距?我怎样才能摆脱它们?

动态插入 View 时android边距不起作用

html - 为什么我的按钮和它的边框之间有一个空白?