如何在 'treemapify' 中更改标签 color/size
How to change label color/size in 'treemapify'
在'treemapify'中,我使用函数'paste0'组合三个变量作为标签,现在我想分别更改字体颜色和大小(如下代码中的注释)。
任何人都可以帮忙吗?谢谢!
library(ggplot2)
library(treemapify)
library(formattable)
plot_data <- data.frame(
region=c("a","b","c","d","e","f"),
amount=c(3,2,1,7,2,4),
increase=c(0.5,0.1,0.7,0.4,0.3,0.9))
plot_data %>% ggplot(aes(area=amount,fill=region,
label=paste0(region, #' want the font color 'darkblue'
'\n',
comma(amount,1), # want the font color 'grey60'
'\n',
percent(increase,1) # want the font color 'white'
)))+
geom_treemap()+geom_treemap_text()
这是一个相当乏味的解决方案,但它应该为您提供所需的灵活性:
ggplot(plot_data, aes(area = amount, fill = region)) +
geom_treemap() +
geom_treemap_text(aes(label = region), color = "darkblue") +
geom_treemap_text(aes(label = scales::comma(amount, 1)),
color = "grey60", padding.y = unit(10, "mm")) +
geom_treemap_text(aes(label = scales::percent(increase, 1)), color = "white",
padding.y = unit(20, "mm"))
在'treemapify'中,我使用函数'paste0'组合三个变量作为标签,现在我想分别更改字体颜色和大小(如下代码中的注释)。 任何人都可以帮忙吗?谢谢!
library(ggplot2)
library(treemapify)
library(formattable)
plot_data <- data.frame(
region=c("a","b","c","d","e","f"),
amount=c(3,2,1,7,2,4),
increase=c(0.5,0.1,0.7,0.4,0.3,0.9))
plot_data %>% ggplot(aes(area=amount,fill=region,
label=paste0(region, #' want the font color 'darkblue'
'\n',
comma(amount,1), # want the font color 'grey60'
'\n',
percent(increase,1) # want the font color 'white'
)))+
geom_treemap()+geom_treemap_text()
这是一个相当乏味的解决方案,但它应该为您提供所需的灵活性:
ggplot(plot_data, aes(area = amount, fill = region)) +
geom_treemap() +
geom_treemap_text(aes(label = region), color = "darkblue") +
geom_treemap_text(aes(label = scales::comma(amount, 1)),
color = "grey60", padding.y = unit(10, "mm")) +
geom_treemap_text(aes(label = scales::percent(increase, 1)), color = "white",
padding.y = unit(20, "mm"))