本文介绍了使用dplyr和gglot包括负值的分面水平发散堆叠条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这个例子是清楚的。我想要中间条跨度为‘0’的堆叠条,因为它代表一个中性值。这与Likert刻度一起使用。为了重现性,我使用了钻石数据集。

下面的示例足够接近我的用例,并且演示了我很难将"好的"或"正的"数据按正确的顺序排列(因此中性数据最接近于0)。

以下是我的代码:

require(tidyverse)

diamonds_new <- 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) %>% count()

diamonds_bad <-
  diamonds_new %>% filter(quality %in% c("Very poor", "Poor", "Neutral")) %>%
  mutate(n = ifelse(quality == "Neutral", -n/2, -n))

diamonds_good <-
  diamonds_new %>% filter(quality %in% c("Neutral", "Good", "Excellent")) %>%
  mutate(n = ifelse(quality == "Neutral", n/2, n)) # %>%
#  arrange(color, clarity, desc(quality))  # this doesn't seem to make a difference

ggplot() + geom_col(data = diamonds_bad, aes(x=color, y = n, fill = quality)) +
  geom_col(data = diamonds_good, aes(x=color, y = n, fill = quality)) +
  facet_grid(. ~ clarity, scales = "free") +
  coord_flip()

我也尝试过使用scale_fill_manual(),但也没有找到有效的方法。

我认为这比现有的一些示例要复杂得多,这些示例没有负值的复杂性,也不需要span 0。使用当前版本的gglot,我遗漏了什么?

另外,正集和负集需要拆分,或者至少这样做更容易,我说的对吗?

推荐答案

创建的列由分别堆叠正值和负值的position_stack形成,其中正值向上堆叠,负值向下堆叠。通过将中心组设置为其原始值的一半,然后将其绘制为正值和负值,使中心组(本例中的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")

这给出了图表

这篇关于使用dplyr和gglot包括负值的分面水平发散堆叠条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 19:33