调整Johnson-Neyman图:坐标轴、配色及标题格式问题求助
解决方案
你可以通过在ggplot对象上叠加标准ggplot2语法的图层和主题设置来实现需求,修改后的完整代码如下:
library(interactions) library(ggplot2) # 加载ggplot2以使用相关自定义函数 data <- read.csv("C:/Users/Itai/Desktop/1.csv") test <- lm(choose_town ~ cond_num * CFNU, data = data) summary(test) # 生成Johnson-Neyman分析及绘图对象 y <- johnson_neyman(model = test, pred = cond_num, modx = CFNU) y_plot <- y[["plot"]] # 调整标签、坐标轴及样式设置 y_plot <- y_plot + labs( x = "CNFU", y = "Donation Destination", title = "Johnson-Neyman Plot" ) + # 设置坐标轴刻度与范围 scale_x_continuous(breaks = 1:7, limits = c(1,7)) + scale_y_continuous(breaks = c(-1,0,1), limits = c(-1,1)) + # 切换为灰度配色方案 scale_color_grey(start = 0.2, end = 0.8) + scale_fill_grey(start = 0.7, end = 0.9) + # 设置标题居中 theme(plot.title = element_text(hjust = 0.5)) # 输出图形 y_plot
关键改动说明
- 坐标轴刻度调整:通过
scale_x_continuous()和scale_y_continuous()的breaks参数指定要显示的刻度值,limits参数控制坐标轴范围,这种方式更符合ggplot2的标准语法逻辑。 - 灰度配色:使用
scale_color_grey()和scale_fill_grey()分别设置线条/点的颜色和填充区域的灰度,start和end参数可灵活调整灰度的深浅区间。 - 标题居中:通过
theme()中的plot.title = element_text(hjust = 0.5)实现标题的水平居中对齐。
内容的提问来源于stack exchange,提问作者Itai
相关产品推荐
相关产品推荐

