I am making a plot of values over time for different counties. I want to group the counties by state. I tried to do this using facet_wrap:
ggplot(lg1, aes(x=decade,y=fips,fill=cat_total))
geom_raster() scale_fill_gradient(low="white", high="red", guide="colorbar")
facet_wrap(~st)
However, it split it into different images:
Is there a way to specify facet wrap so that it keeps them within the same image, grouped by otherwise like this:
Sample from dput:
structure(list(decade = structure(c(8L, 13L, 1L, 1L, 8L, 4L,
6L, 7L, 6L, 8L, 8L, 9L, 13L, 1L, 6L, 10L, 4L, 7L, 7L, 10L, 2L,
11L, 13L, 9L, 13L, 5L, 2L, 1L, 6L, 1L, 5L, 2L, 10L, 11L, 12L,
2L, 4L, 9L, 12L, 5L, 7L, 8L, 6L, 10L, 5L, 2L, 12L, 13L, 13L,
4L, 9L, 12L, 7L, 4L, 4L, 4L, 8L, 12L, 2L, 3L, 11L, 3L, 11L, 13L,
10L, 12L, 8L, 13L, 9L, 13L, 3L, 11L, 13L, 8L, 4L, 5L, 11L, 10L,
12L, 1L, 10L, 5L, 5L, 2L, 7L, 10L, 2L, 7L, 10L, 7L, 12L, 2L,
10L, 3L, 9L, 2L, 11L, 4L, 9L, 6L), levels = c("1900s", "1910s",
"1920s", "1930s", "1940s", "1950s", "1960s", "1970s", "1980s",
"1990s", "2000s", "2010s", "2020s"), class = "factor"), fips = c("54091",
"47161", "55073", "22053", "21035", "55007", "42071", "17005",
"29039", "37145", "21001", "22127", "39001", "48173", "29107",
"55005", "05047", "55003", "37197", "19125", "51117", "39097",
"05115", "54041", "42091", "47181", "55045", "26081", "21197",
"23005", "18173", "10001", "26067", "48111", "37073", "18117",
"48341", "28077", "39145", "24023", "40015", "45053", "20205",
"29179", "36025", "19113", "29223", "36047", "17013", "17101",
"18135", "48173", "20121", "13101", "01117", "01007", "42005",
"54059", "29119", "01007", "29227", "40057", "48257", "13201",
"13229", "21031", "24017", "22113", "23001", "47103", "29001",
"25021", "21179", "39075", "48227", "19057", "13089", "48235",
"01019", "39043", "20021", "01037", "42077", "24029", "13233",
"21157", "51047", "25025", "54061", "29181", "17145", "51073",
"39127", "13301", "13195", "20135", "13081", "21169", "18163",
"42061"), cat_total = c(51, 9, 17, 35, 48, 7, 46, 31, 27, 60,
53, 30, 10, 22, 21, 11, 35, 10, 56, 12, 33, 47, 6, 35, 10, 44,
17, 26, 54, 25, 38, 27, 22, 23, 60, 29, 16, 36, 50, 38, 19, 83,
24, 26, 33, 13, 41, 9, 7, 35, 31, 20, 18, 61, 52, 51, 46, 56,
22, 28, 26, 10, 42, 11, 49, 50, 53, 7, 25, 10, 13, 41, 11, 36,
23, 15, 63, 15, 58, 34, 21, 52, 38, 27, 61, 30, 31, 43, 35, 34,
40, 31, 33, 33, 48, 11, 64, 43, 34, 43), st = c("54", "47", "55",
"22", "21", "55", "42", "17", "29", "37", "21", "22", "39", "48",
"29", "55", "05", "55", "37", "19", "51", "39", "05", "54", "42",
"47", "55", "26", "21", "23", "18", "10", "26", "48", "37", "18",
"48", "28", "39", "24", "40", "45", "20", "29", "36", "19", "29",
"36", "17", "17", "18", "48", "20", "13", "01", "01", "42", "54",
"29", "01", "29", "40", "48", "13", "13", "21", "24", "22", "23",
"47", "29", "25", "21", "39", "48", "19", "13", "48", "01", "39",
"20", "01", "42", "24", "13", "21", "51", "25", "54", "29", "17",
"51", "39", "13", "13", "20", "13", "21", "18", "42"), fips_total = c(497,
505, 148, 439, 491, 118, 487, 367, 305, 604, 534, 440, 494, 233,
254, 126, 373, 121, 607, 187, 593, 444, 392, 512, 489, 542, 198,
263, 531, 350, 468, 515, 274, 148, 594, 456, 172, 518, 505, 491,
275, 695, 279, 379, 410, 192, 409, 465, 331, 434, 409, 233, 260,
695, 598, 591, 453, 555, 333, 591, 195, 245, 344, 675, 698, 510,
533, 451, 343, 576, 237, 413, 506, 436, 230, 233, 648, 250, 615,
388, 308, 622, 474, 510, 623, 481, 537, 410, 485, 413, 407, 560,
465, 674, 655, 170, 683, 530, 463, 468)), class = "data.frame", row.names = c(NA,
-100L))
CodePudding user response:
One option would be to use facet_grid
with space="free_y"
and some additional styling via theme
options, like removing the spacing between the panels.:
library(ggplot2)
ggplot(lg1, aes(x = decade, y = fips, fill = cat_total))
geom_raster()
scale_fill_gradient(low = "white", high = "red", guide = "colorbar")
facet_grid(st ~ ., scales = "free_y", space = "free_y", switch = "y")
theme(
panel.spacing.y = unit(0, "pt"),
axis.text.y = element_text(size = 6),
strip.placement = "outside",
strip.text.y.left = element_text(angle = 0),
strip.background.y = element_rect(color = "white")
)
labs(x = NULL, y = NULL)