Use geom_col() in ggplot2 to draw bars whose heights come directly from supplied numeric values. It is equivalent to geom_bar(stat = "identity"), making it useful for plotting counts or averages that have already been summarized. section 17.5.1 section 18.3
A typical workflow combines geom_col() with:
- A summarized data frame containing categorical x values and numeric y values. section 17.5.1
- fill aesthetics and scale functions to distinguish bar categories and label them. section 18.3
- position = "dodge" for side-by-side comparisons, facet_wrap() for separate panels, and coord_flip() for horizontal layouts. section 17.5.1 section 18.3
- geom_text() for displaying numeric labels directly on or above the bars. section 17.5.1
Plot average variances
After calculating average opening and closing variances and reshaping them into tblD, plot the supplied values with: ggplot(tblD, aes(x = avg, y = value, fill = avg)) + geom_col() + geom_text(aes(label = round(value, 2)), vjust = -0.2). The resulting chart compares the two averages directly and labels each bar to two decimal places. section 17.5.1
Build a horizontal pyramid
Use geom_col(width = .6) with season on x, cap on y, fill mapped to type, and coord_flip() to display real and inflation-adjusted NBA salary-cap values as a horizontal pyramid. section 18.3
p1 <- ggplot(new_cap, aes(x = season, y = cap, fill = type)) +
geom_col(width = .6) +
scale_y_continuous(breaks = breaks,
labels = labels) +
coord_flip() +
labs(title = "NBA Salary Cap History: 1984-85 to 2020-21",
x = "Season",
y = "Real or Adjusted Cap",
caption = "salary cap was introduced prior to the 1984-85 season") +
scale_fill_discrete(name = "",
labels = c("Adjusted Dollars", "Real Dollars")) +
theme(plot.title = element_text(face = "bold")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(legend.position = "bottom")
print(p1)
Browse a complete geom_col() specification showing width, continuous-axis labels, coordinate flipping, fill labels, and legend placement. section 18.3
