如何使多面条形图使用堆积条形图?
How can I make a faceted bar chart use stacked bars?
我确定我在这里遗漏了一些明显的东西,但是已经尝试了一百万次不同的迭代并且似乎无法找到正确的配方。我正在处理调查数据并在 facet_grid 中对其进行分面处理,现在运行良好:
# ingest some data
df <- structure(list(Q52_bin = structure(c(3L, 2L, 2L, 2L, 2L, 2L), .Label = c("low",
"medium", "high"), class = "factor"), Q53_bin = structure(c(2L,
3L, 2L, 2L, 2L, 2L), .Label = c("low", "medium", "high"), class = "factor"),
Q57_bin = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("low",
"medium", "high"), class = "factor"), Q4 = c("A little",
"Some", "Some", "A great deal", "A lot", "Some")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
# make column names coherent and simplified
names(df) <- c("Q52_bin", "Q53_bin", "Q57_bin", "response")
df %>%
pivot_longer(!response, names_to = "bin_name", values_to = "b") %>%
count(response, bin_name, b) %>%
ggplot(aes(x=n,y=response)) +
geom_col(position="stack", stat="identity") +
scale_fill_discrete()+
facet_grid(vars(b), vars(bin_name)) + labs(caption = "How much have you thought about climate change before today?", x = "", y = "")
然而,我真正想要的是将这些条形堆叠成水平条形,每个面内有一个条形图,使用颜色来指示不同的李克特响应。我在想我可以简单地切换到:
ggplot(aes(x=n,fill=response)) +
geom_bar(position="fill") +
但输出不连贯(数以亿计的窄条)。关于如何根据需要切换它有什么建议吗?
不确定我是否理解你的问题,但我想这就是你想要的。
示例代码:
df %>%
pivot_longer(!response, names_to = "bin_name", values_to = "b") %>%
count(response, bin_name, b) %>%
ggplot(aes(x=n,y=b, fill=response)) +
geom_bar(position="stack", stat="identity") +
scale_fill_discrete()+
facet_grid(vars(b), vars(bin_name)) +
labs(caption = "How much have you thought about climate change before today?", x = "", y = "")
剧情:
如果改变
facet_grid(~b) # b takes values medium and high
response bin_name b n
<chr> <chr> <fct> <int>
1 A great deal Q52_bin medium 1
2 A great deal Q53_bin medium 1
3 A great deal Q57_bin medium 1
4 A little Q52_bin high 1
5 A little Q53_bin medium 1
6 A little Q57_bin medium 1
7 A lot Q52_bin medium 1
8 A lot Q53_bin medium 1
9 A lot Q57_bin medium 1
10 Some Q52_bin medium 3
11 Some Q53_bin medium 2
12 Some Q53_bin high 1
13 Some Q57_bin medium 3
如果你想忽略 Q52_bin...-s
为了更好地使用不同的主题
library(ggtheme)
theme_gdocs()
我确定我在这里遗漏了一些明显的东西,但是已经尝试了一百万次不同的迭代并且似乎无法找到正确的配方。我正在处理调查数据并在 facet_grid 中对其进行分面处理,现在运行良好:
# ingest some data
df <- structure(list(Q52_bin = structure(c(3L, 2L, 2L, 2L, 2L, 2L), .Label = c("low",
"medium", "high"), class = "factor"), Q53_bin = structure(c(2L,
3L, 2L, 2L, 2L, 2L), .Label = c("low", "medium", "high"), class = "factor"),
Q57_bin = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("low",
"medium", "high"), class = "factor"), Q4 = c("A little",
"Some", "Some", "A great deal", "A lot", "Some")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
# make column names coherent and simplified
names(df) <- c("Q52_bin", "Q53_bin", "Q57_bin", "response")
df %>%
pivot_longer(!response, names_to = "bin_name", values_to = "b") %>%
count(response, bin_name, b) %>%
ggplot(aes(x=n,y=response)) +
geom_col(position="stack", stat="identity") +
scale_fill_discrete()+
facet_grid(vars(b), vars(bin_name)) + labs(caption = "How much have you thought about climate change before today?", x = "", y = "")
然而,我真正想要的是将这些条形堆叠成水平条形,每个面内有一个条形图,使用颜色来指示不同的李克特响应。我在想我可以简单地切换到:
ggplot(aes(x=n,fill=response)) +
geom_bar(position="fill") +
但输出不连贯(数以亿计的窄条)。关于如何根据需要切换它有什么建议吗?
不确定我是否理解你的问题,但我想这就是你想要的。
示例代码:
df %>%
pivot_longer(!response, names_to = "bin_name", values_to = "b") %>%
count(response, bin_name, b) %>%
ggplot(aes(x=n,y=b, fill=response)) +
geom_bar(position="stack", stat="identity") +
scale_fill_discrete()+
facet_grid(vars(b), vars(bin_name)) +
labs(caption = "How much have you thought about climate change before today?", x = "", y = "")
剧情:
如果改变
facet_grid(~b) # b takes values medium and high
response bin_name b n
<chr> <chr> <fct> <int>
1 A great deal Q52_bin medium 1
2 A great deal Q53_bin medium 1
3 A great deal Q57_bin medium 1
4 A little Q52_bin high 1
5 A little Q53_bin medium 1
6 A little Q57_bin medium 1
7 A lot Q52_bin medium 1
8 A lot Q53_bin medium 1
9 A lot Q57_bin medium 1
10 Some Q52_bin medium 3
11 Some Q53_bin medium 2
12 Some Q53_bin high 1
13 Some Q57_bin medium 3
如果你想忽略 Q52_bin...-s
为了更好地使用不同的主题
library(ggtheme)
theme_gdocs()