按偏斜对 facet_wrap 内的直方图进行排序
Sorting histogram plots within facet_wrap by skew
我对每个国家大约有 1000 个观测值,我使用 facet_wrap 来显示每个国家的 geom_bar,但输出是按字母顺序排列的。我想按偏斜对它们进行聚类或排序(因此最正偏斜的国家在一起并向正态分布国家移动,然后负偏斜国家以最负偏斜结束)而不关注哪些国家更相似彼此。我在想也许 psych::describe() 可能有用,因为它计算偏斜,但我很难弄清楚如何将这些信息添加到 .
任何建议都会有所帮助
如果没有可重现的示例,我无法详细介绍,但这将是我的一般方法。使用 psych::describe()
创建一个国家向量,这些国家/地区从最大正偏度到最小正偏度排序:country_order
。接下来,将数据集中的国家/地区列分解为 country = factor(country, levels = country_order)
。当您使用 facet_wrap
时,绘图将按照与 country_order
.
相同的顺序显示
经过一些故障排除后,我发现(我认为是)一种有效的方法:
skews <- psych::describe.By(df$DV, df$Country, mat = TRUE) #.BY and mat will produce a matrix that you can use to merge into your df easily
skews %<>%select(group1, mean, skew) %>% sjlabelled::as_factor(., group1) #Turn it into a factor, I also kept country means
combined <- sort(union(levels(df$Country), levels(skews$group1))) #I was getting an error that my levels were inconsistent even though they were the same (since group1 came from df$Country) which I think was due to having Country reference category Germany which through off the alphabetical sort of group1 so I used [dfrankow's answer][1]
df <- left_join(mutate(df, Country=factor(Country, levels=combined)),
mutate(skews, Country=factor(group1, levels=combined))) %>% rename(`Country skew` = "skew", `Country mean` = "mean") %>% select(-group1)
df$`Country skew` <- round(df$`Country skew`, 2)
ggplot(df) +
geom_bar(aes(x = DV, y=(..prop..)))+
xlab("Scale axis text") + ylab("Proportion") +
scale_x_continuous()+
scale_y_continuous(labels = scales::percent_format(accuracy = 1))+
ggtitle("DV distribution by country mean")+
facet_wrap(~ Country %>% fct_reorder(.,mean), nrow = 2) #this way the reorder that was important for my lm can remain intact
我对每个国家大约有 1000 个观测值,我使用 facet_wrap 来显示每个国家的 geom_bar,但输出是按字母顺序排列的。我想按偏斜对它们进行聚类或排序(因此最正偏斜的国家在一起并向正态分布国家移动,然后负偏斜国家以最负偏斜结束)而不关注哪些国家更相似彼此。我在想也许 psych::describe() 可能有用,因为它计算偏斜,但我很难弄清楚如何将这些信息添加到
任何建议都会有所帮助
如果没有可重现的示例,我无法详细介绍,但这将是我的一般方法。使用 psych::describe()
创建一个国家向量,这些国家/地区从最大正偏度到最小正偏度排序:country_order
。接下来,将数据集中的国家/地区列分解为 country = factor(country, levels = country_order)
。当您使用 facet_wrap
时,绘图将按照与 country_order
.
经过一些故障排除后,我发现(我认为是)一种有效的方法:
skews <- psych::describe.By(df$DV, df$Country, mat = TRUE) #.BY and mat will produce a matrix that you can use to merge into your df easily
skews %<>%select(group1, mean, skew) %>% sjlabelled::as_factor(., group1) #Turn it into a factor, I also kept country means
combined <- sort(union(levels(df$Country), levels(skews$group1))) #I was getting an error that my levels were inconsistent even though they were the same (since group1 came from df$Country) which I think was due to having Country reference category Germany which through off the alphabetical sort of group1 so I used [dfrankow's answer][1]
df <- left_join(mutate(df, Country=factor(Country, levels=combined)),
mutate(skews, Country=factor(group1, levels=combined))) %>% rename(`Country skew` = "skew", `Country mean` = "mean") %>% select(-group1)
df$`Country skew` <- round(df$`Country skew`, 2)
ggplot(df) +
geom_bar(aes(x = DV, y=(..prop..)))+
xlab("Scale axis text") + ylab("Proportion") +
scale_x_continuous()+
scale_y_continuous(labels = scales::percent_format(accuracy = 1))+
ggtitle("DV distribution by country mean")+
facet_wrap(~ Country %>% fct_reorder(.,mean), nrow = 2) #this way the reorder that was important for my lm can remain intact