将geom_col按特定顺序排列

huangapple go评论66阅读模式
英文:

Putting geom_col in specific order

问题

我有一些(尽管有点混乱)的数据,正在尝试使用以下代码将其整理成一个图表。有两个问题困扰着我。

1)我想要更改图表中test_level的显示方式,使得0显示为"Test Under",1显示为"Test Medium",2显示为"Test Above Avg",3显示为"Test Excellent"。我尝试创建一个包含这些标签的向量,并在facet_grid()中使用(labeller = as_labeller(facet_labels))进行强制转换,但由于某种原因它不起作用。

2)我一直试图重新排序'var',使得'var'的值按照以下顺序在图表中显示(从上到下):
White
Black
Hispanic
Other Race
Male
Female
Less than HS
Some HS
Graduate HS
Some College or Technical School
Graduate College or Higher

我最初尝试创建一个新变量,实际上是一个具有这种顺序的11级因子变量,但可能有更简洁的方法来实现这一点(如果这种方法能够正常工作的话;我选择放弃它,选择在这里咨询)。

代码:

ggplot(df, aes(x = percent, y = var_factor, fill = group_factor_ordered)) +
  geom_col(orientation = "y", 
           color = "black") +
  facet_grid(group_factor_ordered ~ test_level,
             scales = "free", space = "free_y") +
  labs(title = "Demographic breakdown of Testing Level",
         y = "",
         x = "Percent") +
  theme_minimal() +
  theme(legend.position = "none",
        strip.text.y = element_blank())

数据:

df <- structure(list(group = c("gender", "gender", "gender", "gender", 
"gender", "gender", "gender", "gender", "p_educ", "p_educ", "p_educ", 
"p_educ", "p_educ", "p_educ", "p_educ", "p_educ", "p_educ", "p_educ", 
"p_educ", "p_educ", "p_educ", "p_educ", "p_educ", "p_educ", "p_educ", 
"p_educ", "p_educ", "p_educ", "race", "race", "race", "race", "race", 
"race", "race", "race", "race", "race", "race", "race", "race", "race", 
"race", "race"), levels = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L), .Label = c("1", "2", "3", "4", "5"), class = "factor"), 
    test_level = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
    2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
    2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
    1L, 2L, 3L, 4L), .Label = c("0", "1", "2", "3"), class = "factor"), 
    percent = c(1.7, 13.6, 17.6, 67, 1.6, 11.3, 14.9, 72.1, 4, 
    21.2, 13.5, 61.3, 2.7, 19.1, 19.9, 58.3, 0.8, 16.6, 18, 64.7, 
    1, 5.8, 17.3, 75.8, 1, 6.5, 92.5, 1, 5.8, 12.6, 80.6, 1.8, 
    13.3, 18.5, 66.4, 1.8, 14.6, 15.3, 68.3, 1, 7.7, 16.1, 75.2
    ), var = c("Female", "Female", "Female", "Female", "Male", 
    "Male", "Male", "Male", "Less than HS", "Less than HS", "Less than HS", 
    "Less than HS", "Some HS", "Some HS", "Some HS", "Some HS", 
    "Graduate HS", "Graduate HS", "Graduate HS", "Graduate HS", 
    "Some College or\nTechnical School", "Some College or\nTechnical School", 
    "Some College or\nTechnical School", "Some College or\nTechnical School", 
    "Graduate College\nor Higher", "Graduate College\nor Higher", 
    "Graduate College\nor Higher", "White", "White", "White", 
    "White", "Black", "Black", "Black", "Black", "Hispanic", 
    "Hispanic", "Hispanic", "Hispanic", "Other Race", "Other Race", 
    "Other Race", "Other Race"), group_factor = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L,

<details>
<summary>英文:</summary>

I have some (admittedly chaotic) data and am trying to wrangle it into a figure using the code below. There are 2 things that I am struggling with. 

1) I want to change how test_level is displayed in the figure, so that 0 is &quot;Test Under&quot;, 1 is &quot;Test Medium&quot;, 2 is &quot;Test Above Avg&quot;, and 3 is &quot;Test Excellent&quot;. I tried creating a vector of these labels and coercing them using ```(labeller = as_labeller(facet_labels)``` within ```facet_grid()``` but it wouldn&#39;t take for some reason. 

2) I have been trying and failing to reorder &#39;var&#39; so that the &#39;var&#39; values display in the figure (from top to bottom) in this order:  
White  
Black  
Hispanic  
Other Race  
Male  
Female  
Less than HS  
Some HS  
Graduate HS  
Some College or Technical School  
Graduate College or Higher  

I was originally trying to create a new variable that was essentially an 11-level factor variable with that order, but there must be a more succinct way to achieve this (if that would even work at all; I abandoned it in favor of asking here). 




Code: 

ggplot(df, aes(x = percent, y = var_factor, fill = group_factor_ordered)) +
geom_col(orientation = "y",
color = "black") +
facet_grid(group_factor_ordered ~ test_level,
scales = "free", space = "free_y") +
labs(title = "Demographic breakdown of Testing Level",
y = "",
x = "Percent") +
theme_minimal() +
theme(legend.position = "none",
strip.text.y = element_blank())


Data:

df <- structure(list(group = c("gender", "gender", "gender", "gender",
"gender", "gender", "gender", "gender", "p_educ", "p_educ", "p_educ",
"p_educ", "p_educ", "p_educ", "p_educ", "p_educ", "p_educ", "p_educ",
"p_educ", "p_educ", "p_educ", "p_educ", "p_educ", "p_educ", "p_educ",
"p_educ", "p_educ", "race", "race", "race", "race", "race", "race",
"race", "race", "race", "race", "race", "race", "race", "race",
"race", "race"), levels = structure(c(1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L), .Label = c("1", "2", "3", "4", "5"), class = "factor"),
test_level = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L), .Label = c("0", "1", "2", "3"), class = "factor"),
percent = c(1.7, 13.6, 17.6, 67, 1.6, 11.3, 14.9, 72.1, 4,
21.2, 13.5, 61.3, 2.7, 19.1, 19.9, 58.3, 0.8, 16.6, 18, 64.7,
1, 5.8, 17.3, 75.8, 1, 6.5, 92.5, 1, 5.8, 12.6, 80.6, 1.8,
13.3, 18.5, 66.4, 1.8, 14.6, 15.3, 68.3, 1, 7.7, 16.1, 75.2
), var = c("Female", "Female", "Female", "Female", "Male",
"Male", "Male", "Male", "Less than HS", "Less than HS", "Less than HS",
"Less than HS", "Some HS", "Some HS", "Some HS", "Some HS",
"Graduate HS", "Graduate HS", "Graduate HS", "Graduate HS",
"Some College or\nTechnical School", "Some College or\nTechnical School",
"Some College or\nTechnical School", "Some College or\nTechnical School",
"Graduate College\nor Higher", "Graduate College\nor Higher",
"Graduate College\nor Higher", "White", "White", "White",
"White", "Black", "Black", "Black", "Black", "Hispanic",
"Hispanic", "Hispanic", "Hispanic", "Other Race", "Other Race",
"Other Race", "Other Race"), group_factor = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("gender",
"p_educ", "race"), class = "factor"), group_factor_ordered = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("race",
"gender", "p_educ"), class = "factor")), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -43L), groups = structure(list(
group = c("gender", "gender", "p_educ", "p_educ", "p_educ",
"p_educ", "p_educ", "race", "race", "race", "race"), levels = structure(c(1L,
2L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L), .Label = c("1",
"2", "3", "4", "5"), class = "factor"), .rows = structure(list(
1:4, 5:8, 9:12, 13:16, 17:20, 21:24, 25:27, 28:31, 32:35,
36:39, 40:43), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -11L), .drop = TRUE))

答案1

得分: 2

以下是代码部分的翻译:

library(ggplot2)

test_labels <- c(
  "0" = "测试不足", "1" = "测试中等",
  "2" = "测试高于平均", "3" = "测试卓越"
)

var_levels <- c(
  "男性", "女性", "高中以下", "部分高中", "高中毕业",
  "一些大学或\n技术学校", "大学毕业\n或更高学历",
  "白人", "黑人", "西班牙裔", "其他种族"
)

df$var_ordered <- factor(df$var, levels = rev(var_levels))

ggplot(df, aes(x = percent, y = var_ordered, fill = group_factor_ordered)) +
  geom_col(
    orientation = "y",
    color = "black"
  ) +
  facet_grid(group_factor_ordered ~ test_level,
    scales = "free", space = "free_y",
    labeller = labeller(test_level = test_labels)
  ) +
  labs(
    title = "测试水平的人口统计",
    y = "",
    x = "百分比"
  ) +
  theme_minimal() +
  theme(
    legend.position = "none",
    strip.text.y = element_blank()
  )

如果您需要更多帮助,请告诉我。

英文:

For your facet labels you could use a named vector which serves as a lookup table and for your var the way to go is you 11-levels factor:

library(ggplot2)
test_labels &lt;- c(
&quot;0&quot; = &quot;Test Under&quot;, &quot;1&quot; = &quot;Test Medium&quot;,
&quot;2&quot; = &quot;Test Above Avg&quot;, &quot;3&quot; = &quot;Test Excellent&quot;
)
var_levels &lt;- c(
&quot;Male&quot;, &quot;Female&quot;, &quot;Less than HS&quot;, &quot;Some HS&quot;, &quot;Graduate HS&quot;,
&quot;Some College or\nTechnical School&quot;, &quot;Graduate College\nor Higher&quot;,
&quot;White&quot;, &quot;Black&quot;, &quot;Hispanic&quot;, &quot;Other Race&quot;
)
df$var_ordered &lt;- factor(df$var, levels = rev(var_levels))
ggplot(df, aes(x = percent, y = var_ordered, fill = group_factor_ordered)) +
geom_col(
orientation = &quot;y&quot;,
color = &quot;black&quot;
) +
facet_grid(group_factor_ordered ~ test_level,
scales = &quot;free&quot;, space = &quot;free_y&quot;,
labeller = labeller(test_level = test_labels)
) +
labs(
title = &quot;Demographic breakdown of Testing Level&quot;,
y = &quot;&quot;,
x = &quot;Percent&quot;
) +
theme_minimal() +
theme(
legend.position = &quot;none&quot;,
strip.text.y = element_blank()
)

将geom_col按特定顺序排列

huangapple
  • 本文由 发表于 2023年2月8日 23:56:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定