我试图将模型的截距值放在 y 轴上。 我使用的代码是这样的:
library(ggplot2)
mod <- lm(circumference ~ age + I(age^2), Orange); summary(mod)
novo_dados <- data.frame(age = seq(0, max(Orange$age), length.out = 100))
valores_previstos <- predict(mod, newdata = novo_dados)
intercepto <- coef(mod)[1]
ggplot(data = Orange, aes(x = age, y = circumference)) +
geom_point(color = "black", shape = 19, size = 2) +
coord_cartesian(xlim = c(0,1600), ylim = c(0,250)) +
geom_line(data = novo_dados, aes(x = age, y = valores_previstos),
color = "black", linewidth = 2) +
labs(title = "Quadratic Model",
x = "Age",
y = "Circumference") +
scale_y_continuous(breaks = c(0, intercepto, max(Orange$circumference)),
labels = c("0", round(intercepto, 2), max(Orange$circumference))) +
scale_y_continuous(breaks = c(0, 100, 200, 250), labels = c("0", "100", "200", "250")) +
theme(legend.position = "none",
axis.title = element_text(size = 30),
axis.text.x = element_text(color = "black", hjust=1),
axis.text.y = element_text(color = "black", hjust=1),
axis.text = element_text(size = 30),
plot.title = element_text(size = 15),
strip.text.x = element_text(size = 12))
上面代码的结果是:
但请注意,它随着与将 y 轴上的截距值作为文本包含在内相关的信息而消失,即:
scale_y_continuous(breaks = c(0, intercepto, max(Orange$circumference)),
labels = c("0", round(intercepto, 2), max(Orange$circumference)))
当我仅考虑上述部分来运行图表时,我得到了我想要的,但是 y 轴仅显示 y 变量的最大值。
ggplot(data = Orange, aes(x = age, y = circumference)) +
geom_point(color = "black", shape = 19, size = 2) +
coord_cartesian(xlim = c(0,1600), ylim = c(0,250)) +
geom_line(data = novo_dados, aes(x = age, y = valores_previstos),
color = "black", linewidth = 2) +
labs(title = "Quadratic Model",
x = "Age",
y = "Circumference") +
scale_y_continuous(breaks = c(0, intercepto, max(Orange$circumference)),
labels = c("0", round(intercepto, 2), max(Orange$circumference))) +
# scale_y_continuous(breaks = c(0, 100, 200, 250), labels = c("0", "100", "200", "250")) +
theme(legend.position = "none",
axis.title = element_text(size = 30),
axis.text.x = element_text(color = "black", hjust=1),
axis.text.y = element_text(color = "black", hjust=1),
axis.text = element_text(size = 30),
plot.title = element_text(size = 15),
strip.text.x = element_text(size = 12))
我希望截距值出现在 y 轴上,但其他 y 轴值没有变化,即保持正常。
拥有两个 y 比例确实只会将指定的最后一个应用于绘图:
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
但是您可以将截距值和 y 轴值包含在相同的比例中:
head(Orange)
library(ggplot2)
mod <- lm(circumference ~ age + I(age^2), Orange)
novo_dados <- data.frame(age = seq(0, max(Orange$age), length.out = 100))
valores_previstos <- predict(mod, newdata = novo_dados)
intercepto <- coef(mod)[1]
ggplot(data = Orange, aes(x = age, y = circumference)) +
geom_point(color = "black", shape = 19, size = 2) +
coord_cartesian(xlim = c(0, 1600), ylim = c(0, 250)) +
geom_line(data = novo_dados, aes(x = age, y = valores_previstos),
color = "black", linewidth = 2) +
labs(title = "Quadratic Model",
x = "Age",
y = "Circumference") +
scale_y_continuous(breaks = c(0, intercepto, 100, 200, 250),
labels = c("0", round(intercepto, 2), "100", "200", "250")) +
theme(legend.position = "none",
axis.title = element_text(size = 30),
axis.text.x = element_text(color = "black", hjust = 1),
axis.text.y = element_text(color = "black", hjust = 1),
axis.text = element_text(size = 30),
plot.title = element_text(size = 15),
strip.text.x = element_text(size = 12))
您想要 y 轴刻度上的截距值吗?您只需要调整 y 轴,但不删除您想要的原始断点,如下所示:
head(Orange)
library(ggplot2)
mod <- lm(circumference ~ age + I(age^2), Orange); summary(mod)
novo_dados <- data.frame(age = seq(0, max(Orange$age), length.out = 100))
valores_previstos <- predict(mod, newdata = novo_dados)
intercepto <- coef(mod)[1]
ggplot(data = Orange, aes(x = age, y = circumference)) +
geom_point(color = "black", shape = 19, size = 2) +
coord_cartesian(xlim = c(0,1600), ylim = c(0,250)) +
geom_line(data = novo_dados, aes(x = age, y = valores_previstos),
color = "black", linewidth = 2) +
labs(title = "Quadratic Model",
x = "Age",
y = "Circumference") +
scale_y_continuous(breaks = c(0, intercepto, max(Orange$circumference), 100, 200, 250), ## Include the original breaks
labels = c("0", round(intercepto, 2), max(Orange$circumference), "100", "200", "250")) +
theme(legend.position = "none",
axis.title = element_text(size = 20), ## Adjust text size for clarity
axis.text.x = element_text(color = "black", hjust=1),
axis.text.y = element_text(color = "black", hjust=1),
axis.text = element_text(size = 15), ## Adjust text size for clarity
plot.title = element_text(size = 15),
strip.text.x = element_text(size = 12))