如何使用 ggplot 中的数字变量排序的分组分类变量生成子图?
How to generate sub-plots with grouped categorical variable sorted by a numeric variable in ggplot?
我有一个数据框 text
,每个文件 file_num = 1 or 2 or 3
中出现 n
个 word
。我想使用 ggplot 生成三个子图,一个对应 file_num
的每个值,y 轴为 word
,x 轴为频率 n
。我希望根据每个 file_num
观察到的 n
的增加或减少值对每个子图进行排序。我尝试了许多不同的方法来解决这个看似微不足道的问题,但都没有成功。
这是我的dput
测试数据:
structure(list(file_num = c("1", "1", "1", "1", "2", "2", "2",
"2", "2", "3", "3", "3", "3", "3"), word = c("test", "quality",
"page", "limit", "information", "limit", "test", "instruments",
"quality", "limit", "test", "effective", "page", "system"), n = c(5,
35, 55, 75, 20, 30, 40, 60, 70, 101, 201, 301, 401, 501)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -14L), spec = structure(list(
cols = list(file_num = structure(list(), class = c("collector_character",
"collector")), word = structure(list(), class = c("collector_character",
"collector")), n = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
这是我尝试过的:
library(tidytext)
library(stringr)
library(pdftools)
library(dplyr)
library(purrr)
library(ggplot2)
library(forcats)
text %>% group_by(file_num) %>% arrange(file_num, desc(n)) %>%
ggplot(.,aes(factor(word,levels = unique(word)), n, fill = file_num)) +
geom_bar(stat = "identity", position = "dodge") +
scale_x_discrete("Word") +
scale_y_continuous("n") + coord_flip() +
facet_grid(rows = vars(file_num), scales = "free")
这是使用上述代码在使用 dput
数据创建的数据帧 text
上生成的图。它为 file_num = 1 显示了所需的结果(word
按 n
的递增值排序),但对于 file_num = 2 或 3 则没有:
您可以使用 ggcharts
包非常简单地实现此 "ordered per facet",在您的数据上使用以下代码:
library(ggcharts)
bar_chart(data = text, x = word, y = n,
fill = file_num,
facet = file_num,
horizontal = TRUE
)
这会产生下图:
请告诉我这是否是您想要的。
更新:
bar_chart
创建的对象属于classggplot
,如下所示:
class(chart)
[1] "gg" "ggplot"
这意味着可以使用 ggplot2
函数来改变图形,例如:
chart +
guides(fill=FALSE) + ## remove legend
ggtitle("My new title") + ## add title
theme_linedraw() +
theme(strip.background = element_rect(colour = "red", size = 2))
生成如下图片(仅供参考):
感谢@Tjebo 为我指明了正确的方向。这是一个基于 ggplot
的有效解决方案。它确实需要在 ggplot
.
中使用之前保存修改后的数据帧 text
让我知道是否有办法将修改后的数据帧直接通过管道传输到 ggplot
text1 <- text %>% ungroup %>% arrange(file_num, n) %>%
mutate(order = row_number()) # create variable order
ggplot(text1,aes(order, n, fill = file_num)) +
geom_bar(stat = "identity", show.legend = FALSE) +
scale_x_continuous(
breaks = text1$order,
labels = text1$word,
expand = c(0,0),
xlab("Word")) +
facet_grid(file_num ~ ., scales = "free") +
coord_flip()
输出图:
我有一个数据框 text
,每个文件 file_num = 1 or 2 or 3
中出现 n
个 word
。我想使用 ggplot 生成三个子图,一个对应 file_num
的每个值,y 轴为 word
,x 轴为频率 n
。我希望根据每个 file_num
观察到的 n
的增加或减少值对每个子图进行排序。我尝试了许多不同的方法来解决这个看似微不足道的问题,但都没有成功。
这是我的dput
测试数据:
structure(list(file_num = c("1", "1", "1", "1", "2", "2", "2",
"2", "2", "3", "3", "3", "3", "3"), word = c("test", "quality",
"page", "limit", "information", "limit", "test", "instruments",
"quality", "limit", "test", "effective", "page", "system"), n = c(5,
35, 55, 75, 20, 30, 40, 60, 70, 101, 201, 301, 401, 501)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -14L), spec = structure(list(
cols = list(file_num = structure(list(), class = c("collector_character",
"collector")), word = structure(list(), class = c("collector_character",
"collector")), n = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
这是我尝试过的:
library(tidytext)
library(stringr)
library(pdftools)
library(dplyr)
library(purrr)
library(ggplot2)
library(forcats)
text %>% group_by(file_num) %>% arrange(file_num, desc(n)) %>%
ggplot(.,aes(factor(word,levels = unique(word)), n, fill = file_num)) +
geom_bar(stat = "identity", position = "dodge") +
scale_x_discrete("Word") +
scale_y_continuous("n") + coord_flip() +
facet_grid(rows = vars(file_num), scales = "free")
这是使用上述代码在使用 dput
数据创建的数据帧 text
上生成的图。它为 file_num = 1 显示了所需的结果(word
按 n
的递增值排序),但对于 file_num = 2 或 3 则没有:
您可以使用 ggcharts
包非常简单地实现此 "ordered per facet",在您的数据上使用以下代码:
library(ggcharts)
bar_chart(data = text, x = word, y = n,
fill = file_num,
facet = file_num,
horizontal = TRUE
)
这会产生下图:
请告诉我这是否是您想要的。
更新:
bar_chart
创建的对象属于classggplot
,如下所示:
class(chart)
[1] "gg" "ggplot"
这意味着可以使用 ggplot2
函数来改变图形,例如:
chart +
guides(fill=FALSE) + ## remove legend
ggtitle("My new title") + ## add title
theme_linedraw() +
theme(strip.background = element_rect(colour = "red", size = 2))
生成如下图片(仅供参考):
感谢@Tjebo 为我指明了正确的方向。这是一个基于 ggplot
的有效解决方案。它确实需要在 ggplot
.
text
让我知道是否有办法将修改后的数据帧直接通过管道传输到 ggplot
text1 <- text %>% ungroup %>% arrange(file_num, n) %>%
mutate(order = row_number()) # create variable order
ggplot(text1,aes(order, n, fill = file_num)) +
geom_bar(stat = "identity", show.legend = FALSE) +
scale_x_continuous(
breaks = text1$order,
labels = text1$word,
expand = c(0,0),
xlab("Word")) +
facet_grid(file_num ~ ., scales = "free") +
coord_flip()
输出图: