简体   繁体   中英

Minimize the height of a geom_bar plot

I have a bar plot cutting off the data labels because of height being bigger that other data points. I want to adjust the height so that the labels are visible.

season batsman       total_runs
<int> <chr>              <int>
1   2016 V Kohli              973
2   2018 KS Williamson        747
3   2012 CH Gayle             733
4   2013 MEK Hussey           733
5   2019 DA Warner            727
6   2014 RV Uthappa           660
7   2017 DA Warner            641
8   2010 SR Tendulkar         618
9   2008 SE Marsh             616
10   2011 CH Gayle             608
11   2009 ML Hayden            572
12   2015 DA Warner            562

I have tried ylim but does not work in my case.

season_top_scorer <- match_full%>%
  group_by(season,batsman)%>%
  summarize(total_runs = sum(batsman_runs))%>%
  arrange(season,desc(total_runs))%>%
  filter(total_runs == max(total_runs))%>%
  arrange(desc(total_runs))%>%
  ggplot(aes(x = season,y = total_runs,fill = batsman))+
  geom_bar(stat ="identity")+
  ggtitle("Highest run scorer each season")+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))+
  scale_x_discrete(name="Season", 
  limits = c(2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019))+
  geom_text(aes(label= total_runs,vjust= 0 ))+
  scale_y_discrete(name = "Total Runs",  limits = c(0,250,500,750,1000,1250))

The only problem is with season 2016. The height of bar is too big that its cutting off the label.Any idea what might solve this problem in the above code

You should use scale_y_continuous instead of scale_y_discrete .

scale_y_continuous(name = "Total Runs", breaks = c(0, 250, 500, 750, 1000, 1250))

在此输入图像描述

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