0

I'm currently working on a sales dataset, and I have been trying to make a barchart, showcasing the units sold by each country, with the colouring of the charts being the number of inhabitants in each country. so far I have used the following code to get the chart

ChartProductsbyCountry <- ggplot(Dataframe10, aes(x=Country, y=SumAct_U)) + geom_col(aes(fill=Inhabitants_2019)) + theme(axis.text.x = element_text(angle = 90, size = 10))

library(scales)
ChartProductsbyCountry + scale_y_continuous(labels = comma) 

This gives me the following chart Barchart

I'm already very satisfied with it, however I would like to change some things but don't know how:

on the right side, showing the "colouring labels/legend", it does not show the actual numbers but rather 2e+07, 4e+07, etc... how can I change it to showing Numbers instead? And as for the y-axis, how should I change my code to have ticks going from 0 to 1.250.000,00 at every 125.000 (Units) (so starting with 0, then 125.000, then 250.000, then 375.000, ...)?

  • Same as for the y scale but in case of a (continuous) fill scale it's `scale_fill_continuous`. And you could set your desired ticks via the `breaks` argument of the scale. – stefan Jan 28 '22 at 10:05
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 02 '22 at 09:53

1 Answers1

0

OP, as mentioned by @stefan in the comment, you can use the breaks= argument to control spacing of your ticks on the axis. To format the colorbar legend items, you can use scale_fill_continuous() in the same manner you did for the y axis. Here's an example:

library(ggplot2)
library(scales)

set.seed(8675309)
df <- data.frame(x=LETTERS, y=sample(0:20, replace=T, size=26) * 1000000)

p <- 
ggplot(df, aes(x,y, fill=y)) + geom_col() +
  scale_y_continuous(labels = comma)
p

enter image description here

To set the number formatting in the colorbar, you can use labels = comma within scale_fill_continuous(). To change the ticks on the y axis, you can access that via the breaks= argument. Essentially, send a vector of values to breaks= to have the labels marked where you want. In this case, I'll set it up to make a tick mark every 1 million, and then set the colorbar to use comma format:

ggplot(df, aes(x,y, fill=y)) + geom_col() +
  scale_y_continuous(labels = comma, breaks=seq(0, max(df$y), by=1000000)) +
  scale_fill_continuous(labels=comma)

enter image description here

chemdork123
  • 12,369
  • 2
  • 16
  • 32