You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ggplot2调整geom_line与geom_point的size参数失效问题求助

问题原因

你将size参数放在了aes()函数内部,ggplot会将size识别为需要和数据绑定的映射属性,而非固定的图形大小参数,因此你设置的固定数值不会按预期生效,还会额外生成不需要的size图例,导致线条、点的大小不受控制。

解决方法

把size参数移动到aes()外部即可,修改后的完整代码如下:

ggplot(data=ebf, aes(x=forcats::fct_inorder(Subscales), y=MP1, group="")) +
  # 线条size可在1~1.5之间调整到你觉得合适的粗细
  geom_line(aes(y = MP1, linetype="MP1", color = "MP1"), size = 1.2) +
  geom_line(aes(y = MP2, linetype="MP2", color = "MP2"), size = 1.2) +
  geom_line(aes(y = MP3, linetype="MP3", color = "MP3"), size = 1.2) +
  # 点的size可在2~3之间调整到你觉得合适的大小
  geom_point(aes(y = MP1, shape ="MP1", color = "MP1"), size = 2.5)+
  geom_point(aes(y = MP2, shape ="MP2", color = "MP2"), size = 2.5)+
  geom_point(aes(y = MP3, shape ="MP3", color = "MP3"), size = 2.5)+
  coord_flip() +
  labs(x="RESTQ-Sport Subscales", color = "Legend:", linetype = "Legend:", shape = "Legend:") +
  scale_color_manual(values = colors)+
  scale_y_continuous(limits = c(0, 6), breaks = c(0,1,2,3,4,5,6), labels = paste0(0:6, "\n", labels.minor), sec.axis = sec_axis(~.x, breaks = 0:6)) +
  theme(panel.grid.major.y = element_blank(),panel.grid.minor.x = element_blank(),axis.line.x = element_line(size = 1, colour = "black", linetype=1),axis.title=element_blank(),legend.position="bottom")

可选优化建议

你可以先将数据集转换为长格式,避免重复写多段geom_line和geom_point代码,后续调整也更方便:

library(tidyr)
ebf_long <- ebf %>% 
  pivot_longer(cols = starts_with("MP"), names_to = "MP_group", values_to = "value")

ggplot(data=ebf_long, aes(x=forcats::fct_inorder(Subscales), y=value, group=MP_group, color=MP_group, linetype=MP_group, shape=MP_group)) +
  geom_line(size = 1.2) +
  geom_point(size = 2.5) +
  coord_flip() +
  labs(x="RESTQ-Sport Subscales", color = "Legend:", linetype = "Legend:", shape = "Legend:") +
  scale_color_manual(values = colors)+
  scale_y_continuous(limits = c(0, 6), breaks = c(0,1,2,3,4,5,6), labels = paste0(0:6, "\n", labels.minor), sec.axis = sec_axis(~.x, breaks = 0:6)) +
  theme(panel.grid.major.y = element_blank(),panel.grid.minor.x = element_blank(),axis.line.x = element_line(size = 1, colour = "black", linetype=1),axis.title=element_blank(),legend.position="bottom")

内容的提问来源于stack exchange,提问作者psycho95

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 14:45:05