Prafulla
Prafulla

Reputation: 585

How to arrange y axis in bar chart using ggplot2

I have a following dataframe and I'am trying to plot bar chart.

country <- c('AUD','USD','GBP','ROW','EUR')
count <- c(58, 28, 8, 4, 2)
data <- data.frame(country, count)

    ggplot(data = data , aes(x = 'COUNTRY', y = reorder(count, -count), fill = country))+
  geom_bar(stat = "identity")+ 
  xlab("COUNTRY")+
  ylab("TOTAL")+
  theme_minimal()+
  geom_text(aes(label = country), vjust = -0.5, size = 3)+
  scale_fill_brewer(palette="Paired")+
  theme(legend.position = "bottom",
        legend.title = element_blank())

Plot generated by this code does not have axis and point labels in order. It generates below plot.

enter image description here

I need a help to re-arrange this axis and count labels.

Upvotes: 0

Views: 206

Answers (1)

Christoph Wolk
Christoph Wolk

Reputation: 1758

It's not quite clear to me what you want the output to look like. Would something like this be ok?

ggplot(data = data , aes(x = 'COUNTRY', y = count, 
                         fill = reorder(country, count)))+
  geom_bar(stat = "identity")+ 
  xlab("COUNTRY")+
  ylab("TOTAL")+
  theme_minimal()+
  geom_text(aes(label = sprintf("%s (%d)", country, count), 
                y = cumsum(count) - 0.5*count), size = 3)+
  scale_fill_brewer(palette="Paired")+
  theme(legend.position = "bottom",
        legend.title = element_blank())

Upvotes: 1

Related Questions