Merlin Nuiamäe
Merlin Nuiamäe

Reputation: 93

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

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?

Upvotes: 2

Views: 244

Answers (3)

TarJae
TarJae

Reputation: 79184

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

Upvotes: 2

Quinten
Quinten

Reputation: 41499

You can use the sprintf("€%0.2f") command to label the values with euro in this code:

palgad_joonisele2 <- 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"))


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=sprintf("€%0.2f", tooandja_kulu_aastas)), 
            colour = "white", 
            size = 2.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()) 

Output: enter image description here

Upvotes: 2

Rui Barradas
Rui Barradas

Reputation: 76621

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 reprex package (v2.0.1)

Upvotes: 2

Related Questions