Home > database >  Add € sign to data labels in bubble chart - ggplot?
Add € sign to data labels in bubble chart - ggplot?

Time:03-15

This is the head of my data:

structure(list(asutus = c("Eesti Draamateater", "Eesti Draamateater", 
"Eesti Noorsooteater", "Eesti Noorsooteater", "Rahvusooper Estonia", 
"Rahvusooper Estonia", "Rakvere Teatrimaja", "Rakvere Teatrimaja", 
"Sakala Teatrimaja", "Vene Teater", "Vene Teater"), liik = c("Kinnisvara haldus ja korraldus", 
"Teatrite tugiteenused", "Kinnisvara haldus ja korraldus", "Teatrite tugiteenused", 
"Kinnisvara haldus ja korraldus", "Teatrite tugiteenused", "Kinnisvara haldus ja korraldus", 
"Teatrite tugiteenused", "Kinnisvara haldus ja korraldus", "Kinnisvara haldus ja korraldus", 
"Teatrite tugiteenused"), tooandja_kulu_aastas = c(131980, 455701, 
103401, 257137, 124755, 1081211, 49147, 188658, 24373, 105234, 
236232)), row.names = c(NA, -11L), class = c("tbl_df", "tbl", 
"data.frame"))

My colors:

prx_col_palette <- function(){
  c("#E69F00",
    "#56B4E9",
    "#009E73",
    "#F0E442",
    "#0072B2",
    "#D55E00",
    "#CC79A7")
}
prx_cols <- prx_col_palette()

My bubble chart:

ggplot(palgad_joonisele2,
       aes(x = liik, 
           y = asutus,
           colour = liik,
           size = tooandja_kulu_aastas))  
  geom_point()  
  geom_text(aes(label = tooandja_kulu_aastas), 
            colour = "white", 
            size = 3.5)  
  scale_x_discrete(position = "top") 
  scale_y_discrete(limits = rev)  
  scale_size_continuous(range = c(14, 37))   
  scale_colour_manual(values = prx_cols) 
  labs(x = NULL, y = NULL)  
  theme(legend.position = "none",
        panel.background = element_blank(),
        panel.grid = element_blank(),
        axis.text.x = element_text(size=11),
        axis.text.y = element_text(size=11),
        axis.ticks = element_blank())

This is what my plot looks like right now:

enter image description here

My question is - how to get Euro signs (€) before the numbers on the graph and format the data labels that they would be more readable since they are labor costs. For example like this: €10 000 not 10000 or like €10,000?

CodePudding user response:

Use package scales to change the geom_text label. The only change is

label = dollar(tooandja_kulu_aastas, prefix = "\u20ac")

the rest of the code is exactly the same as in the question.

library(ggplot2)
library(scales)

prx_col_palette <- function(){
  c("#E69F00",
    "#56B4E9",
    "#009E73",
    "#F0E442",
    "#0072B2",
    "#D55E00",
    "#CC79A7")
}
prx_cols <- prx_col_palette()

ggplot(palgad_joonisele2,
       aes(x = liik, 
           y = asutus,
           colour = liik,
           size = tooandja_kulu_aastas))  
  geom_point()  
  geom_text(aes(label = dollar(tooandja_kulu_aastas, prefix = "\u20ac")), 
            colour = "white", 
            size = 3.5)  
  scale_x_discrete(position = "top") 
  scale_y_discrete(limits = rev)  
  scale_size_continuous(range = c(14, 37))   
  scale_colour_manual(values = prx_cols) 
  labs(x = NULL, y = NULL)  
  theme(legend.position = "none",
        panel.background = element_blank(),
        panel.grid = element_blank(),
        axis.text.x = element_text(size=11),
        axis.text.y = element_text(size=11),
        axis.ticks = element_blank())

Created on 2022-03-14 by the enter image description here

CodePudding user response:

Alternative approach using paste0('€',formatC(tooandja_kulu_aastas, big.mark=',', format = 'fg'))

library(tidyverse)
library(scales)


prx_col_palette <- function(){
  c("#E69F00",
    "#56B4E9",
    "#009E73",
    "#F0E442",
    "#0072B2",
    "#D55E00",
    "#CC79A7")
}
prx_cols <- prx_col_palette()

palgad_joonisele2 %>% 
  mutate(my_label = paste0('€',formatC(tooandja_kulu_aastas, big.mark=',', format = 'fg'))) %>% 
ggplot(aes(x = liik, 
           y = asutus,
           colour = liik,
           size = tooandja_kulu_aastas))  
  geom_point()  
  geom_text(aes(label = my_label), 
            colour = "white", 
            size = 3.5)  
  scale_x_discrete(position = "top") 
  scale_y_discrete(limits = rev)  
  scale_size_continuous(range = c(14, 37))   
  scale_colour_manual(values = prx_cols) 
  labs(x = NULL, y = NULL)  
  theme(legend.position = "none",
        panel.background = element_blank(),
        panel.grid = element_blank(),
        axis.text.x = element_text(size=11),
        axis.text.y = element_text(size=11),
        axis.ticks = element_blank())

enter image description here

  • Related