【问题标题】:Change ggplot coloured lines in legend to squares or circles将图例中的 ggplot 彩色线条更改为正方形或圆形
【发布时间】:2012-09-01 03:15:42
【问题描述】:

除了当前构成图例键的灰色背景上的彩色线条,我希望键标签旁边有颜色的正方形或圆形,以便使颜色易于看到。我怎样才能做到这一点?下面是一个代码 sn-p 用作示例:

 mry <- do.call(rbind, by(movies, round(movies$rating), function(df) {
   nums <- tapply(df$length, df$year, length)
   data.frame(rating=round(df$rating[1]), year = as.numeric(names(nums)),
   number=as.vector(nums))
 }))

ggplot(mry, aes(x=year, y=number, colour=factor(rating))) + geom_line() +
scale_colour_brewer(palette="YlGnBu")

【问题讨论】:

    标签: r ggplot2 legend-properties


    【解决方案1】:

    一个黑客来做圈......

    ggplot(mry, aes(x=year, y=number, colour=factor(rating))) +
      scale_colour_brewer(palette="YlGnBu") +  
      geom_point()  +      
      geom_point(size=5,colour="white",show_guide=FALSE)  +
      opts(
        panel.background = theme_rect(fill =  "transparent"), 
        panel.grid.minor = theme_blank(),
        panel.grid.major = theme_blank(),
        plot.background = theme_rect(fill = "transparent",colour = NA)
      ) +  geom_line(show_guide=FALSE)
    

    【讨论】:

    • 你可以通过改变点的形状来制作正方形等
    • 如果您想要更大的圆形和正方形的大小,也可以轻松编辑
    • 更新 opts 现已弃用,使用theme 并将theme_rect 替换为element_rect 等...
    猜你喜欢
    • 1970-01-01
    • 2021-07-16
    • 2020-07-11
    • 2021-02-07
    • 2015-07-26
    • 2014-04-09
    • 1970-01-01
    • 2019-11-19
    • 2021-10-05
    相关资源
    最近更新 更多