2

I have a dataframe called crashes_deaths_injuries_TA_pop that looks like:

TA_name Population
Gore 34466
Buller 444444
Cluth 34455

I am trying to show the data with a bar chart however, the scale of x-axis is messed up and I'm not sure how to . I know this must be easy but I am new to coding so I am struggling.

here is my code for the bar chart:

ggplot(crashes_deaths_injuries_TA_pop, aes(x = reorder(TA_name, Population), y = Population, fill = Population)) +
  geom_col(alpha = 0.7) +
  scale_fill_distiller(palette = "Reds", direction = 1) +
  coord_flip() +
  labs(x = "", y = "Population", fill = "Population",
       title = "Populations of South Island TA's | 2018",
       caption = "Data: Census 2018, StatsNZ") +
  theme_minimal() +
  theme(legend.position = "none") 

I was also wondering how can you add like the population as a number at the end of each bar for each TA_name

bar chart

Park
  • 14,771
  • 6
  • 10
  • 29
  • 1
    Is it the scale or the scientific notation that is "messed up"? (see e.g. https://stackoverflow.com/questions/5352099/how-to-disable-scientific-notation/5352328#5352328) – jared_mamrot Oct 07 '21 at 03:27
  • I have removed the scientific notation but now I want to edit the breaks, would this be with breaks command? – scumbagsurfer Oct 07 '21 at 03:31

1 Answers1

5

Use scale_y_continuous(breaks = ) to edit ticks of x axis of yours and use geom_text to add Population next to the bar chart. Here's an example.

ggplot(crashes_deaths_injuries_TA_pop, aes(x = reorder(TA_name, Population), y = Population, fill = Population)) +
  geom_col(alpha = 0.7) +
  scale_fill_distiller(palette = "Reds", direction = 1) +
  coord_flip() +
  labs(x = "", y = "Population", fill = "Population",
       title = "Populations of South Island TA's | 2018",
       caption = "Data: Census 2018, StatsNZ") +
  theme_minimal() +
  theme(legend.position = "none") +
  scale_y_continuous(breaks = seq(0, 5e05, 0.5e05)) + geom_text(aes(label = Population, y = Population+0.18e05))
  

enter image description here

Park
  • 14,771
  • 6
  • 10
  • 29
  • Could you please explain the "0, 5e05, 0.5e05" of scale_y_continuous and "+0.18e05" for geom_text just for future use, not really sure – scumbagsurfer Oct 07 '21 at 06:52
  • @scumbagsurfer First, `breaks = seq(0, 5e05, 0.5e05)` makes ticks of x-axis. As you can see, ticks are sequencs of `50000 = 0.5e05, 1e05, 1.5e05, ...`. You can change space between ticks by changing `0.5e05`. Second, `+0.18e05` is added to move location of text that to make numbers not overlapped with bars. I manually tested several numbers and find appropriate number for you. – Park Oct 07 '21 at 06:56