我是 R 的初学者,但我正在尝试绘制一个在 x(5)和 y(2.5)轴上包含小刻度线的图。我改变了 theme_bw 以我想要的方式改变情节;然而,我不确定这是否会抑制我看到小刻度线的能力。
我尝试使用minor_breaks来添加这些刻度线;然而,他们还没有露面。这是我的代码,第一部分是我对主题所做的更改,第二部分是情节中的编码。我附上了我得到的情节的图像。您可以看到两个轴上标记的主要刻度线之间没有小刻度线。
#Setting Up General Plot Theme:
theme_set(theme_bw())
theme_update(
text = element_text(family = "Gill Sans MT", size = 14, color = "black"), # Set Font
axis.title.x = element_text(face = "bold"), # Bold X Axis Title
axis.title.y = element_text(face = "bold", angle = 90), # Bold Y Axis Title
axis.text.x = element_text(face = "bold", size = 12, color = "black", margin = margin(t = 5)), #Bold X Axis Text
axis.text.y = element_text(face = "bold", size = 12, color = "black", margin = margin(r = 5)), # Bold Y Axis Text with margin at bottom
axis.ticks = element_line(linewidth = 3, color = "black" ), # Increase Tick Mark Thickness
axis.ticks.length = unit(-0.25, "cm"), # Increase and Invert Tick Marks
panel.border = element_rect(color = "black", linewidth = 3), # Increase Axis Thickness
panel.grid.major = element_blank(), # Remove Major Grid Lines
panel.grid.minor = element_blank() # Remove Minor Grid Lines
)
# Plots:
[enter image description here](https://i.stack.imgur.com/KXghd.png)
# Set X-Axis Bounds and Labels:
x_bounds <- c(0, 90) # Set the lower and upper bounds for the x-axis.
x_labels <- seq(0, 100, by = 10) # Define the labels for the x-axis.
# Plotting:
plot <- ggplot(melting_combined, aes(x = Temperature_M5, y = CD_M5)) +
geom_point(size = 3) +
labs(
x = "Temperature (\u00b0C)",
y = expression(bold(paste("MRE"[222], " \u00d7 10"^-3, " (deg cm"^2, "dmol"^-1, ")")))
) +
scale_y_continuous(expand = c(0, 0), minor_breaks = seq(-25, 0, by = 2.5), breaks = seq(-25, 0, by = 5), limits = c(-25, 0)) +
scale_x_continuous(expand = c(0, 0), minor_breaks = seq(10, 90, by = 5), breaks = seq(10, 90, by = 10), limits = c(10, 90)) +
coord_cartesian(clip = "off") +
theme(panel.border = element_rect(linewidth = 1, fill = NA),
axis.ticks = element_line(linewidth = 1)
)
print(plot)
您可以使用 ggprism 包来完成此操作。
library(ggprism)
library(ggplot2)
A <- ggplot(mtcars, aes(x = mpg, y = disp)) +
geom_point()
B <- ggplot(mtcars, aes(x = mpg, y = disp)) +
geom_point() +
scale_x_continuous(guide = "prism_minor")
更新: 或者使用 ggplot2 版本 3.5.0(2024-02-23 发布):
ggplot(mtcars, aes(x = mpg, y = disp)) +
geom_point() +
scale_x_continuous(guide=guide_axis(minor.ticks = TRUE))
(感谢@teunbrand!) 在 ggplot2 中的 x 轴上添加小刻度线(无标签)