Bar charts in R
geom_col takes the heights you supply; geom_bar counts rows for you.
library(ggplot2)
library(dplyr)
steps <- tibble(
lang = c("python", "rust", "c", "go", "r"),
steps = c(364, 181, 165, 128, 138)
)
ranked <- ggplot(steps, aes(x = reorder(lang, steps), y = steps)) +
geom_col(fill = "#7c5cff") +
coord_flip() +
labs(x = NULL, y = "steps")
counts <- tibble(lang = c("r", "r", "python"), stars = c(3L, 2L, 3L))
stacked <- ggplot(counts, aes(lang, fill = factor(stars))) +
geom_bar(position = "dodge")
print(class(ranked)[1])
ggsave(tempfile(fileext = ".png"), stacked, width = 4, height = 3, dpi = 100)
How it works
geom_colneeds a y;geom_barcomputes one.positiondecides stacked, dodged or filled.reordersorts the categories by the value.
Keywords and builtins used here
aescclasscoord_flipfactorgeom_bargeom_colggplotggsavelabslibraryprintreordertempfiletibble
The run, in numbers
- Lines
- 19
- Characters to type
- 535
- Tokens
- 182
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 109 seconds.
Step 1 of 4 in Geometries, step 4 of 15 in ggplot2.