简体   繁体   中英

How to display display all bar in ggplot

Display 10 bars based on score.

I want to display 10 bars based on individual_score. Since CH Gayle and AB de Villiers is repeated twice I am only getting 8 bars. But I want to show 10 bars based on score in descending order.

batsman        individual_score   
CH Gayle    175
BB McCullum 158
AB de Villiers  133
RR Pant 130
AB de Villiers  129
CH Gayle    128
M Vijay 127
DA Warner   126
V Sehwag    122
SR Watson   121

I tried reorder for descending order but it does not work. I also tried fill = score and batsman to get 10 bars.

highest_individual_score <- innings %>%
group_by(match_id,batsman) %>%
summarize(individual_score = sum(batsman_runs)) %>%
arrange(desc(individual_score)) %>%
ungroup() %>%
top_n(10, individual_score) %>%
ggplot(aes(x= batsman, y = individual_score,fill = batsman)) +
geom_bar(stat= "identity", show.legend = FALSE) +
theme(axis.text.x = element_text(angle = 90)) +
labs(x = "Batsman", y = "Individual Score", title ="Top 10 highest 
individual scorer")

The above code gives me only 8 bars in alphabetical order. How do I make it display 10 bars?

What if you tried something like this? With your code, ggplot2 is grouping them together because the x-axis is batsman , and it's taking you literally on that. But what if you made the x-axis some unique thing, unrelated to either batsman or individual_score ?

I did this below by giving each row an arbitrary unique ID. I used row_number() so they just go naturally in order. I then turned them into a factor (rather than leaving them as numeric) so that it works nicely with the scale.

library(tidyverse)

highest_individual_score <- innings %>%
  group_by(match_id, batsman) %>%
  summarize(individual_score = sum(batsman_runs)) %>%
  arrange(desc(individual_score)) %>%
  ungroup() %>%
  top_n(10, individual_score) %>% 
  mutate(id = as.factor(row_number()))

Then I made minor changes to your ggplot2 code itself:

highest_individual_score %>%
  ggplot(aes(x = id, y = individual_score, fill = batsman))+
  geom_bar(stat = "identity", show.legend = FALSE)+
  scale_x_discrete(
    labels = highest_individual_score$batsman,
    name = "Batsman"
  ) +
  theme(axis.text.x = element_text(angle = 90))+
  labs(y = "Individual Score", title ="Top 10 highest 
       individual scorer")

Essentially, the x-axis is now the unique ID, and so it treats each record separately. But then you have to relabel the axis with your batsmen, which I did using the labels argument of scale_x_discrete() . Make sense?

The following is a hack but it works.
The trick is to paste a blank " " at the end of the duplicated batsman . Then convert to factor . Like this there will be all different values but the blank doesn't show up.

library(dplyr)
library(ggplot2)

df1 %>%
  mutate(batsman = as.character(batsman),
         batsman = ifelse(duplicated(batsman), paste0(batsman, " "), batsman),
         batsman = factor(batsman)) %>%
  ggplot(aes(x= batsman, y = individual_score,fill = batsman)) +
  geom_bar(stat= "identity", show.legend = FALSE) +
  theme(axis.text.x = element_text(angle = 90)) +
  labs(x = "Batsman", 
       y = "Individual Score", 
       title ="Top 10 highest individual scorer")

在此处输入图片说明

Data.

df1 <- read.table(text = "
batsman        individual_score   
'CH Gayle'    175
'BB McCullum' 158
'AB de Villiers'  133
'RR Pant' 130
'AB de Villiers'  129
'CH Gayle'    128
'M Vijay' 127
'DA Warner'   126
'V Sehwag'    122
'SR Watson'   121
", header = TRUE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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