【问题标题】:change border from around legend from a scatterplot从散点图更改图例周围的边框
【发布时间】:2012-11-28 16:07:35
【问题描述】:

这应该很简单,但我不知道如何从我的图例周围移除边框。我还想将图例放在图表中并删除内部网格线以及顶部和左侧边框。我正在使用 scatterplot 函数,这是我迄今为止编写的代码:

scatterplot(Comp1~ln1wr|Season, moose,  
      xlab = "Risk", ylab = "Principal component 1",
      labels= row.names(moose), by.groups=T, smooth=F, boxplots=F, legend.plot=F)
      legend("bottomleft", moose, fill=0)

在这里,我只是在尝试是否可以将图例放置在其他位置,但是每次运行此代码时,都会出现错误

Error in as.graphicsAnnot(legend) : 
  argument "legend" is missing, with no default

我想将图例放在图表中,但不会与显示的数据冲突。这是样本数据:

structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 32L, 33L, 
33L, 34L, 34L, 34L), .Label = c("F07001", "F07002", "F07003", 
"F07004", "F07005", "F07006", "F07008", "F07009", "F07010", "F07011", 
"F07014", "F07015", "F07017", "F07018", "F07019", "F07020", "F07021", 
"F07022", "F07023", "F07024", "F10001", "F10004", "F10008", "F10009", 
"F10010", "F10012", "F10013", "F98015", "M07007", "M07012", "M07013", 
"M07016", "M10007", "M10011", "M10015"), class = "factor"), Season = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L), .Label = c("SUM", "WIN"
), class = "factor"), Time = structure(c(1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L), .Label = c("day", "night"), class = "factor"), 
    Repro = structure(c(2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
    3L), .Label = c("f", "fc", "m"), class = "factor"), Comp1 = c(-0.524557195, 
    -0.794214153, -0.408247216, -0.621285004, -0.238828585, 0.976634392, 
    -0.202405922, -0.633821539, -0.306163898, -0.302261589, 1.218779672
    ), ln1wr = c(0.833126490613386, 0.824526258616325, 0.990730077688989, 
    0.981816265754353, 0.933462450382474, 1.446048015519, 1.13253050687157, 
    1.1349442179155, 1.14965388471562, 1.14879830358128, 1.14055365645628
    )), .Names = c("ID", "Season", "Time", "Repro", "Comp1", 
"ln1wr"), row.names = c(1L, 2L, 3L, 4L, 5L, 220L, 221L, 222L, 
223L, 224L, 225L), class = "data.frame")

【问题讨论】:

  • legend 不知道您希望在图例中包含什么文字。阅读?legend
  • 您必须将参数“legend”赋予 legend:legend("bottomleft", legend = "text", other-arguments)
  • 你想用你的情节实现什么?您提供的代码示例似乎没有显示有意义的数据图。
  • scatterplot 来自什么包?
  • @Chargaff 对此我深表歉意,我所做的只是提供了第一部分真实数据,没有想到它可能不会向您展示一些有意义的东西。

标签: r legend scatter-plot legend-properties


【解决方案1】:

建议

par(bty="l",las=1)
scatterplot(Comp1~ln1wr|Season, moose,  
      xlab = "Risk", ylab = "Principal component 1",
            labels= row.names(moose),
            by.groups=TRUE, smooth=FALSE, boxplots=FALSE,
            grid=FALSE,
            legend.plot=FALSE)
legend("bottomright", title="Season",
       legend=levels(moose$Season), bty="n",
       pch=1:2, col=1:2)
  • ?legend 所示,bty 控制图例框 -- "n" 表示“无”。
  • 我将图例放在右下角而不是左下角,因为这样似乎可以更好地避开您的数据。
  • 我使用bty="l" 来消除盒子的顶部和右侧边缘(这意味着“盒子类型L”)
  • 我使用 las=1 将 y 轴刻度标签设为水平 - 您没有要求这样做,但我非常喜欢它
  • grid=FALSE 移除内部网格线

【讨论】:

    【解决方案2】:

    您必须为您的驼鹿 ID 设置唯一性,因为每只驼鹿拥有多个点。

    legend("bottomleft",legend=unique(moose))

    然后您必须将颜色和点类型与您的图例相关联(对应于您绘图中的驼鹿 ID)。我也会看看 plot() 而不是 scatterplot()。

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 2011-06-08
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 2011-08-20
      • 2023-03-07
      相关资源
      最近更新 更多