gpt4 book ai didi

r - 如何在两个不同类别之间绘制 facet_wrap 中的国家/地区?

转载 作者:行者123 更新时间:2023-12-04 07:32:23 24 4
gpt4 key购买 nike

我使用 facet_wrap 绘制了六个国家/地区的估计值。我想加一层,把国家分成两组ab同时保留 facet_wrap 选项。这意味着我希望看到两组中每个国家/地区分开的地块ab .
这是所有国家在一起时的情节:

df %>%
ggplot(aes(x= estimate, y=term)) +
geom_point(mapping=aes(x=estimate, y=term)) +
facet_wrap( ~ cntry, strip.position = "bottom")
enter image description here
我尝试使用 ggforce 将它们分开.正如我们所看到的,它实际上确实将它们分开了,但实际上所有国家都混合在一个图中。有没有办法让每个国家在两个不同的群体中单独绘制?
这是代码和情节:
df %>%
ggplot(aes(x= estimate, y=term)) +
geom_point(mapping=aes(x=estimate, y=term)) +
facet_wrap( ~ cntry, strip.position = "bottom") +
ggforce::facet_row(vars(cat_f), scales = 'free_x', strip.position = 'bottom')
enter image description here
这是数据:
structure(
list(
cntry =
structure(
c("Austria", "Belgium", "Italy", "Switzerland", "Spain", "Austria","Belgium", "Italy", "Switzerland", "Spain", "Denmark"),
format.stata = "%15s"
),
estimate =
structure(
c(1, 1.5, 2, 1.20000004768372, 1.70000004768372, 4, 5.5, 6, 3, 3.09999990463257, 4.30000019073486),
format.stata = "%8.0g"
),
cat_f =
structure(c("Widening gap",
"Widening gap", "Widening gap", "Widening gap", "Widening gap",
"No change", "No change", "No change", "No change", "No change",
"No change"),
format.stata = "%13s"
),
term =
structure(c("2009", "2009", "2009", "2009", "2009", "2008", "2008", "2008", "2008", "2008", "2008"),
format.stata = "%15s"
)
),
row.names = c(NA, -11L),
class = "data.frame"
)

最佳答案

我不明白预期的输出;这是你想要做的吗?

library(tidyverse)
df <- structure(list(cntry = structure(c("Austria", "Belgium", "Italy",
"Switzerland", "Spain", "Austria", "Belgium", "Italy", "Switzerland",
"Spain", "Denmark"), format.stata = "%15s"), estimate = structure(c(1,
1.5, 2, 1.20000004768372, 1.70000004768372, 4, 5.5, 6, 3, 3.09999990463257,
4.30000019073486), format.stata = "%8.0g"), cat_f = structure(c("Widening gap",
"Widening gap", "Widening gap", "Widening gap", "Widening gap",
"No change", "No change", "No change", "No change", "No change",
"No change"), format.stata = "%13s"), term = structure(c("2009",
"2009", "2009", "2009", "2009", "2008", "2008", "2008", "2008",
"2008", "2008"), format.stata = "%15s")), row.names = c(NA, -11L
), class = "data.frame")
df %>%
ggplot(aes(x= estimate, y=term)) +
geom_point() +
facet_grid(cols = vars(cat_f), rows = vars(cntry))
example_1.png

关于r - 如何在两个不同类别之间绘制 facet_wrap 中的国家/地区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67882371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com