来自R中数据框的水平条形图

时间:2017-05-02 22:16:28

标签: r bar-chart

我有一个名为mydata的数据框,其中一个名为“Tm”的列包含团队名称,另外10个具有统计信息。其中一个(PTS)具有每个玩家的分数。我使用此代码创建一个新的数据框,其中每个团队的点数基于玩家和点的平均值

2 个答案:

答案 0 :(得分:2)

假设您的数据框mydata如下所示:

mydata <- data.frame(Tm = c("ATL",  "BOS",  "BRK",  "CHI",  "CHO",  "CLE",  "DAL",  "DEN",  
                            "DET",  "GSW",  "HOU",  "IND",  "LAC",  "LAL",  "MEM",  "MIA", 
                            "MIL",  "MIN",  "NOP",  "NYK",  "OKC",  "ORL",  "PHI",  "PHO",  
                            "POR",  "SAC",  "SAS",  "TOR",  "UTA",  "WAS"), 
                     PTS = c(8433, 8669, 7503, 8335, 8479, 8554, 8388, 8355, 8361, 9421, 
                             8737, 7860, 8569, 7982, 8126, 8204, 8122, 8398, 8423, 8065, 
                             9038, 8369, 7142, 8271, 8622, 8740, 7936, 8394, 8010, 8534), 
                     stringsAsFactors = FALSE)

我会使用ggplot作为条形图。而且我不会为平均值生成额外的列;只需使用该行来表示其值。

ggplot(mydata, aes(reorder(Tm, PTS), PTS)) + 
  geom_col(fill = "grey70") + coord_flip() + 
  labs(x = "Team", y = "PTS") + 
  geom_hline(aes(yintercept = mean(PTS)), color = "red")

另一种选择:根据它们的值是高于还是低于平均值来填充条形。

ggplot(mydata, aes(reorder(Tm, PTS), PTS)) + 
  geom_col(aes(fill = PTS > mean(PTS))) + coord_flip() + 
  labs(x = "Team", y = "PTS") + 
  geom_hline(aes(yintercept = mean(PTS)), color = "red") + 
  guides(fill = FALSE)

结果: enter image description here

答案 1 :(得分:0)

以下是使用R中的diamonds示例数据集制作此类绘图的方法:

library(dplyr)
library(ggplot2)

bind_rows(diamonds %>%
              group_by(color) %>%
              summarise(price = mean(price)) %>%
              mutate(average = FALSE),
          data_frame(color = "mean", 
                     price = mean(diamonds$price),
                     average = TRUE)) %>%
    arrange(desc(price)) %>%
    mutate(color = reorder(color, price)) %>%
    ggplot(aes(color, price, fill = average)) +
    geom_col(show.legend = FALSE) +
    coord_flip() +
    scale_fill_manual(values = c("gray80", "red")) +
    theme_minimal()