【问题标题】:Editing ggplot default legend symbols编辑 ggplot 默认图例符号
【发布时间】:2019-09-10 08:57:37
【问题描述】:

我有以下脚本通过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 新手,所以我希望有人能让我走上正轨!

附:我的数据:

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

【问题讨论】:

标签: r ggplot2


【解决方案1】:

使用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)))

由于您没有在代码中提供自定义抖动,因此我已删除它。

【讨论】:

    猜你喜欢
    • 2020-07-28
    • 1970-01-01
    • 2021-02-02
    • 2014-07-01
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    相关资源
    最近更新 更多