1

我想制作一个bar chart突出不同类别的内容 - 我geom_col为每个国家/地区使用一个方面。

问题是彩色版本与'greyed'特定国家的版本不一致;相反,它总是在图表的底部。

钢铁生产图表

使用此代码生成:


# Steel production data
  steel <- tribble(
    ~country,   ~"2016",    ~"2017",    ~"2018",    ~"2019",
    "China",     828.4,  853.7,  863.9,  988.2, 
    "Japan",     104.9,  104.7,  104.2,  99.6, 
    "India",     95.0,   101.5,  107.8,  111.5, 
    "USA",     80.2,     81.6,   84.2,   88.2, 
    "Other",     564.8,  577.7,  587.8,  549.9 
  )

# Pivot the data and turn country into factors
  steel_long <- tidyr::pivot_longer(steel, -country, names_to = "year", values_to = "production")
  names(steel_long) <- tolower(names(steel_long))

  steel_long$country <- as.factor(steel_long$country)
  steel_long$country <- forcats::fct_relevel(steel_long$country, "Other", after = Inf) # Always put RotW last

  steel_long$country2 <- steel_long$country # Add second country to add the grey lines on the mini charts

  steel_long$year <- lubridate::make_date(year = steel_long$year, 12, 31)

# Graph - Column
  ggplot() +
    geom_col(data = steel_long[, 2:4], 
             mapping = aes(x = year, y = production, group = country2), colour = "white", fill = "grey", lwd = 1) +
    geom_col(data = steel_long, mapping = aes(x = year, y = production, fill = country), lwd = 1.1) +
    facet_wrap(~country) +
    labs(title = "Global steel production (Source: World Steel Association)", x = "", y = "Million metric tons") +
    guides(fill = "none") +
    theme_minimal()

是否可以为与国家相关的列的特定区域着色?

谢谢

4

1 回答 1

1

尝试这个。基本思想是根据国家的数量复制数据集。分面时,数据集根据分面变量进行拆分。通过复制数据集,我们确保每个方面的列图由整个数据集组成。单个数据集之间的唯一区别country_fill是用于为国家/地区设置颜色以突出显示的列,同时将所有其他数据集的填充颜色设置为灰色。要在我使用的图中设置填充颜色scale_fill_identity

library(tidyverse)

# Steel production data
steel <- tribble(
  ~country,   ~"2016",    ~"2017",    ~"2018",    ~"2019",
  "China",     828.4,  853.7,  863.9,  988.2, 
  "Japan",     104.9,  104.7,  104.2,  99.6, 
  "India",     95.0,   101.5,  107.8,  111.5, 
  "USA",     80.2,     81.6,   84.2,   88.2, 
  "Other",     564.8,  577.7,  587.8,  549.9 
)

# Pivot the data and turn country into factors
steel_long <- tidyr::pivot_longer(steel, -country, names_to = "year", values_to = "production")
names(steel_long) <- tolower(names(steel_long))

steel_long$country <- as.factor(steel_long$country)
steel_long$country <- forcats::fct_relevel(steel_long$country, "Other", after = Inf) # Always put RotW last

steel_long$country2 <- steel_long$country # Add second country to add the grey lines on the mini charts

steel_long$year <- lubridate::make_date(year = steel_long$year, 12, 31)

# Colors
colors <- scales::hue_pal()(5) %>% 
  setNames(unique(steel_long$country)) %>% 
  tibble::enframe(name = "country3", value = "country_fill")

# Replicate dataframe
steel_long_rep <- purrr::map(unique(steel_long$country), ~ steel_long) %>% 
  setNames(unique(steel_long$country)) %>% 
  bind_rows(.id = "country3") %>%
  # Join colors
  left_join(colors) %>% 
  # Set fill for non-facet countries to grey
  mutate(country_fill = ifelse(country != country3, "grey", country_fill))
#> Joining, by = "country3"

steel_long_rep$country3 <- forcats::fct_relevel(steel_long_rep$country3, "Other", after = Inf) 

# Graph - Column
ggplot() +
  geom_col(data = steel_long_rep, mapping = aes(x = year, y = production, group = country, fill = country_fill), colour = "white", lwd = 1) +
  scale_fill_identity() +
  facet_wrap(~country3) +
  labs(title = "Global steel production (Source: World Steel Association)", x = "", y = "Million metric tons") +
  guides(fill = "none") +
  theme_minimal()

reprex 包(v0.3.0)于 2020-04-12 创建

于 2020-04-12T19:09:19.577 回答