在 geom_text() 如何缩小文本标签大小而不丢失映射属性
In geom_text() how to shirnk text label size and not lost mapping attributes
在下面的代码中,我使用 geom_text() 将文本添加到绘图并将值映射到大小。但是与绘图相比标签尺寸有点大,如何将它们全部缩小到小尺寸? (并且没有丢失映射属性)。谢谢!
library(tidyverse)
plot_data <- data.frame(category=c('A','A','B','C'),
sub_category=c('a1','a2','b1','c1'),
value=c(6,12,3,2))
plot_data %>% mutate(sub_category=if_else(category=='A',
sub_category,category)) %>%
pivot_longer(names_to = 'title',values_to ='cat_region',-value) %>%
filter(!(title=='sub_category'&cat_region %in% c('B','C') )) %>%
group_by(title,cat_region) %>%
summarise(value_sum=sum(value)) %>%
ggplot(aes(x=title,y=value_sum,fill=cat_region,
group=interaction(title,cat_region)))+geom_col()+
geom_text(position=position_stack(0.5),
aes(size=value_sum,
label=paste0(cat_region,'\n',value_sum)))+
scale_x_discrete(
limits=c('blank','blank','blank','category','sub_category'))+
coord_polar(theta='y')
与其他美学一样,您可以通过比例设置可能值的范围,例如在 size
审美的情况下,通过 scale_size
的 range
参数。通过减小范围的最大值,您可以缩小所有标签。
library(tidyverse)
plot_data %>%
mutate(sub_category = if_else(category == "A",
sub_category, category
)) %>%
pivot_longer(names_to = "title", values_to = "cat_region", -value) %>%
filter(!(title == "sub_category" & cat_region %in% c("B", "C"))) %>%
group_by(title, cat_region) %>%
summarise(value_sum = sum(value)) %>%
ggplot(aes(
x = title, y = value_sum, fill = cat_region,
group = interaction(title, cat_region)
)) +
geom_col() +
geom_text(position=position_stack(0.5),
aes(
size = value_sum,
label = paste0(cat_region, "\n", value_sum)
)
) +
scale_x_discrete(
limits = c("blank", "blank", "blank", "category", "sub_category")
) +
scale_size(range = c(1, 3.5)) +
coord_polar(theta = "y")
#> `summarise()` has grouped output by 'title'. You can override using the
#> `.groups` argument.
在下面的代码中,我使用 geom_text() 将文本添加到绘图并将值映射到大小。但是与绘图相比标签尺寸有点大,如何将它们全部缩小到小尺寸? (并且没有丢失映射属性)。谢谢!
library(tidyverse)
plot_data <- data.frame(category=c('A','A','B','C'),
sub_category=c('a1','a2','b1','c1'),
value=c(6,12,3,2))
plot_data %>% mutate(sub_category=if_else(category=='A',
sub_category,category)) %>%
pivot_longer(names_to = 'title',values_to ='cat_region',-value) %>%
filter(!(title=='sub_category'&cat_region %in% c('B','C') )) %>%
group_by(title,cat_region) %>%
summarise(value_sum=sum(value)) %>%
ggplot(aes(x=title,y=value_sum,fill=cat_region,
group=interaction(title,cat_region)))+geom_col()+
geom_text(position=position_stack(0.5),
aes(size=value_sum,
label=paste0(cat_region,'\n',value_sum)))+
scale_x_discrete(
limits=c('blank','blank','blank','category','sub_category'))+
coord_polar(theta='y')
与其他美学一样,您可以通过比例设置可能值的范围,例如在 size
审美的情况下,通过 scale_size
的 range
参数。通过减小范围的最大值,您可以缩小所有标签。
library(tidyverse)
plot_data %>%
mutate(sub_category = if_else(category == "A",
sub_category, category
)) %>%
pivot_longer(names_to = "title", values_to = "cat_region", -value) %>%
filter(!(title == "sub_category" & cat_region %in% c("B", "C"))) %>%
group_by(title, cat_region) %>%
summarise(value_sum = sum(value)) %>%
ggplot(aes(
x = title, y = value_sum, fill = cat_region,
group = interaction(title, cat_region)
)) +
geom_col() +
geom_text(position=position_stack(0.5),
aes(
size = value_sum,
label = paste0(cat_region, "\n", value_sum)
)
) +
scale_x_discrete(
limits = c("blank", "blank", "blank", "category", "sub_category")
) +
scale_size(range = c(1, 3.5)) +
coord_polar(theta = "y")
#> `summarise()` has grouped output by 'title'. You can override using the
#> `.groups` argument.