繁体   English   中英

如何根据 geom_bar 中的 bar 值对 bar 进行排序?

[英]How to order the bar based on bar values in geom_bar?

这是我获取数据集和 c 的地方

board_game_original<- read.csv("https://raw.githubusercontent.com/bryandmartin/STAT302/master/docs/Projects/project1_bgdataviz/board_game_raw.csv")

#tidy up the column of mechanic and category with cSplit function
library(splitstackshape)
mechanic <- board_game$mechanic
board_game_tidy <- cSplit(board_game,splitCols=c("mechanic","category"), sep = ",", direction = "long")

我试图通过按 y 轴上条的值对条进行排序来使图形更有条理。 我尝试使用重新排序功能,但仍然不起作用。 有没有人有什么建议? 我对 R 很陌生,希望能学到更多!

library(ggplot2)
average_complexity <- board_game_tidy %>% 
            filter(yearpublished >= 1950, users_rated >= 25, average_complexity>0 ) %>%
            select(average_complexity)
category_complexity_graph <- ggplot(data=board_game_tidy, aes(x = reorder(category, -average_complexity), y = average_complexity, na.rm = TRUE)) + 
        geom_bar(stat = "identity", na.rm = TRUE, color="white",fill="sky blue") + 
        ylim(0,5) +
        theme_bw() +
        ggtitle("Which category of board games has the highest level of average complexity") +
        xlab("category of board games") +
        ylab("average complexity of the board game") +
        theme(axis.text.x = element_text(size=5, angle = 45)) +
        theme(plot.title = element_text(hjust = 0.5)) 
category_complexity_graph

这是我绘制的图表: 在此处输入图片说明 “类别”是一个分类变量,“平均复杂度”是一个连续变量。

我试图回答“哪个类别的平均复杂度最高?”这个问题。 但是这张图看起来很乱,任何清理它的建议也将不胜感激! 谢谢你们

也许这就是你正在寻找的。 问题不在于重新排序,而在于准备数据。 (; 换个说法,按平均值重新排序不会给你一个很好的图,因为你有多个 obs。每个类别,更重要的是每个类别有不同数量的 obs。当你用这个数据集做条形图时,所有这些 obs。得到堆叠,即您的图显示平均复杂度的总和。因此,要获得所需的结果,您必须首先按类别汇总数据集。这样做之后,您的重新排序代码将起作用并为您提供一个不错的图。

但是,我建议翻转轴,使标签更易于阅读:

board_game_original<- read.csv("https://raw.githubusercontent.com/bryandmartin/STAT302/master/docs/Projects/project1_bgdataviz/board_game_raw.csv")

#tidy up the column of mechanic and category with cSplit function
library(splitstackshape)
board_game <- board_game_original
mechanic <- board_game$mechanic
board_game_tidy <- cSplit(board_game,splitCols=c("mechanic","category"), sep = ",", direction = "long")

library(ggplot2)
library(dplyr)
# Summarise your dataset
board_game_tidy1 <- board_game_tidy %>% 
  as_tibble() %>% 
  filter(yearpublished >= 1950, users_rated >= 25, average_complexity > 0, !is.na(category)) %>%
  group_by(category) %>% 
  summarise(n = n(), average_complexity = mean(average_complexity, na.rm = TRUE))

ggplot(data=board_game_tidy1, aes(x = reorder(category, average_complexity), y = average_complexity, na.rm = TRUE)) + 
  geom_bar(stat = "identity", na.rm = TRUE, color="white",fill="sky blue") + 
  ylim(0,5) +
  theme_bw() +
  ggtitle("Which category of board games has the highest level of average complexity") +
  xlab("category of board games") +
  ylab("average complexity of the board game") +
  #theme(axis.text.x = element_text(size=5, angle = 45)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  coord_flip()
 

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM