Search code examples
rggplot2chartsbar-chartmongolite

R ggplot horizontal bar chart with thousand data


I am trying to produce a bar graph that has thousand data.

I have size problem with ggplot.

Code :

ggplot(data = df, aes(x=extension, y=duration)) +
  geom_bar(stat="identity", width=10,fill="steelblue")+
  ggtitle("Chart") + 
  xlab("Number") +
  ylab("Duration") +
  theme(legend.position = "none")+
  theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5))+ 
  coord_flip()

Output:

Chart output

Load data frame from MongoDB. Data Frame:

1        36952  7158803
2        36110  7068360
3        36080  4736043
4        36509  4726630
5        36890  4699026
6        36051  4698594
7        36783  4677233
8        36402  4672623
9        36880  4672093
10       36513  4655583
11       36522  4630962
12       36116  4628046
13       36746  4593291
....

Solution

  • From your sample chart I would infer that your x-axis (extension) is probably a factor. If it were numeric, ggplot would correctly scale the axis.

    I would recommend to check the class of the columns of your dataset. Make sure that both are numeric. Alternatively, you would have to come up with an appropriate scaling of your x-axis.

    Here's the plot where your flipped x-axis is a factor; ggplot tries to render every separate level of the factor and they overlap as there are so many. I created some fake data quickly to mimic yours.

    enter image description here

    Here's the plot where extension is numeric and ggplot neatly scales this correctly.

    enter image description here