编号的点标签加上散点图中的图例 [英] Numbered point labels plus a legend in a scatterplot

查看:152
本文介绍了编号的点标签加上散点图中的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用数字(1,2,3,...)在R( ggplot2 )中的散点图中标记点,然后将数字与名称进行匹配在一个传说中(1 - 阿尔法,2 - 布拉沃,3 - 查理...),作为一种处理太多太长标签的情节。

让我们假设这是a.df:

 
名称X属性是属性大小属性颜色属性
Alpha 1 2.5 10 A
Bravo 3 3.5 5 B
Charlie 2 1.5 10 C
Delta 5 1 15 D

是一个标准的散点图:

 
ggplot(a.df,aes(x = X.Attribute,y = Y.Attribute,size = Size .Attribute,fill = Colour.Attribute,label = Name))+
geom_point(shape = 21)+
geom_text(size = 5,hjust = -0.2,vjust = 0.2)

有没有办法改变它如下?




  • 具有用数字标记的散点图点(1,2,3 ...)
  • 在分配图形标签(1, 2,3 ...)转换为a.df $名称



  • 在下一步中,我想将其他属性指定给点大小和颜色,这可能会排除一些'黑客'。

    解决方案

    这是一个替代解决方案,它将标签绘制为 geom_text 。我借用了
    ggplot2 - 在剧情外注释。对于Cars93数据
    库(网格)
    库(ggplot2)
    库(MASS)#$>

      
    d< - Cars93 [1:30,]
    d $ row_num< -1:nrow(d)
    d $ legend_entry< - paste(,d $ row_num,d $ Manufacturer ,d $模型)

    ymin < - min(d $ Price)
    ymax < - max(d $ Price)
    y_values < - ymax-(ymax- (d)aes(x = Min.Price,y = Price))+
    geom_text(d) (aes(label = row_num))+
    geom_text(aes(label = legend_entry,x = Inf,y = y_values,hjust = 0))+
    theme(plot.margin = unit(c(1 ,15,1,1),lines))

    gt< - ggplot_gtable(ggplot_build(p))
    gt $ layout $ clip [gt $ layout $ name ==面板]< - off
    grid.draw(gt)


    I am trying to label points in a scatterplot in R (ggplot2) using numbers (1, 2, 3, ...) and then match the numbers to names in a legend (1 - Alpha, 2 - Bravo, 3 - Charlie... ), as a way of dealing with too many, too long labels on the plot.

    Let's assume this is a.df:

    Name      X Attribute   Y Attribute  Size Attribute  Color Attribute
    Alpha     1             2.5          10              A
    Bravo     3             3.5          5               B
    Charlie   2             1.5          10              C
    Delta     5             1            15              D
    

    And this is a standard scatterplot:

    ggplot(a.df, aes(x=X.Attribute, y=Y.Attribute, size=Size.Attribute, fill=Colour.Attribute, label=Name)) +
       geom_point(shape=21) +
       geom_text(size=5, hjust=-0.2,vjust=0.2)
    

    Is there a way to change it as follows?

    • have scatterplot points labeled with numbers (1,2,3...)
    • have a legend next to the plot assigning the plot labels (1,2,3...) to a.df$Name

    In the next step I would like to assign other attributes to the point size and color, which may rule out some 'hacks'.

    解决方案

    Here's an alternative solution, which draws the labels as geom_text. I've borrowed from ggplot2 - annotate outside of plot.

    library(MASS)  # for Cars93 data
    library(grid)
    library(ggplot2)
    
    d <- Cars93[1:30,]
    d$row_num <- 1:nrow(d)
    d$legend_entry <- paste("  ", d$row_num, d$Manufacturer, d$Model)
    
    ymin <- min(d$Price)
    ymax <- max(d$Price)
    y_values <- ymax-(ymax-ymin)*(1:nrow(d))/nrow(d)
    
    p <- ggplot(d, aes(x=Min.Price, y=Price)) +
            geom_text(aes(label=row_num)) +
            geom_text(aes(label=legend_entry, x=Inf, y=y_values, hjust=0)) +
            theme(plot.margin = unit(c(1,15,1,1), "lines"))
    
    gt <- ggplot_gtable(ggplot_build(p))
    gt$layout$clip[gt$layout$name == "panel"] <- "off"
    grid.draw(gt)
    

    这篇关于编号的点标签加上散点图中的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆