我想知道是否可以在ggplot中的图例中手动定位标签?
我的例子是这样的:我有一个关于国家的数据,我正在为每个大陆做一个100%的堆积条,所以我有:
dt <- data.table(continent = c(rep('Africa', 2), rep('Asia', 3), rep('Europe', 4)),
country = c('Nigeria', 'Kenya',
'China', 'India', 'Japan',
'Germany', 'Sweden', 'Spain', 'Croatia'),
value = runif(9, 0, 10),
number=(1:9))
ggplot(data=dt,
aes(x = continent, y = value, fill = as.factor(number))) +
geom_bar(stat = "identity", position = "fill", color='white', width=0.3 ) +
labs(x = '', y = 'Percentage') +
scale_y_continuous(expand=c(0,0)) +
scale_fill_manual('Country',
labels = dt[, country],
values = (grDevices::colorRampPalette(c('#BB16A3', '#f8e7f5')))(9)) +
theme(legend.position='bottom', aspect.ratio = 1) +
guides(fill = guide_legend(title.position="top", title.hjust = 0.5, reverse=T)) +
coord_flip()
所以我的问题是,是否可以重新定位图例中的标签,以便每个大洲的国家都在一个单独的列中?还是单独一行?
谢谢!
最佳答案 我注意到每个洲都有不同数量的国家. ggplot()可以按行或按列填充图例矩阵,但我从未见过每行/每列中具有不同数量单元格的锯齿状矩阵.
但是,有可能破解看起来像锯齿状图例矩阵的东西.以下是一些实现.如果要按特定顺序对大陆/国家/地区标签进行排序,或者更改图例键之间的间距等,您可能希望调整参数.
准备工作:
# define fill mapping so that it can be re-used for both top plot & legend
scale_fill_country <-
scale_fill_manual(labels = dt[, country],
values = (grDevices::colorRampPalette(c('#BB16A3', '#f8e7f5')))(9))
# create top plot (without any legend)
gg.plot <- ggplot(data = dt,
aes(x = continent, y = value, fill = as.factor(number))) +
#note: geom_col is equivalent to geom_bar(stat = "identity")
geom_col(position = "fill", color='white', width=0.3 ) +
labs(x = '', y = 'Percentage') +
scale_y_continuous(expand=c(0,0)) +
scale_fill_country +
theme_classic() +
theme(legend.position = "none") +
coord_flip()
修改图例的数据源:
library(dplyr)
dt.legend <- dt %>%
# pad with empty rows so that there are equal number of countries under
# each continent
group_by(continent) %>%
arrange(country) %>%
mutate(country.id = seq(1, n())) %>%
ungroup() %>%
tidyr::complete(continent, country.id, fill = list(country = " ")) %>%
# make each empty row distinct (within the same continent), & sort them
# after the original rows
rowwise() %>%
mutate(country = ifelse(country == " ",
paste0(rep.int(" ", country.id), collapse = ""),
country)) %>%
ungroup() %>%
mutate(country = forcats::fct_reorder(country, country.id))
> dt.legend
# A tibble: 12 x 5
continent country.id country value number
<chr> <int> <fct> <dbl> <int>
1 Africa 1 Kenya 2.02 2
2 Africa 2 Nigeria 7.17 1
3 Africa 3 " " NA NA
4 Africa 4 " " NA NA
5 Asia 1 China 3.21 3
6 Asia 2 India 5.59 4
7 Asia 3 Japan 9.31 5
8 Asia 4 " " NA NA
9 Europe 1 Croatia 0.0131 9
10 Europe 2 Germany 0.0775 6
11 Europe 3 Spain 3.98 8
12 Europe 4 Sweden 0.703 7
版本1:一行中的每个大陆,标题下面的图例键(如果您不希望显示与每行关联的大陆标签,请将axis.text.y = element_blank()添加到theme())
gg.legend.rows1 <- ggplot(data = dt.legend,
aes(x = country, y = continent,
fill = as.factor(number))) +
geom_tile(color = "white", size = 2) +
facet_wrap(~ continent, scales = "free", ncol = 1) +
scale_y_discrete(expand = c(0, 0)) +
scale_fill_country +
theme_minimal() +
theme(axis.title = element_blank(),
strip.text = element_blank(),
panel.grid = element_blank(),
legend.position = "none")
cowplot::plot_grid(gg.plot, gg.legend.rows1,
ncol = 1,
rel_heights = c(1, 0.3))
版本2:一行中的每个大陆,标注到图例右侧的标签(我想不出用这种方法获得大陆标签的方法,但我认为这个问题无论如何都不是必需的…)
gg.legend.rows2 <- ggplot(data = dt.legend,
aes(x = "", y = country, fill = as.factor(number))) +
geom_tile() +
scale_y_discrete(position = "right", expand = c(0, 0)) +
facet_wrap(~ interaction(continent, country, lex.order = TRUE),
scales = "free") +
scale_fill_country +
theme_minimal() +
theme(axis.title = element_blank(),
axis.text.x = element_blank(),
strip.text = element_blank(),
panel.grid = element_blank(),
panel.spacing = unit(0, "pt"),
legend.position = "none")
cowplot::plot_grid(gg.plot, gg.legend.rows2,
axis = "l", align = "v",
ncol = 1,
rel_heights = c(1, 0.2))
版本3:一列中的每个大陆,标注到图例键的右侧(如果您不希望显示与每列相关联的大陆标签,请将axis.text.x = element_blank()添加到theme())
gg.legend.columns <- ggplot(data = dt.legend,
aes(x = continent, y = forcats::fct_rev(country),
fill = as.factor(number))) +
geom_tile(color = "white", size = 2) +
facet_wrap(~ continent, scales = "free", nrow = 1) +
scale_x_discrete(position = "top", expand = c(0, 0)) +
scale_y_discrete(position = "right", expand = c(0, 0)) +
scale_fill_country +
theme_minimal() +
theme(axis.title = element_blank(),
strip.text = element_blank(),
panel.grid = element_blank(),
legend.position = "none")
cowplot::plot_grid(gg.plot, gg.legend.columns,
axis = "l", align = "v",
ncol = 1,
rel_heights = c(1, 0.3))