簡體   English   中英

如何在保持單元格順序的同時從上到下更改ggplot中一個類別的位置?

[英]How to change the positioning of one category in ggplot from top to bottom while keeping the order of the cells?

雖然在我看來這是一個小問題,但我無法理解它。 我有以下三個主要變量的數據:i)教育(因子):代表三個不同的教育水平(1、2、3)。 ii) 份額(數字):代表該國每個教育水平的人口百分比。 iii) 國家(因素):代表 30 個國家。

I plot 每個國家/地區的教育份額,通過將教育水平最低的國家重新排序到教育水平最高的國家。 問題是當 ggplot plot 順序正確時,我希望下面的高中(紅色)的相同位置顯示在圖表的底部而不是頂部。 以中下(下)、中上(中)、上(上)的順序閱讀圖表更直觀。 有人可以指導一下嗎?

這是代碼:

x$lab <- as.character(x$cntry2)
x$country = as.numeric(x$cntry2)
x$educ2= x$educ
x$educ = as.integer(x$educ)
x$educ = as.factor(x$educ)


labs <- x[!duplicated(x$country),]
labs <- labs[,c('country','lab')]

country_order <- x %>% 
  filter(educ == 1) %>%
  mutate(country = fct_reorder(factor(country), share, .desc = FALSE)) %>% 
  pull(country) %>%
  levels()
df2 <- x %>%
  mutate(country = fct_relevel(factor(country), country_order))


ggplot(df2, aes(x=country, y=share)) + 
  geom_col(aes(fill=educ2), color = "black") +
  labs(fill= "educ") +   
  theme_classic() +
  xlab("Country")+ ylab("%") +
  scale_x_discrete(labels=labs$lab[match(country_order,labs$country)]) +
  theme( 
    legend.position="bottom",
  )

這是數據:

structure(list(educ = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
    3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
    3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L
    ), .Label = c("1", "2", "3"), class = "factor"), cntry2 = structure(c(1L, 
    1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 
    6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L, 
    11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L, 14L, 15L, 15L, 15L, 
    16L, 16L, 16L, 17L, 17L, 17L, 18L, 18L, 18L, 19L, 19L, 19L, 20L, 
    20L, 20L, 21L, 21L, 21L, 22L, 22L, 22L, 23L, 23L, 23L, 24L, 24L, 
    24L, 25L, 25L, 25L, 26L, 26L, 26L, 27L, 27L, 27L, 28L, 28L, 28L, 
    29L, 29L, 29L), .Label = c("AU", "BE", "BG", "CH", "CY", "CZ", 
    "DK", "EE", "ES", "FI", "FR", "GR", "HR", "HU", "IE", "IS", "IT", 
    "LT", "LU", "LV", "NL", "NO", "PO", "PT", "RO", "SE", "SK", "SV", 
    "UK"), class = "factor"), share = c(14.9585723390695, 64.8311026131294, 
    20.2103250478011, 20.3203525363306, 37.9050825638106, 41.7745648998589, 
    20.5482068669118, 58.6719831908696, 20.7798099422186, 11.0478359908884, 
    52.7334851936219, 36.2186788154898, 24.5876872718117, 41.546015359436, 
    33.8662973687524, 8.1806499751285, 77.2156358812801, 14.6037141435914, 
    18.43684842358, 44.6831364124597, 36.8800151639603, 13.0409077472315, 
    58.1917820678991, 28.7673101848694, 42.6745525429736, 24.1881534644693, 
    33.1372939925571, 16.4818187731737, 46.3084628894816, 37.2097183373447, 
    22.0091736220768, 47.7329122490413, 30.2579141288819, 31.6958715347475, 
    40.8370856615852, 27.4670428036673, 19.5171138871772, 65.7890314268108, 
    14.693854686012, 15.620426612099, 63.1486925776748, 21.2308808102263, 
    27.79203576455, 33.4878715125424, 38.7200927229075, 29.0666986564299, 
    41.950575815739, 28.9827255278311, 36.0270124068613, 47.1984225312789, 
    16.7745650618598, 8.18044159594323, 60.960664858964, 30.8588935450928, 
    37.0050817095017, 37.4766935985084, 25.5182246919899, 15.7399902739504, 
    59.1482759419216, 25.111733784128, 19.2624176167015, 43.4944817814291, 
    37.2431006018693, 17.6501727404436, 44.6784798840967, 37.6713473754597, 
    10.0113985848549, 69.2765124631697, 20.7120889519754, 64.5252051582649, 
    21.4536928487691, 14.021101992966, 21.8502998519559, 62.6627857375856, 
    15.4869144104584, 11.4840104928012, 55.3435190932938, 33.172470413905, 
    4.21056255594574, 74.1410176960683, 21.648419747986, 15.6869892409901, 
    61.3851490387442, 22.9278617202657, 14.2357801080394, 49.3703276303246, 
    36.393892261636), lab = c("AU", "AU", "AU", "BE", "BE", "BE", 
    "BG", "BG", "BG", "CH", "CH", "CH", "CY", "CY", "CY", "CZ", "CZ", 
    "CZ", "DK", "DK", "DK", "EE", "EE", "EE", "ES", "ES", "ES", "FI", 
    "FI", "FI", "FR", "FR", "FR", "GR", "GR", "GR", "HR", "HR", "HR", 
    "HU", "HU", "HU", "IE", "IE", "IE", "IS", "IS", "IS", "IT", "IT", 
    "IT", "LT", "LT", "LT", "LU", "LU", "LU", "LV", "LV", "LV", "NL", 
    "NL", "NL", "NO", "NO", "NO", "PO", "PO", "PO", "PT", "PT", "PT", 
    "RO", "RO", "RO", "SE", "SE", "SE", "SK", "SK", "SK", "SV", "SV", 
    "SV", "UK", "UK", "UK"), country = c(1, 1, 1, 2, 2, 2, 3, 3, 
    3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 
    10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 
    15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 
    22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 
    27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30), educ2 = structure(c(1L, 
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
    3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
    3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Below upper-secondary", 
    "Upper-secondary", "Tertiary"), class = "factor")), row.names = c(NA, 
    -87L), class = c("tbl_df", "tbl", "data.frame"))

您可以使用forcats::fct_rev

library(ggplot2)

ggplot(x, aes(x=country, y=share, 
              fill = forcats::fct_rev(factor(as.integer(educ2))))) + 
  geom_col(color = "black") +
  labs(fill= "educ")  +
  theme_classic() +
  xlab("Country")+ ylab("%") +
  theme(legend.position="bottom")

在此處輸入圖像描述

我已經刪除了用於過濾和標記 x 軸的代碼以保持簡單。 如果需要,您可以將其添加到您的帖子中。


要反轉顏色,您可以使用position = position_stack(reverse = TRUE)

ggplot(x, aes(x=country, y=share, 
              fill = factor(as.integer(educ2)))) + 
  geom_col(color = "black", position = position_stack(reverse = TRUE)) +
  labs(fill= "educ")  +
  theme_classic() +
  xlab("Country")+ ylab("%") +
  theme(legend.position="bottom")

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM