将€符号添加到气泡图中的数据标签 - ggplot?

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

这是我的数据头部:

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 = 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())

这就是我的情节现在的样子:

我的问题是 - 如何在图表上的数字之前获取欧元符号 (€) 并格式化数据标签,因为它们是人工成本,所以它们更易读。例如:€10 000 不是 10000 或类似 €10,000?

使用包 scales 更改 geom_text 标签。唯一的变化是

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

其余代码与问题中的完全相同。

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())

reprex package (v2.0.1)

于 2022 年 3 月 14 日创建

您可以使用 sprintf("€%0.2f") 命令在此代码中用欧元标记值:

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()) 

输出:

使用 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())