Ggplot2 custom axis and label formatting

I am trying to draw shortcuts that look informative, clear and neat.

I gave an example and raised another question about label and axis formatting.

For example, I have sales data that includes the brand, categories and expenses in euros. When the sum of EUR large (millions or more) of labels looks very difficult to read, not informative.

As a result, the x-axis is in Scientific notation , and also looks really unclean.

I managed to format the labels in my own way: it shows Euros in thousands. geom_text(aes(label= paste(round(EUR/1000,0),"€"), y=pos), colour="white") Is there a simpler or automatic way?

Since Scientific notation looks really obscure, for the axis, I tried to use scale_y_continuous(formatter = "dollar") , but this does not seem to work. Moreover, I could not find if Eur implemented instead of the dollar. I believe it would be better to show y-axis in thousands . Any solutions?

In addition, I am enclosing a reproducible example:

 library(plyr) library(dplyr) library(ggplot2) library(scales) set.seed(1992) n=68 Category <- sample(c("Black", "Red", "Blue", "Cyna", "Purple"), n, replace = TRUE, prob = NULL) Brand <- sample("Brand", n, replace = TRUE, prob = NULL) Brand <- paste0(Brand, sample(1:5, n, replace = TRUE, prob = NULL)) EUR <- abs(rnorm(n))*100000 df <- data.frame(Category, Brand, EUR) df.summary = df %>% group_by(Brand, Category) %>% summarise(EUR = sum(EUR)) %>% # Within each Brand, sum all values in each Category mutate( pos = cumsum(EUR)-0.5*EUR) ggplot(df.summary, aes(x=reorder(Brand,EUR,function(x)+sum(x)), y=EUR, fill=Category)) + geom_bar(stat='identity', width = .7, colour="black", lwd=0.1) + geom_text(aes(label=ifelse(EUR>100,paste(round(EUR/1000,0),"€"),""), y=pos), colour="white") + coord_flip()+ labs(y="", x="") 

enter image description here

+7
source share
3 answers

You can set the prefix in dollar_format for euro instead of dollars:

 scale_y_continuous(labels=dollar_format(prefix="€")) + 

This takes care of the problem of scientific notation.

To get everything in thousands, you can simply divide by 1000 when creating the summary. To reduce clutter, you can leave the euro symbol in the bar labels, but I saved the symbol in the example below:

 df.summary = df %>% group_by(Brand, Category) %>% summarise(EUR = sum(EUR)/1000) %>% # Within each Brand, sum all values in each Category mutate( pos = (cumsum(EUR)-0.5*EUR)) ggplot(df.summary, aes(x=reorder(Brand,EUR,function(x)+sum(x)), y=EUR, fill=Category)) + geom_bar(stat='identity', width = .7, colour="black", lwd=0.1) + geom_text(aes(label=ifelse(EUR>100,paste0("€", round(EUR,0)),""), y=pos), colour="white") + scale_y_continuous(labels=dollar_format(prefix="€")) + coord_flip()+ labs(y="Thousands of €", x="") 

enter image description here

+13
source

@ AK47 there is a parsing of the Euro sign in a brilliant state.

Try replacing it with the following: (\ u20AC)

So far, it has worked very well.

+2
source

It works too

 scale_y_continuous(labels = function(x) paste0(x, "€")) 

You can put any character instead of €

0
source

All Articles