ggplot:线条和形状大小的调整不是比较
ggplot: adjustment of line and shape size is not comparatively
我有这个数据集:
structure(list(Subscales = c("Self-Regulation", "Self-Efficacy",
"Personal Accomplishment", "Being in Shape", "Injury", "Emotional Exhaustion",
"Disturbed Breaks", "Sleep Quality", "General Well-Being", "Physical Recovery",
"Social Recovery", "Success", "Physical Complaints", "Lack of Energy",
"Fatigue", "Conflicts/Pressure", "Social Stress", "Emotional Stress",
"General Stress"), MP1 = c(3.51, 3.34, 2.64, 3.58, 1.84, 1.46,
1.26, 4.29, 3.84, 3.44, 3.46, 2.68, 1.69, 1.93, 1.86, 1.78, 1.8,
1.68, 1.69), MP2 = c(3.47, 3.26, 2.57, 3.4, 1.89, 1.45, 1.26,
4.21, 3.87, 3.47, 3.43, 2.54, 1.8, 1.81, 1.99, 2.03, 1.93, 1.8,
1.73), MP3 = c(3.5, 3.37, 2.8, 3.37, 1.98, 1.46, 1.31, 4.16,
4.03, 3.45, 3.85, 2.79, 1.85, 1.63, 2.14, 1.91, 1.89, 1.7, 1.5
), Skalen = structure(c(15L, 14L, 11L, 1L, 9L, 4L, 3L, 16L, 8L,
13L, 17L, 19L, 12L, 10L, 6L, 2L, 18L, 5L, 7L), .Label = c("Being in Shape",
"Conflicts/Pressure", "Disturbed Breaks", "Emotional Exhaustion",
"Emotional Stress", "Fatigue", "General Stress", "General Well-Being",
"Injury", "Lack of Energy", "Personal Accomplishment", "Physical Complaints",
"Physical Recovery", "Self-Efficacy", "Self-Regulation", "Sleep Quality",
"Social Recovery", "Social Stress", "Success"), class = "factor")), row.names = c(NA,
-19L), class = c("tbl_df", "tbl", "data.frame"))
并创建了这个情节:
ggplot(data=ebf, aes(x=forcats::fct_inorder(Subscales), y=MP1, group="")) +
geom_line(aes(y = MP1, linetype="MP1", color = "MP1")) +
geom_line(aes(y = MP2, linetype="MP2", color = "MP2")) +
geom_line(aes(y = MP3, linetype="MP3", color = "MP3")) +
geom_point(aes(y = MP1, shape ="MP1", color = "MP1"))+
geom_point(aes(y = MP2, shape ="MP2", color = "MP2"))+
geom_point(aes(y = MP3, shape ="MP3", color = "MP3"))+
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")
为了更好的说明,我想调整图表的线条和形状。线条应该粗一点,形状应该大一点。
所以我像这样更改了我的代码,就像我之前对以前的 ggplot 图所做的那样:
ggplot(data=ebf, aes(x=forcats::fct_inorder(Subscales), y=MP1, group="")) +
geom_line(aes(y = MP1, linetype="MP1", color = "MP1", size = 1)) +
geom_line(aes(y = MP2, linetype="MP2", color = "MP2", size = 1)) +
geom_line(aes(y = MP3, linetype="MP3", color = "MP3", size = 1)) +
geom_point(aes(y = MP1, shape ="MP1", color = "MP1", size = 3))+
geom_point(aes(y = MP2, shape ="MP2", color = "MP2", size = 3))+
geom_point(aes(y = MP3, shape ="MP3", color = "MP3", size = 3))+
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")
但无论我如何定义线条和形状大小,结果总是大形状或粗线,如下所示:
我不知道如何调整它,以获得具有足够线条和形状的漂亮图表。
在此先致谢,感谢您提供的任何帮助。
干杯
我 运行 在 运行 你的代码中遇到了一些问题,所以我不得不修改下面的代码块。但是可能导致您出现问题的原因是您将 size
参数放在 aes()
中。如果你拉出size
,那么你就可以调整线和点的大小。
ggplot(data=ebf, aes(x=forcats::fct_inorder(Subscales), y=MP1, group="")) +
geom_line(aes(y = MP1, linetype="MP1", color = "MP1"), size = 1) +
geom_line(aes(y = MP2, linetype="MP2", color = "MP2"), size = 1) +
geom_line(aes(y = MP3, linetype="MP3", color = "MP3"), size = 1) +
geom_point(aes(y = MP1, shape ="MP1", color = "MP1"), size = 3)+
geom_point(aes(y = MP2, shape ="MP2", color = "MP2"), size = 3)+
geom_point(aes(y = MP3, shape ="MP3", color = "MP3"), size = 3)+
coord_flip() +
labs(x="RESTQ-Sport Subscales", color = "Legend:", linetype = "Legend:", shape = "Legend:")
正如哈里森·琼斯已经提到的,我们缺少 labels.minor
和 color
来完全重现您的情节。所以我也跳过了这些行。他还正确地指出, aes
调用中的 size 参数是你的问题。我想另外指出,如果您的数据采用整洁的数据格式(每次观察一行),您的绘图可能会更容易和更短。这将避免对 geom_line
和 geom_point
的三个调用的需要,并且您可以在 ggplot 调用中定义所有美学,而无需重复它们。
ebf %>%
pivot_longer(cols = MP1:MP3, names_to = "MP", values_to = "values") %>%
ggplot(aes(x=forcats::fct_inorder(Subscales), y = values, group = MP, color = MP, linetype = MP)) +
geom_line() +
geom_point(size = 2) +
coord_flip() +
labs(x="RESTQ-Sport Subscales", color = "Legend:", linetype = "Legend:", shape = "Legend:") +
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")
我有这个数据集:
structure(list(Subscales = c("Self-Regulation", "Self-Efficacy",
"Personal Accomplishment", "Being in Shape", "Injury", "Emotional Exhaustion",
"Disturbed Breaks", "Sleep Quality", "General Well-Being", "Physical Recovery",
"Social Recovery", "Success", "Physical Complaints", "Lack of Energy",
"Fatigue", "Conflicts/Pressure", "Social Stress", "Emotional Stress",
"General Stress"), MP1 = c(3.51, 3.34, 2.64, 3.58, 1.84, 1.46,
1.26, 4.29, 3.84, 3.44, 3.46, 2.68, 1.69, 1.93, 1.86, 1.78, 1.8,
1.68, 1.69), MP2 = c(3.47, 3.26, 2.57, 3.4, 1.89, 1.45, 1.26,
4.21, 3.87, 3.47, 3.43, 2.54, 1.8, 1.81, 1.99, 2.03, 1.93, 1.8,
1.73), MP3 = c(3.5, 3.37, 2.8, 3.37, 1.98, 1.46, 1.31, 4.16,
4.03, 3.45, 3.85, 2.79, 1.85, 1.63, 2.14, 1.91, 1.89, 1.7, 1.5
), Skalen = structure(c(15L, 14L, 11L, 1L, 9L, 4L, 3L, 16L, 8L,
13L, 17L, 19L, 12L, 10L, 6L, 2L, 18L, 5L, 7L), .Label = c("Being in Shape",
"Conflicts/Pressure", "Disturbed Breaks", "Emotional Exhaustion",
"Emotional Stress", "Fatigue", "General Stress", "General Well-Being",
"Injury", "Lack of Energy", "Personal Accomplishment", "Physical Complaints",
"Physical Recovery", "Self-Efficacy", "Self-Regulation", "Sleep Quality",
"Social Recovery", "Social Stress", "Success"), class = "factor")), row.names = c(NA,
-19L), class = c("tbl_df", "tbl", "data.frame"))
并创建了这个情节:
ggplot(data=ebf, aes(x=forcats::fct_inorder(Subscales), y=MP1, group="")) +
geom_line(aes(y = MP1, linetype="MP1", color = "MP1")) +
geom_line(aes(y = MP2, linetype="MP2", color = "MP2")) +
geom_line(aes(y = MP3, linetype="MP3", color = "MP3")) +
geom_point(aes(y = MP1, shape ="MP1", color = "MP1"))+
geom_point(aes(y = MP2, shape ="MP2", color = "MP2"))+
geom_point(aes(y = MP3, shape ="MP3", color = "MP3"))+
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")
为了更好的说明,我想调整图表的线条和形状。线条应该粗一点,形状应该大一点。
所以我像这样更改了我的代码,就像我之前对以前的 ggplot 图所做的那样:
ggplot(data=ebf, aes(x=forcats::fct_inorder(Subscales), y=MP1, group="")) +
geom_line(aes(y = MP1, linetype="MP1", color = "MP1", size = 1)) +
geom_line(aes(y = MP2, linetype="MP2", color = "MP2", size = 1)) +
geom_line(aes(y = MP3, linetype="MP3", color = "MP3", size = 1)) +
geom_point(aes(y = MP1, shape ="MP1", color = "MP1", size = 3))+
geom_point(aes(y = MP2, shape ="MP2", color = "MP2", size = 3))+
geom_point(aes(y = MP3, shape ="MP3", color = "MP3", size = 3))+
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")
但无论我如何定义线条和形状大小,结果总是大形状或粗线,如下所示:
我不知道如何调整它,以获得具有足够线条和形状的漂亮图表。
在此先致谢,感谢您提供的任何帮助。
干杯
我 运行 在 运行 你的代码中遇到了一些问题,所以我不得不修改下面的代码块。但是可能导致您出现问题的原因是您将 size
参数放在 aes()
中。如果你拉出size
,那么你就可以调整线和点的大小。
ggplot(data=ebf, aes(x=forcats::fct_inorder(Subscales), y=MP1, group="")) +
geom_line(aes(y = MP1, linetype="MP1", color = "MP1"), size = 1) +
geom_line(aes(y = MP2, linetype="MP2", color = "MP2"), size = 1) +
geom_line(aes(y = MP3, linetype="MP3", color = "MP3"), size = 1) +
geom_point(aes(y = MP1, shape ="MP1", color = "MP1"), size = 3)+
geom_point(aes(y = MP2, shape ="MP2", color = "MP2"), size = 3)+
geom_point(aes(y = MP3, shape ="MP3", color = "MP3"), size = 3)+
coord_flip() +
labs(x="RESTQ-Sport Subscales", color = "Legend:", linetype = "Legend:", shape = "Legend:")
正如哈里森·琼斯已经提到的,我们缺少 labels.minor
和 color
来完全重现您的情节。所以我也跳过了这些行。他还正确地指出, aes
调用中的 size 参数是你的问题。我想另外指出,如果您的数据采用整洁的数据格式(每次观察一行),您的绘图可能会更容易和更短。这将避免对 geom_line
和 geom_point
的三个调用的需要,并且您可以在 ggplot 调用中定义所有美学,而无需重复它们。
ebf %>%
pivot_longer(cols = MP1:MP3, names_to = "MP", values_to = "values") %>%
ggplot(aes(x=forcats::fct_inorder(Subscales), y = values, group = MP, color = MP, linetype = MP)) +
geom_line() +
geom_point(size = 2) +
coord_flip() +
labs(x="RESTQ-Sport Subscales", color = "Legend:", linetype = "Legend:", shape = "Legend:") +
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")