美文网首页绘图技巧R
ggplot2条形图添加数据标签

ggplot2条形图添加数据标签

作者: 冬之心 | 来源:发表于2019-07-21 15:58 被阅读0次

    title: "添加数据标签"
    author: "wintryheart"
    date: "2019/7/21"
    output: html_document


    knitr::opts_chunk$set(echo = TRUE)
    

    数据

    library(reshape2)
    y2000 <- c(77.17, 11.46, 11.38)
    y2010 <- c(68.23, 16.4,15.37)
    nesttype <- c("与子女生活","独居空巢","夫妻空巢")
    emptynest <- data.frame(nesttype,y2000, y2010)
    knitr::kable(emptynest)
    emptynest2 <- melt(emptynest, id.vars = "nesttype", variable.name = "year", value.name = "proportion")
    with(emptynest2, 
     nesttype <- factor(nesttype, levels = c("与子女生活","夫妇空巢","独居空巢"), labels = c("与子女生活","夫妇空巢","独居空巢"), ordered=T), 
     year <- substr(year, 2, 5) 
    )
    

    简单的条形图

    library(ggplot2)
    library(gridExtra)
    
    p1 <- ggplot(emptynest2, aes(x=nesttype, y=proportion)) + geom_bar(aes(fill=year), position="dodge", stat="identity")
    
    p2 <- ggplot(emptynest2, aes(x=nesttype, y=proportion)) + geom_col(aes(fill=year), position="dodge")
    
    
    grid.arrange(p1, p2, ncol=1)
    
    • geom_col()和geom_bar()的区别在于,geom_col()默认使用原数据,而geom_bar()默认进行count。
    12.png

    group分组问题

    在上图上,ggplot()并没有设置aes()的group参数。这种做法不影响geom_bar(),但是影响geom_text(),使得数据标签不能正确显示。

    
    p3 <- ggplot(emptynest2, aes(x=nesttype, y=proportion)) + 
      geom_col(aes(fill=year), position="dodge") + 
      geom_text(aes(label=proportion, y=proportion+0.05), position=position_dodge(0.9), vjust=0) 
    
    p4 <- ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) + 
      geom_col(aes(fill=year), position="dodge") + 
      geom_text(aes(label=proportion, y=proportion+0.05), position=position_dodge(0.9), vjust=0) 
    
    grid.arrange(p3,p4,ncol=1)
    
    
    13.png

    geom_text()的参数设置问题

    position问题

    对于多组数据,不能直接设置\color{red}{position="dodge"},而要用position_dodge()设置不同组之间标签的避开宽度(dodging width)。

    ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) + 
      geom_col(aes(fill=year), position="dodge") + 
      geom_text(aes(label=proportion, y=proportion+0.05), position="dodge", vjust=0) 
    
    
    ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) + 
      geom_col(aes(fill=year), position="dodge") + 
      geom_text(aes(label=proportion, y=proportion+0.05), position=position_dodge(0.9), vjust=0) 
    
    
    21.png 22.png

    vjust问题

    vjust选项调节数据标签相对于条形顶部的位置。默认为0.5。

    
    ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) + 
      geom_col(aes(fill=year), position="dodge") + 
      geom_text(aes(label=proportion, y=proportion+0.05), position=position_dodge(0.9)) 
    
    ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) + 
      geom_col(aes(fill=year), position="dodge") + 
      geom_text(aes(label=proportion, y=proportion+0.05), position=position_dodge(0.9), vjust=0) 
    
    ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) + 
      geom_col(aes(fill=year), position="dodge") + 
      geom_text(aes(label=proportion, y=proportion+0.05), position=position_dodge(0.9), vjust=1) 
    
    
    31.png 32.png 33.png

    aes()问题

    \color{red}{y=proportion+0.05}目的是抬升标签的位置,使得标签脱离条形的顶部。默认是贴合。

    
    ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) + 
      geom_col(aes(fill=year), position="dodge") + 
      geom_text(aes(label=proportion), position=position_dodge(0.9), vjust=0) 
    
    ggplot(emptynest2, aes(x=nesttype, y=proportion, group=year)) + 
      geom_col(aes(fill=year), position="dodge") + 
      geom_text(aes(label=proportion, y=proportion+1), position=position_dodge(0.9), vjust=0) 
    
    
    41.png 42.png

    附录

    下面内容节选自geom_text()帮助文件。

    # Aligning labels and bars --------------------------------------------------
    df <- data.frame(
      x = factor(c(1, 1, 2, 2)),
      y = c(1, 3, 2, 1),
      grp = c("a", "b", "a", "b")
    )
    
    # ggplot2 doesn't know you want to give the labels the same virtual width
    # as the bars:
    ggplot(data = df, aes(x, y, group = grp)) +
      geom_col(aes(fill = grp), position = "dodge") +
      geom_text(aes(label = y), position = "dodge")
    # So tell it:
    ggplot(data = df, aes(x, y, group = grp)) +
      geom_col(aes(fill = grp), position = "dodge") +
      geom_text(aes(label = y), position = position_dodge(0.9))
    # Use you can't nudge and dodge text, so instead adjust the y position
    ggplot(data = df, aes(x, y, group = grp)) +
      geom_col(aes(fill = grp), position = "dodge") +
      geom_text(
        aes(label = y, y = y + 0.05),
        position = position_dodge(0.9),
        vjust = 0
      )
    
    
    # Justification -------------------------------------------------------------
    df <- data.frame(
      x = c(1, 1, 2, 2, 1.5),
      y = c(1, 2, 1, 2, 1.5),
      text = c("bottom-left", "bottom-right", "top-left", "top-right", "center")
    )
    ggplot(df, aes(x, y)) +
      geom_text(aes(label = text))
    ggplot(df, aes(x, y)) +
      geom_text(aes(label = text), vjust = "inward", hjust = "inward")
    
    
    51.png 52.png 53.png 61.png 62.png

    相关文章

      网友评论

        本文标题:ggplot2条形图添加数据标签

        本文链接:https://www.haomeiwen.com/subject/pytvlctx.html