grid.arrange - 使网格更高/扩展

时间:2017-12-09 12:33:28

标签: r ggplot2

我很乐意请求你的帮助。我正在尝试扩展下面的图形网格,以便完全显示三个图形。无论我到哪里,我都找不到任何线索如何调整实际网格的大小,而不是其中的每个图块。

Graph is squeezed

这是我正在使用的代码:

p1 <- df %>%
    filter(df$economic_area == "Poorer areas") %>%
    group_by(cand_nm) %>%
    summarise(sum = sum(contb_receipt_amt)) %>%
    arrange(desc(sum)) %>%
     ggplot(aes(x = reorder(cand_nm, -sum), y=sum)) + geom_bar(stat = "identity") + 
      theme(axis.text.x = element_text(angle = 55, hjust = 1)) + 
      scale_y_continuous(labels = comma) + 
      labs(x='Candidate', 
           y='Recieved donations in USD', 
           title='Donations per candidate from poorer areas')

p2 <- df %>%
    filter(df$economic_area == "Middle income areas") %>%
    group_by(cand_nm) %>%
    summarise(sum = sum(contb_receipt_amt)) %>%
    arrange(desc(sum)) %>%
     ggplot(aes(x = reorder(cand_nm, -sum), y=sum)) + geom_bar(stat = "identity") + 
      theme(axis.text.x = element_text(angle = 55, hjust = 1)) + 
      scale_y_continuous(labels = comma) + 
      labs(x='Candidate', 
           y='Recieved donations in USD', 
           title='Donations per candidate from middle income areas')

p3 <- df %>%
    filter(df$economic_area == "Upper class areas") %>%
    group_by(cand_nm) %>%
    summarise(sum = sum(contb_receipt_amt)) %>%
    arrange(desc(sum)) %>%
     ggplot(aes(x = reorder(cand_nm, -sum), y=sum)) + geom_bar(stat = "identity") + 
      theme(axis.text.x = element_text(angle = 55, hjust = 1)) + 
      scale_y_continuous(labels = comma) + 
      labs(x='Candidate', 
           y='Recieved donations in USD', 
           title='Donations per candidate from upper class areas')

grid.arrange(p1, p2, p3, nrow = 3, heights = c(10, 0.65, 0.65))

非常感谢您的帮助和问候, Trgovec

编辑:

我也试过使用facet,但它不起作用。

p <- df %>%
  group_by(cand_nm) %>%
  summarise(sum = sum(contb_receipt_amt)) %>%
  arrange(desc(sum)) %>%
   ggplot(aes(x = reorder(cand_nm, -sum), y=sum)) + geom_bar(stat = "identity") + 
    theme(axis.text.x = element_text(angle = 55, hjust = 1)) + 
    scale_y_continuous(labels = comma) + 
    labs(x='Candidate', 
         y='Recieved donations in USD', 
         title='Recieved donations per candidate')

p + facet_grid(. ~ df$Median_household_income)

这是错误:

Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(536L, 1279L, 380L,  : 
  replacement has 618217 rows, data has 25

编辑: 这是可重现的数据集:

> dput(test)
structure(list(cand_nm = structure(c(4L, 4L, 20L, 23L, 1L, 20L, 
19L, 5L, 23L, 1L, 12L, 3L, 19L, 2L, 5L, 8L, 16L, 6L, 12L, 3L, 
4L, 15L, 24L, 22L, 11L, 2L, 17L, 6L, 9L, 17L, 11L, 19L, 8L, 16L, 
22L, 20L, 13L, 24L, 5L, 23L, 15L, 25L, 9L, 14L, 9L, 10L, 21L, 
13L, 1L, 10L, 18L, 12L, 21L, 14L, 2L, 7L, 17L, 16L, 25L, 22L, 
3L, 8L, 15L, 18L, 6L, 24L, 7L, 11L), .Label = c("Bush, Jeb", 
"Carson, Benjamin S.", "Christie, Christopher J.", "Clinton, Hillary Rodham", 
"Cruz, Rafael Edward 'Ted'", "Fiorina, Carly", "Gilmore, James S III", 
"Graham, Lindsey O.", "Huckabee, Mike", "Jindal, Bobby", "Johnson, Gary", 
"Kasich, John R.", "Lessig, Lawrence", "McMullin, Evan", "O'Malley, Martin Joseph", 
"Pataki, George E.", "Paul, Rand", "Perry, James R. (Rick)", 
"Rubio, Marco", "Sanders, Bernard", "Santorum, Richard J.", "Stein, Jill", 
"Trump, Donald J.", "Walker, Scott", "Webb, James Henry Jr."), class = "factor"), 
    economic_area = structure(c(2L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 
    3L, 3L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 3L, 1L, 2L, 2L, 
    2L, 2L, 3L, 2L, 3L, 2L, 3L, 3L, 1L, 3L, 3L, 3L, 1L, 2L, 3L, 
    1L, 1L, 3L, 2L, 3L, 2L, 1L, 2L, 2L, 3L, 1L, 3L, 2L, 1L, 3L, 
    3L, 1L, 3L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 2L, 1L
    ), .Label = c("Poorer areas", "Middle income areas", "Upper class areas"
    ), class = "factor"), sum = c(125925526.85, 16410009.07, 
    5622760.67, 4233067.76, 2551429, 1985456.25, 1626777.31, 
    1154448.33, 1104488.41, 860788, 621133.75, 587637, 566709.69, 
    436575.16, 358550.75, 271350.07, 227328.63, 218724.23, 211130.5, 
    211075, 197447.84, 195618, 192773, 191260.1, 167872.43, 161952.1, 
    141996.17, 121297.5, 92806, 80349.55, 70725.29, 66755, 63672, 
    62400, 58971, 48382.4, 48291.56, 44000, 43426.5, 38671.22, 
    37890, 29900, 28964, 21753, 20750, 19626.42, 16655.1, 15980.75, 
    15021, 12550, 12250, 12150, 9900, 8434.5, 6092, 5800, 5450, 
    4425.66, 4050, 2936, 2700, 2700, 2700, 1750, 1000, 1000, 
    600, 575)), row.names = c(NA, -68L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), vars = "cand_nm", drop = TRUE, .Names = c("cand_nm", 
"economic_area", "sum"), indices = list(c(4L, 9L, 48L), c(13L, 
25L, 54L), c(11L, 19L, 60L), c(0L, 1L, 20L), c(7L, 14L, 38L), 
    c(17L, 27L, 64L), c(55L, 66L), c(15L, 32L, 61L), c(28L, 42L, 
    44L), c(45L, 49L), c(24L, 30L, 67L), c(10L, 18L, 51L), c(36L, 
    47L), c(43L, 53L), c(21L, 40L, 62L), c(16L, 33L, 57L), c(26L, 
    29L, 56L), c(50L, 63L), c(6L, 12L, 31L), c(2L, 5L, 35L), 
    c(46L, 52L), c(23L, 34L, 59L), c(3L, 8L, 39L), c(22L, 37L, 
    65L), c(41L, 58L)), group_sizes = c(3L, 3L, 3L, 3L, 3L, 3L, 
2L, 3L, 3L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 
3L, 3L, 2L), biggest_group_size = 3L, labels = structure(list(
    cand_nm = structure(1:25, .Label = c("Bush, Jeb", "Carson, Benjamin S.", 
    "Christie, Christopher J.", "Clinton, Hillary Rodham", "Cruz, Rafael Edward 'Ted'", 
    "Fiorina, Carly", "Gilmore, James S III", "Graham, Lindsey O.", 
    "Huckabee, Mike", "Jindal, Bobby", "Johnson, Gary", "Kasich, John R.", 
    "Lessig, Lawrence", "McMullin, Evan", "O'Malley, Martin Joseph", 
    "Pataki, George E.", "Paul, Rand", "Perry, James R. (Rick)", 
    "Rubio, Marco", "Sanders, Bernard", "Santorum, Richard J.", 
    "Stein, Jill", "Trump, Donald J.", "Walker, Scott", "Webb, James Henry Jr."
    ), class = "factor")), row.names = c(NA, -25L), class = "data.frame", vars = "cand_nm", drop = TRUE, .Names = "cand_nm"))

我基本上想将这三个图组合成一个扩展图网格/页面:

1) enter image description here

2) enter image description here

3) enter image description here

0 个答案:

没有答案