编辑ggplot默认图例符号

时间:2019-09-10 08:57:37

标签: r ggplot2

我有以下脚本通过ggplot()生成图:

  ggplot(data = dfnew, aes(x = Time, y = Proportion, color=Area, group=linegroup)) +
  geom_point(aes(shape = as.character(Collar)), size = 6, stroke = 0, 
             position = myjit)+
  geom_line(aes(group = linegroup),linetype = "dotted",size=1, position = myjit) +
  theme(axis.text=element_text(size=15),
        axis.title=element_text(size=20)) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper), width=0.3, size=1,
                position = myjit) +
  scale_color_manual(values = c("SNP" = "coral", 
                                "LGCA" = "darkgoldenrod2")) + labs(shape="Collar ID")

Area图例符号是一个点,可能与Collar ID图例混淆。我希望Area图例符号成为一行。

有没有一种方法可以完成功能,或者在绘制之前需要编辑数据集?

我是R的新手,所以我希望有人能把我放在正确的轨道上!

P.S。我的数据:

> dput(dfnew)
structure(list(Proportion = c(0.491475825983558, 0.624947117938639, 
0.284285973983444, 0.459936074937072, 0.438167575182789, 0.5923527, 
0.269347638359089, 0.444195335296524, 0.472343382529259, 0.6119936, 
0.280545311041942, 0.45582336843016), Lower = c(0.373501802431026, 
0.506815311121949, 0.196793171052086, 0.344394223066228, 0.342020291619279, 
0.4962054, 0.197239652248339, 0.347543569904938, 0.362690139261045, 
0.5158463, 0.198654362934906, 0.347479674558168), Upper = c(0.610508712286318, 
0.729864865043791, 0.39179224043653, 0.580031198686217, 0.539194328764963, 
0.6885, 0.356122647401151, 0.545263076314964, 0.5847316572176, 
0.7081409, 0.380178492952045, 0.56851602179505), Area = c("SNP", 
"SNP", "LGCA", "LGCA", "SNP", "SNP", "LGCA", "LGCA", "SNP", "SNP", 
"LGCA", "LGCA"), Time = c("Day", "Night", "Day", "Night", "Day", 
"Night", "Day", "Night", "Day", "Night", "Day", "Night"), Collar = c("41361´", 
"41361´", "41361´", "41361´", "41365´", "41365´", "41365´", "41365´", 
"41366´", "41366´", "41366´", "41366´"), ymin = c(0.117974023552532, 
0.11813180681669, 0.0874928029313584, 0.115541851870844, 0.0961472835635093, 
0.0961473, 0.0721079861107496, 0.0966517653915864, 0.109653243268214, 
0.0961473, 0.0818909481070366, 0.108343693871992), ymax = c(1.10198453826988, 
1.35481198298243, 0.676078214419975, 1.03996727362329, 0.977361903947751, 
1.2808527, 0.625470285760241, 0.989458411611488, 1.05707503974686, 
1.3201345, 0.660723803993988, 1.02433939022521), linegroup = c("SNP 41361´", 
"SNP 41361´", "LGCA 41361´", "LGCA 41361´", "SNP 41365´", "SNP 41365´", 
"LGCA 41365´", "LGCA 41365´", "SNP 41366´", "SNP 41366´", "LGCA 41366´", 
"LGCA 41366´")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-12L))

1 个答案:

答案 0 :(得分:0)

使用guides覆盖图例设置就足够了。

ggplot(data = df, aes(x = Time, y = Proportion, color = Area, group = linegroup)) +
  geom_point(aes(shape = as.character(Collar)), size = 6, stroke = 0) +
  geom_line(aes(group = linegroup),linetype = "dotted", size = 1) +
  theme(axis.text = element_text(size = 15),
        axis.title = element_text(size = 20)) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper), width=0.3, size = 1) +
  scale_color_manual(values = c("SNP" = "coral", 
                                "LGCA" = "darkgoldenrod2")) +
  labs(shape = "Collar ID") +
  guides(color = guide_legend(override.aes = list(linetype = 1, size = 1)))

由于您未随代码一起提供,因此我已删除了自定义抖动。

enter image description here