更改 R 中每个堆栈条形图中部分的顺序

时间:2021-01-25 09:00:02

标签: r ggplot2

我正在尝试将堆栈条形图中每个部分的顺序从每个 y 变量中的比例从最小更改为最大。 这是我的数据

A tibble: 76 x 5
Groups:   Hours.Type [17]
   Hours.Type                           Type.of.Control total  mean  freq
   <chr>                                <chr>           <int> <dbl> <dbl>
 1 Administrative Services Cost Centers City/County        46   9.2  36.8
 2 Administrative Services Cost Centers Investor            9   1.8   7.2
 3 Administrative Services Cost Centers Non-Profit         23   4.6  18.4
 4 Administrative Services Cost Centers State              47   9.4  37.6
 5 Aides & Orderlies                    City/County        46   9.2  20.3
 6 Aides & Orderlies                    District            3   1     1.3
 7 Aides & Orderlies                    Investor           67  13.4  29.5
 8 Aides & Orderlies                    Non-Profit         64  12.8  28.2
 9 Aides & Orderlies                    State              47   9.4  20.7
10 Ambulatory Cost Centers              City/County        52  10.4  14.3
# ... with 66 more rows

这是我绘制图形的代码。

ggplot(null_mean_Type.of.Control_Hours.Type, aes(x = Hours.Type, y = freq, fill = Type.of.Control)) +
  geom_bar(position= position_stack(), stat= "identity", width = 0.7) +
  theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank()) +
  geom_text(aes(x = Hours.Type ,y = freq, label=paste(freq, sep = " "), group = Type.of.Control), position = position_stack(), hjust = 1.5) +
  scale_fill_discrete(guide = guide_legend(reverse=TRUE)) +
  coord_flip() + #show the proportion of each TOC in every hours type 
  ggtitle("Proportion of T.O.C in every hours type (0 productivity)")

输出为 here

我曾尝试使用 reorder 但它最终像 this 并弹出“有 50 个或更多警告(使用警告()查看前 50 个)”的错误。警告信息都是这样的:

1: In mean.default(X[[i]], ...) :
  argument is not numeric or logical: returning NA

我尝试重新排序的代码是这样的:

ggplot(null_mean_Type.of.Control_Hours.Type, aes(x = Hours.Type, y = reorder(freq, Type.of.Control), fill = Type.of.Control)) + ...

0 个答案:

没有答案
相关问题