【问题标题】:R plot/graph: Layout text and numbers instead of plotting pointsR plot/graph:布局文本和数字而不是绘图点
【发布时间】:2019-02-27 20:13:49
【问题描述】:

我有一个名为 Lexisres 的数据框,其中包含群组和时期的出生、婚姻和死亡人数。

Lexisres <- structure(list(Cohort = c(1760L, 1760L, 1760L, 1760L, 1760L, 
1760L, 1770L, 1770L, 1770L, 1770L, 1770L, 1770L, 1780L, 1780L, 
1780L, 1780L, 1780L, 1780L, 1780L, 1780L, 1790L, 1790L, 1790L, 
1790L, 1790L, 1790L, 1790L, 1790L, 1790L, 1800L, 1800L, 1800L, 
1800L, 1800L, 1800L, 1800L, 1800L, 1800L, 1810L, 1810L, 1810L, 
1810L, 1810L, 1810L, 1810L, 1810L, 1820L, 1820L, 1820L, 1820L, 
1820L, 1820L, 1820L, 1830L, 1830L, 1830L, 1830L, 1830L, 1830L, 
1840L, 1840L, 1840L, 1840L, 1840L, 1850L, 1850L, 1850L, 1850L, 
1860L, 1860L, 1860L, 1870L, 1870L, 1870L, 1880L), Births = c(0L, 
0L, 0L, 0L, 0L, 0L, 8L, 8L, 8L, 8L, 8L, 8L, 69L, 69L, 69L, 69L, 
69L, 69L, 69L, 69L, 331L, 331L, 331L, 331L, 331L, 331L, 331L, 
331L, 331L, 472L, 472L, 472L, 472L, 472L, 472L, 472L, 472L, 472L, 
508L, 508L, 508L, 508L, 508L, 508L, 508L, 508L, 469L, 469L, 469L, 
469L, 469L, 469L, 469L, 550L, 550L, 550L, 550L, 550L, 550L, 595L, 
595L, 595L, 595L, 595L, 656L, 656L, 656L, 656L, 656L, 656L, 656L, 
361L, 361L, 361L, 1L), Period = c(1810L, 1820L, 1830L, 1840L, 
1850L, 1860L, 1810L, 1820L, 1830L, 1840L, 1850L, 1860L, 1810L, 
1820L, 1830L, 1840L, 1850L, 1860L, 1870L, 1880L, 1810L, 1820L, 
1830L, 1840L, 1850L, 1860L, 1870L, 1880L, 1890L, 1810L, 1820L, 
1830L, 1840L, 1850L, 1860L, 1870L, 1880L, 1890L, 1820L, 1830L, 
1840L, 1850L, 1860L, 1870L, 1880L, 1890L, 1830L, 1840L, 1850L, 
1860L, 1870L, 1880L, 1890L, 1840L, 1850L, 1860L, 1870L, 1880L, 
1890L, 1850L, 1860L, 1870L, 1880L, 1890L, 1860L, 1870L, 1880L, 
1890L, 1870L, 1880L, 1890L, 1880L, 1890L, 1890L, 1890L), Marriages = c(0L, 
0L, 0L, 0L, 0L, 0L, 3L, 5L, 0L, 0L, 0L, 0L, 32L, 34L, 3L, 0L, 
0L, 0L, 0L, 0L, 67L, 236L, 24L, 4L, 0L, 0L, 0L, 0L, 0L, 1L, 160L, 
272L, 35L, 4L, 0L, 0L, 0L, 0L, 1L, 207L, 251L, 45L, 4L, 0L, 0L, 
0L, 0L, 137L, 296L, 34L, 2L, 0L, 0L, 1L, 184L, 330L, 35L, 0L, 
0L, 2L, 255L, 289L, 49L, 0L, 0L, 174L, 429L, 53L, 0L, 232L, 538L, 
0L, 0L, 361L, 1L), Deaths = c(5L, 12L, 13L, 17L, 5L, 3L, 13L, 
25L, 24L, 44L, 33L, 6L, 13L, 21L, 28L, 39L, 40L, 44L, 9L, 1L, 
22L, 37L, 71L, 65L, 61L, 112L, 96L, 30L, 1L, 1L, 45L, 95L, 69L, 
81L, 117L, 130L, 121L, 25L, 5L, 63L, 73L, 80L, 82L, 114L, 144L, 
106L, 5L, 67L, 46L, 69L, 91L, 108L, 143L, 2L, 48L, 67L, 80L, 
89L, 120L, 2L, 54L, 83L, 79L, 82L, 5L, 56L, 62L, 99L, 3L, 43L, 
72L, 1L, 35L, 0L, 1L)), .Names = c("Cohort", "Births", "Period", 
"Marriages", "Deaths"), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -75L), spec = structure(list(cols = structure(list(
    Cohort = structure(list(), class = c("collector_integer", 
    "collector")), Births = structure(list(), class = c("collector_integer", 
    "collector")), Period = structure(list(), class = c("collector_integer", 
    "collector")), Marriages = structure(list(), class = c("collector_integer", 
    "collector")), Deaths = structure(list(), class = c("collector_integer", 
    "collector"))), .Names = c("Cohort", "Births", "Period", 
"Marriages", "Deaths")), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"))

现在我想在 y 轴上按 Cohort 绘制它,在 x 轴上按 Period 绘制它,并将所有三个事件计数分别写为一个数据点。换句话说,我想在这里填充这个简单的情节

plot(Lexisres$Period, Lexisres$Cohort)

在每个点上都有各自的出生、婚姻和死亡人数 - 把它们写下来。

可能是这样的:

我该怎么做?

【问题讨论】:

    标签: r plot text layout label


    【解决方案1】:
    # Aggregating data
    library(dplyr)
    df <- df %>% group_by(Cohort, Period) %>% summarise(Births = sum(Births), Marriages = sum(Marriages), Deaths = sum(Deaths))
    
    # Adding square and text coordinates
    df$xmin <- df$Cohort
    df$xmax <- df$Cohort+10
    df$ymin <- df$Period
    df$ymax <- df$Period+10
    df$x_center <- (df$xmax+df$xmin)/2
    df$y_center <- (df$ymax+df$ymin)/2
    
    # Plotting five rectangles with lables
    ggplot() + 
      scale_x_continuous(name="Cohort") + 
      scale_y_continuous(name="Period") +
      geom_rect(data=df, mapping=aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), color="blue", alpha=0) +
      geom_text(data=df, aes(x_center, y_center, label=paste0("Births: ", Births, "\n", "Marriages: ", Marriages,"\n", "Deaths: ", Deaths)), size=2.5) + 
      theme_bw()
    

    注意

    根据请求,变量被粘贴而不是求和。

    df <- df %>% group_by(Cohort, Period) %>% summarise(Births = paste(Births, collapse =","), Marriages = paste(Marriages, collapse =","), Deaths = paste(Deaths, collapse =","))
    

    使用的数据:

    # Placing data in dataframe
    df <- structure(list(Cohort = c(1760L, 1760L, 1760L, 1760L, 1760L, 
                              1760L, 1770L, 1770L, 1770L, 1770L, 1770L, 1770L, 1780L, 1780L, 
                              1780L, 1780L, 1780L, 1780L, 1780L, 1780L, 1790L, 1790L, 1790L, 
                              1790L, 1790L, 1790L, 1790L, 1790L, 1790L, 1800L, 1800L, 1800L, 
                              1800L, 1800L, 1800L, 1800L, 1800L, 1800L, 1810L, 1810L, 1810L, 
                              1810L, 1810L, 1810L, 1810L, 1810L, 1820L, 1820L, 1820L, 1820L, 
                              1820L, 1820L, 1820L, 1830L, 1830L, 1830L, 1830L, 1830L, 1830L, 
                              1840L, 1840L, 1840L, 1840L, 1840L, 1850L, 1850L, 1850L, 1850L, 
                              1860L, 1860L, 1860L, 1870L, 1870L, 1870L, 1880L), Births = c(0L, 
                                                                                           0L, 0L, 0L, 0L, 0L, 8L, 8L, 8L, 8L, 8L, 8L, 69L, 69L, 69L, 69L, 
                                                                                           69L, 69L, 69L, 69L, 331L, 331L, 331L, 331L, 331L, 331L, 331L, 
                                                                                           331L, 331L, 472L, 472L, 472L, 472L, 472L, 472L, 472L, 472L, 472L, 
                                                                                           508L, 508L, 508L, 508L, 508L, 508L, 508L, 508L, 469L, 469L, 469L, 
                                                                                           469L, 469L, 469L, 469L, 550L, 550L, 550L, 550L, 550L, 550L, 595L, 
                                                                                           595L, 595L, 595L, 595L, 656L, 656L, 656L, 656L, 656L, 656L, 656L, 
                                                                                           361L, 361L, 361L, 1L), Period = c(1810L, 1820L, 1830L, 1840L, 
                                                                                                                             1850L, 1860L, 1810L, 1820L, 1830L, 1840L, 1850L, 1860L, 1810L, 
                                                                                                                             1820L, 1830L, 1840L, 1850L, 1860L, 1870L, 1880L, 1810L, 1820L, 
                                                                                                                             1830L, 1840L, 1850L, 1860L, 1870L, 1880L, 1890L, 1810L, 1820L, 
                                                                                                                             1830L, 1840L, 1850L, 1860L, 1870L, 1880L, 1890L, 1820L, 1830L, 
                                                                                                                             1840L, 1850L, 1860L, 1870L, 1880L, 1890L, 1830L, 1840L, 1850L, 
                                                                                                                             1860L, 1870L, 1880L, 1890L, 1840L, 1850L, 1860L, 1870L, 1880L, 
                                                                                                                             1890L, 1850L, 1860L, 1870L, 1880L, 1890L, 1860L, 1870L, 1880L, 
                                                                                                                             1890L, 1870L, 1880L, 1890L, 1880L, 1890L, 1890L, 1890L), Marriages = c(0L, 
                                                                                                                                                                                                    0L, 0L, 0L, 0L, 0L, 3L, 5L, 0L, 0L, 0L, 0L, 32L, 34L, 3L, 0L, 
                                                                                                                                                                                                    0L, 0L, 0L, 0L, 67L, 236L, 24L, 4L, 0L, 0L, 0L, 0L, 0L, 1L, 160L, 
                                                                                                                                                                                                    272L, 35L, 4L, 0L, 0L, 0L, 0L, 1L, 207L, 251L, 45L, 4L, 0L, 0L, 
                                                                                                                                                                                                    0L, 0L, 137L, 296L, 34L, 2L, 0L, 0L, 1L, 184L, 330L, 35L, 0L, 
                                                                                                                                                                                                    0L, 2L, 255L, 289L, 49L, 0L, 0L, 174L, 429L, 53L, 0L, 232L, 538L, 
                                                                                                                                                                                                    0L, 0L, 361L, 1L), Deaths = c(5L, 12L, 13L, 17L, 5L, 3L, 13L, 
                                                                                                                                                                                                                                  25L, 24L, 44L, 33L, 6L, 13L, 21L, 28L, 39L, 40L, 44L, 9L, 1L, 
                                                                                                                                                                                                                                  22L, 37L, 71L, 65L, 61L, 112L, 96L, 30L, 1L, 1L, 45L, 95L, 69L, 
                                                                                                                                                                                                                                  81L, 117L, 130L, 121L, 25L, 5L, 63L, 73L, 80L, 82L, 114L, 144L, 
                                                                                                                                                                                                                                  106L, 5L, 67L, 46L, 69L, 91L, 108L, 143L, 2L, 48L, 67L, 80L, 
                                                                                                                                                                                                                                  89L, 120L, 2L, 54L, 83L, 79L, 82L, 5L, 56L, 62L, 99L, 3L, 43L, 
                                                                                                                                                                                                                                  72L, 1L, 35L, 0L, 1L)), .Names = c("Cohort", "Births", "Period", 
                                                                                                                                                                                                                                                                     "Marriages", "Deaths"), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                                                                     ), row.names = c(NA, -75L), spec = structure(list(cols = structure(list(
                                                                                                                                                                                                                                                                       Cohort = structure(list(), class = c("collector_integer", 
                                                                                                                                                                                                                                                                                                            "collector")), Births = structure(list(), class = c("collector_integer", 
                                                                                                                                                                                                                                                                                                                                                                "collector")), Period = structure(list(), class = c("collector_integer", 
                                                                                                                                                                                                                                                                                                                                                                                                                    "collector")), Marriages = structure(list(), class = c("collector_integer", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                           "collector")), Deaths = structure(list(), class = c("collector_integer", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               "collector"))), .Names = c("Cohort", "Births", "Period", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          "Marriages", "Deaths")), default = structure(list(), class = c("collector_guess", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         "collector"))), .Names = c("cols", "default"), class = "col_spec"))
    

    【讨论】:

    • 我采用企鹅解决方案:ggplot(data = Lexisres) + geom_blank(aes(x = Period, y = Cohort)) + geom_text(aes(x = Period, y = Cohort, label = paste( "\n死亡: ", 死亡, "\n婚姻: ", 婚姻)), size = 3.25, check_overlap = TRUE) + scale_x_continuous(breaks=seq(0, 1900, 10)) + scale_y_continuous(breaks=seq(0, 1880) , 10)) + theme_bw()
    • 允许跟进?我将如何包含第一列,该列显示的出生数不是针对每个群组时期组合,而是仅针对每个群组?换句话说,我想添加一个列来显示每个队列的出生人数,因为这些出生人数在一段时间内不会发生变化。
    • 现在你的 Esben 看起来好多了。
    • 我实现了将新生儿放在前面,就像我想要的那样。但是,现在其余的都被粉碎了... ggplot(data = Lexisres) + geom_blank(aes(x = Period, y = Cohort)) + geom_text(aes(x = 0, y = Cohort, label = paste("\nBirths : ", Births))) + scale_x_continuous(breaks=seq(0, 1900, 10)) + geom_text(aes(x = Period, y = Cohort, label = paste("\nDeaths: ", Deaths, "\nMarriages: ", 婚姻)), size = 3.25, check_overlap = TRUE) + scale_x_continuous(breaks=seq(0, 1900, 10)) + scale_y_continuous(breaks=seq(0, 1880, 10)) + theme_bw()
    • @Bengale 我已经添加了您需要更改以获得所需输出的一行。
    【解决方案2】:

    我建议在此图表中使用 ggplot2

    请看下面的查询:

    ggplot(data = Lexisres) + 
    geom_text(aes(x = Period, 
                  y = Cohort, 
                  label = paste("B: ", Births, 
                               "\nD: ", Deaths, 
                               "\nM: ", Marriages)), 
                                size = 1.5)
    

    【讨论】:

      【解决方案3】:

      另一个解决方案展示了如何使用base R plot()

      按问题要求显示:

      1. “y 轴上的队列,x 轴上的周期”
      2. “每个点的出生、婚姻和死亡计数 - 字面上写下来

      更新的出生、婚姻、死亡总数的解决方案(因为原始数据未预先汇总)


      # Aggregate data: sum all columns, group by Cohort+Period 
      df <- aggregate(. ~ Cohort+Period, df, sum) 
      
      tx=0.8  #variable to set the text scale for our labels
      
      # Variable to tweak the x axis length on either side (by an extra half distance) 
      extend_x <- diff(sort(unique(df$Period))[1:2])/2  # (difference of first two sorted(unique(x)) ) / 2
      
      # Start a new plot
      plot.new() 
      
      # Set up x and y limits manually so we can control where they start & end  
      plot.window(xlim = c( min(df$Period)-extend_x, max(df$Period)+extend_x), # x axis limits
                  ylim = range(df$Cohort),  # y axis limits 
                  xaxs = "i")  # xaxs = "i" sets x-axis to internal = match the original data range (avoids default, xaxs="r", that adds 4% to either side)
      
      # Put text at each x, y point 
      text( x=df$Period, y=df$Cohort, paste0(df$Births,":B"),    cex=tx, adj=c( 1.1, -0.1))  # 1=right justified, 0=align top (tweak by decimals)
      text( x=df$Period, y=df$Cohort, paste0("M:",df$Marriages), cex=tx, adj=c(-0.1,  0.4))  # 0=left justified, 0.5=align middle (tweak by decimals)
      text( x=df$Period, y=df$Cohort, paste0(df$Deaths,":D"),    cex=tx, adj=c( 1.1,  1.4))  # 1=right justified, 1=align bottom (tweak by decimals)
      
      # Axis labels
      axis(1, at=unique(df$Period))        # x-axis labels at all marks
      axis(2, at=unique(df$Cohort),las=1)  # y-axis labels at all marks, `las=1` rotates the label to be horizontal
      
      # Add x & y axis labels  
      title(xlab="Period", ylab="Cohort")  
      
      # Add grid lines
      grid(length(unique(df$Period))+0,length(unique(df$Cohort))+0) 
      
      box()  # draw a box around the plot 
      

      【讨论】:

      • 更新了出生、婚姻、死亡总和的解决方案(因为原始数据未针对 1 对 x/y 点进行预聚合)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多