如何为 facet_wrap(ed) geom_count 图添加标签?
How to add labels to facet_wrap(ed) geom_count plot?
我正在使用 facet_wrap 创建多面图。我希望文本标签包含在气泡内。相反,总数似乎包含在标签中 - 即所有图表都具有相同的数字但气泡大小不同(这是正确的)。
(编辑)
我的代码:
Category1 <- c('A','B','C','A','B','C','A','B','C','A','B','C','A','B','C','A','B','C','A','B')
Category2 <- c('W','V','W','V','W','V','W','V','W','V','W','V','W','V','W','V','W','V','W','V')
Class <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
df <- data.frame(Category1, Category2, Class)
g <- ggplot(df, aes(Category1, Category2))
g <- g + facet_wrap(Class ~ ., nrow = 3) + geom_count(col="tomato3", show.legend=F) + scale_size_continuous(range = c(5, 10))
labs(subtitle="Count Plot", y="Category2", x="Category1", title="Cat1 vs Cat2")
g
g2 <- g + geom_text(data=ggplot_build(g)$data[[1]], aes(x, y, label=n), size=2) #+ scale_size(range = c(5, 15))
g2
我希望气泡的大小将由气泡内的文本指示。但实际结果是所有图形都具有相同的数字。我希望小气泡的数量与其大小成正比。
问题是您使用 ggplot_build
数据的代码没有与原始代码相同的类别。您需要事先创建一个计数数据并将其用于绘图。
创建计数数据
library(tidyverse)
df_count <- df %>%
count(Class, Category1, Category2)
情节
有两种方法可以合并这些新数据。
方法一
我展示的第一个例子是同时使用 df
和 df_count
。此方法将对您的代码进行最少的修改:
g <- ggplot(df, aes(Category1, Category2))
g <- g + facet_wrap(Class ~ ., nrow = 3) + geom_count(col="tomato3", show.legend=F) +
geom_text(data = df_count, aes(Category1, Category2, label=n), size=2) +
scale_size_continuous(range = c(5, 10)) +
labs(subtitle="Count Plot", y="Category2", x="Category1", title="Cat1 vs Cat2")
g
添加了行 geom_text(data = df_count, aes(Category1, Category2, label=n), size=2) +
。
方法二
此方法仅使用计数数据。它使用 geom_point()
而不是 geom_count()
并使用变量 n
更改大小。这种方法在代码可读性方面可能更好。
g_alternative <- ggplot(df_count, aes(Category1, Category2, label = n)) +
facet_wrap(Class ~ ., nrow = 3) +
geom_point(col="tomato3", aes(size = n), show.legend=F) +
geom_text() +
scale_size_continuous(range = c(5, 10)) +
labs(subtitle="Count Plot", y="Category2", x="Category1", title="Cat1 vs Cat2")
g_alternative
输出如下所示:
我正在使用 facet_wrap 创建多面图。我希望文本标签包含在气泡内。相反,总数似乎包含在标签中 - 即所有图表都具有相同的数字但气泡大小不同(这是正确的)。
(编辑)
我的代码:
Category1 <- c('A','B','C','A','B','C','A','B','C','A','B','C','A','B','C','A','B','C','A','B')
Category2 <- c('W','V','W','V','W','V','W','V','W','V','W','V','W','V','W','V','W','V','W','V')
Class <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
df <- data.frame(Category1, Category2, Class)
g <- ggplot(df, aes(Category1, Category2))
g <- g + facet_wrap(Class ~ ., nrow = 3) + geom_count(col="tomato3", show.legend=F) + scale_size_continuous(range = c(5, 10))
labs(subtitle="Count Plot", y="Category2", x="Category1", title="Cat1 vs Cat2")
g
g2 <- g + geom_text(data=ggplot_build(g)$data[[1]], aes(x, y, label=n), size=2) #+ scale_size(range = c(5, 15))
g2
我希望气泡的大小将由气泡内的文本指示。但实际结果是所有图形都具有相同的数字。我希望小气泡的数量与其大小成正比。
问题是您使用 ggplot_build
数据的代码没有与原始代码相同的类别。您需要事先创建一个计数数据并将其用于绘图。
创建计数数据
library(tidyverse)
df_count <- df %>%
count(Class, Category1, Category2)
情节
有两种方法可以合并这些新数据。
方法一
我展示的第一个例子是同时使用 df
和 df_count
。此方法将对您的代码进行最少的修改:
g <- ggplot(df, aes(Category1, Category2))
g <- g + facet_wrap(Class ~ ., nrow = 3) + geom_count(col="tomato3", show.legend=F) +
geom_text(data = df_count, aes(Category1, Category2, label=n), size=2) +
scale_size_continuous(range = c(5, 10)) +
labs(subtitle="Count Plot", y="Category2", x="Category1", title="Cat1 vs Cat2")
g
添加了行 geom_text(data = df_count, aes(Category1, Category2, label=n), size=2) +
。
方法二
此方法仅使用计数数据。它使用 geom_point()
而不是 geom_count()
并使用变量 n
更改大小。这种方法在代码可读性方面可能更好。
g_alternative <- ggplot(df_count, aes(Category1, Category2, label = n)) +
facet_wrap(Class ~ ., nrow = 3) +
geom_point(col="tomato3", aes(size = n), show.legend=F) +
geom_text() +
scale_size_continuous(range = c(5, 10)) +
labs(subtitle="Count Plot", y="Category2", x="Category1", title="Cat1 vs Cat2")
g_alternative
输出如下所示: