【问题标题】:ggplot: adjustment of line and shape size is not comparativelyggplot:线条和形状大小的调整不是比较
【发布时间】:2021-11-15 23:15:32
【问题描述】:

我有这个数据集:

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")

但无论我如何定义线条和形状大小,结果总是会变成大形状或像这样粗线:

我不知道如何调整它,以获得具有足够线条和形状的漂亮图形。

提前致谢,感谢任何形式的帮助。

干杯

【问题讨论】:

    标签: r ggplot2 linegraph


    【解决方案1】:

    我在运行您的代码时遇到了一些问题,因此我不得不在下面修改此代码块。但是可能导致您出现问题的原因是您将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:")
    

    【讨论】:

      【解决方案2】:

      正如哈里森琼斯已经提到的那样,我们缺少 labels.minorcolor 来完全重现你的情节。所以我也跳过了这些行。他还正确地指出,aes 调用中的 size 参数是您的问题。我想另外指出,如果您的数据采用整洁的数据格式(每次观察一行),您的绘图可能会更容易和更短。这将避免对 geom_linegeom_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")
      

      【讨论】:

      • 谢谢你成功了!我同意你们两个,我的代码可以更短更容易。我还不完全熟悉使用 tidyverse。但我尽力而为,我非常感谢我的问题的每一个解决方案,帮助我获得更多知识。
      猜你喜欢
      • 2018-01-11
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 2013-02-16
      • 2017-03-09
      • 2011-09-16
      • 1970-01-01
      相关资源
      最近更新 更多