创建的列geom_col
使用position_stack
which 分别堆叠正值和负值,其中正值向上堆叠,负值向下堆叠。在此示例中,中心组Neutral
通过将其设置为等于其原始值的一半,然后将其绘制为正值和负值来跨越 0。此外,对于正值,组的顺序需要颠倒。
这种方法有助于展示我使用的一些调查的结果,因此我将其制成一个函数以使其更通用。
library(tidyverse)
#
# summarize groups and save counts in variable quality_cnt
#
diamonds_cnt <- diamonds %>%
mutate(quality = fct_recode(cut, "Very_Poor" = "Fair", "Poor" = "Good",
"Neutral" = "Very Good", "Good" = "Premium", "Excellent" = "Ideal")) %>%
select(color, clarity, quality) %>%
group_by(color, clarity, quality) %>% summarize(quality_cnt = n())
# make function to plot counts
plot_ratings <- function(survey, rated_item, rating_cnt, rating, rating_cat, facet = "wrap") {
#
# Input:
# rated_item = unquoted variable name of rated items
# rating = unquoted variable name of ratings for each rated_items;
# variable should be a factor ordered from lowest to highest
# rating_cnt = unquoted variable name of counts or frequencies for each rated_item
# rated_cat = unquoted variable name of categories of rated items
# facet = "grid" for all panels on one row or
# "wrap" to spread panels across multiple rows
#
# make arguments quosures
#
rated_item <- enquo(rated_item)
rating_cnt <- enquo(rating_cnt)
rating <- enquo(rating)
rating_cat <- enquo(rating_cat)
#
# If number of rating levels is odd, find middle rating
#
rating_levels <- levels(pull(survey, !!rating))
mid_level <- ceiling(length(rating_levels)/2)
mid_rating <- ifelse(length(rating_levels)%%2 == 1, rating_levels[mid_level], NA_character_)
#
# make local variabels for use with aes
# plot positive and negative columns separately
#
survey <- survey %>% mutate( rating_plt = !!rating, rating_cnt_plt = !!rating_cnt)
sp <- ggplot(survey, aes_(x = rated_item, fill = rating)) +
geom_col(data=filter(survey, !!rating %in% tail(rating_levels, mid_level)),
aes( y = ifelse(rating_plt == mid_rating, .5*rating_cnt_plt, rating_cnt_plt)),
position = position_stack(reverse = TRUE )) +
geom_col(data=filter(survey, !!rating %in% head(rating_levels, mid_level)),
aes( y = ifelse(rating_plt == mid_rating, -.5*rating_cnt_plt, -rating_cnt_plt)),
position = "stack") +
labs(y = rating_cnt) +
scale_fill_brewer(palette = "RdYlGn", direction = -1) +
coord_flip() +
switch(facet,
grid = facet_grid( facets=rating_cat, scales = "free_x"),
wrap = facet_wrap( facets=rating_cat, scales = "free_x"))
plot(sp)
}
#
# Use function to make charts
#
plot_ratings(diamonds_cnt, rated_item = color, rating_cnt = quality_cnt,
rating = quality, rating_cat = clarity, facet = "wrap")
这给出了图表