R 如何对具有两个因子变量的方面进行排序?
我正在为23个国家做一些估计。如下图所示,国家是随机绘制的。我想从R 如何对具有两个因子变量的方面进行排序?,r,ggplot2,R,Ggplot2,我正在为23个国家做一些估计。如下图所示,国家是随机绘制的。我想从差距扩大组的国家开始订购,然后是无变化和差距缩小 我在这里试图遵循这个建议,但由于某些原因,它没有起作用 此外,如果可能的话,我想用一个词来表示每组中的所有国家,而不是一次又一次地表示每个国家的“差距扩大”。我试着用ggforce来做这件事,但国家并没有被划分出来 有人能帮忙吗 代码如下: library(ggplot) library(ggforce) 以下是我的情节: 下面是一个例子,我尝试使用ggforce将其分为三类。
差距扩大
组的国家开始订购,然后是无变化
和差距缩小
我在这里试图遵循这个建议,但由于某些原因,它没有起作用
此外,如果可能的话,我想用一个词来表示每组中的所有国家,而不是一次又一次地表示每个国家的“差距扩大”。我试着用ggforce
来做这件事,但国家并没有被划分出来
有人能帮忙吗
代码如下:
library(ggplot)
library(ggforce)
以下是我的情节:
下面是一个例子,我尝试使用ggforce将其分为三类。然而,一旦我将其分为三类,它就不再是国家了
df %>%
ggplot(aes(x= estimate, y=term)) +
geom_point(mapping=aes(x=estimate, y=term), size=2.3, shape=21, fill="black") +
geom_vline(xintercept=0, color = "black", linetype= "dotted") +
facet_wrap(vars(cntry,cat_f), scales = "free_x", strip.position = "bottom")
以下是数据:
df %>%
ggplot(aes(x= estimate, y=term)) +
geom_point(mapping=aes(x=estimate, y=term), size=2.3, shape=21, fill="black") +
geom_vline(xintercept=0, color = "black", linetype= "dotted") +
facet_wrap( ~ cat_f + cntry, scales = "free_x", strip.position = "bottom") +
ggforce::facet_row(vars(cat_f), scales = 'free_x', strip.position = 'bottom')
要对其进行排序,请尝试按如下方式更改镶嵌面环绕:
facet\u wrap(~cat\u f+cntry,scales=“free\u x”,strip.position=“bottom”)
要删除标签,可以使用strip.text.x
删除这两个标签,然后使用geom_text
将国家名称添加到绘图中:
theme(strip.text.x=element\u blank())+geom\u text(aes(label=cntry,x=Inf,y=Inf),vjust=2,hjust=2)
您必须根据绘图的最终大小调整参数x、y、vjust和hjust。
我的建议是以颜色和形状的形式呈现有关间隙的信息,如下图所示:
structure(list(cntry = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L,
4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L,
12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L,
18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 23L), .Label = c("Austria",
"Belgium", "Switzerland", "Czech Republic", "Germany", "Denmark",
"Estonia", "Greece", "Spain", "Finland", "France", "Hungary",
"Ireland", "Iceland", "Italy", "Luxembourg", "Netherlands", "Norway",
"Poland", "Portugal", "Sweden", "Slovakia", "United Kingdom"), class = "factor"),
estimate = c(-0.0913140419553834, -0.118306201172409, -0.0239667799007915,
-0.0443156002183645, -0.0104216581052402, -0.0548359509121889,
0.0131305888416785, 0.0315223511324752, -0.0190273624021191,
-0.0186414728272051, 0.0411967940816404, 0.0231043131714301,
-0.0171116200795716, -0.0154689392721703, 0.030292299601443,
0.0169741956257371, -0.105814083412474, -0.140455990414314,
0.0647774293035408, 0.0563998778231824, -0.0235327514882885,
0.00143103206722573, 0.000657080813472903, -0.00615188723709141,
-0.0262666239250381, 0.0217722749404904, 0.0056080759613924,
-0.0717368274077352, 0.00814944419371703, -0.0994925665252663,
0.0036407509645274, 0.0265557284220869, -0.0158617373908501,
-0.0238087514417458, 0.0167937459496994, -0.00390159593739075,
0.0535891490419863, 0.0127836449750844, 0.0594230533290278,
0.0292983217374143, -0.0240741202575357, -0.0210028856455069,
0.0442436113476246, 0.0569644358417757, 0.0137935174123489,
0.00173110517002347), cat_f = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 3L, 3L, 1L, 1L, 1L,
1L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 1L, 1L,
1L, 1L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 2L, 2L), .Label = c("Widening gap",
"No change", "Contracting gap"), class = "factor"), term = c("year2008:occup3",
"year2009:occup3", "year2008:occup3", "year2009:occup3",
"year2008:occup3", "year2009:occup3", "year2008:occup3",
"year2009:occup3", "year2008:occup3", "year2009:occup3",
"year2008:occup3", "year2009:occup3", "year2008:occup3",
"year2009:occup3", "year2008:occup3", "year2009:occup3",
"year2008:occup3", "year2009:occup3", "year2008:occup3",
"year2009:occup3", "year2008:occup3", "year2009:occup3",
"year2008:occup3", "year2009:occup3", "year2008:occup3",
"year2009:occup3", "year2008:occup3", "year2009:occup3",
"year2008:occup3", "year2009:occup3", "year2008:occup3",
"year2009:occup3", "year2008:occup3", "year2009:occup3",
"year2008:occup3", "year2009:occup3", "year2008:occup3",
"year2009:occup3", "year2008:occup3", "year2009:occup3",
"year2008:occup3", "year2009:occup3", "year2008:occup3",
"year2009:occup3", "year2008:occup3", "year2009:occup3")), row.names = c(NA,
-46L), class = "data.frame")
谢谢您的帮助。我试着用
ggfocre
来划分这三个类别(缩小差距、不变、扩大差距)。但是现在它不再在策划国家了,你知道为什么会发生这种情况吗?对不起,我还没有用过ggforce
。您是否试图为每个类别(收缩间隙、无变化、加宽间隙)设置一行?是,为收缩间隙、无变化和加宽间隙的每行设置多个图形。每个图表代表一个属于三个类别之一的国家。您有13个正在扩大的差距,4个没有变化,6个正在缩小的差距。只要准确地告诉我您希望如何安排它们(列和行),我就可以试着为您做这件事。或者,您可以在上面的图中使用颜色和形状来区分行为,和/或在图中添加加宽间隙、无变化和收缩间隙的信息。非常感谢,如果您可以按照您想要的任何顺序向我展示颜色方式
ggplot(dt, aes(x= estimate, y=term)) +
geom_point(mapping=aes(x=estimate, y=term, shape = cat_f, color = cat_f), size=3) +
geom_vline(xintercept=0, color = "black", linetype= "dotted") +
facet_wrap( ~ cat_f + cntry, scales = "free_x", strip.position = "bottom") +
theme(strip.text.x = element_blank()) + theme(legend.position = c(0.8,0.1)) +
geom_text(aes(label = cntry, x = -Inf, y = Inf), vjust = 2, hjust = -1)