订购多面点图

时间:2015-04-24 14:56:36

标签: r ggplot2 data-visualization

这是我使用以下代码创建的多面点图(在帖子末尾提供的dput格式的数据)。

ggplot(na.omit(df), aes(x = N, y = journal, group = journal, colour = studyType)) + 
geom_segment(aes(yend = journal), xend=0, colour="grey50") +
geom_point(alpha = .8, size = 4) + 
scale_color_manual(values = cp) + 
theme_bw() +
facet_grid(.~studyType) + 
theme(panel.grid.major.y = element_blank()) 

enter image description here

我现在的兴趣是对行(Y轴)进行重新排序,因此它们基于整体N按降序排列,计算为整个面的总和。因此,BJoSW期刊将首先列出,然后H& SW将排在第二位,依此类推。在回顾了有关该主题的其他帖子后,该解决方案似乎涉及使用整体摘要变量重新排序y变量journal

j.order <- df$journal[order(df$N.sort, decreasing = TRUE)]
df$journal <- factor(df$journal, levels = j.order) 

这会产生一条警告消息(不推荐使用因子中的重复级别),因此显然不对。虽然这个数字越来越接近我想要的,但ggplot调用也在重复级别上给我警告信息。所以,我猜我没有正确设置数据或者我是如何处理缺失值的。非常感谢任何帮助。

structure(list(journal = structure(c(43L, 43L, 35L, 35L, 30L, 
30L, 30L, 37L, 37L, 47L, 1L, 1L, 1L, 1L, 48L, 20L, 20L, 20L, 
13L, 13L, 13L, 8L, 8L, 45L, 45L, 16L, 16L, 16L, 16L, 26L, 26L, 
39L, 39L, 10L, 10L, 10L, 49L, 23L, 23L, 23L, 28L, 28L, 33L, 33L, 
41L, 41L, 50L, 5L, 5L, 5L), .Label = c("BJoSW", "BJoSW", "BJoSW", 
"BJoSW", "JoESW", "JoESW", "JoESW", "Ch&S", "Ch&S", "H&SW", "H&SW", 
"H&SW", "C&FSW", "C&FSW", "C&FSW", "CSWJ", "CSWJ", "CSWJ", "CSWJ", 
"C&ASWJ", "C&ASWJ", "C&ASWJ", "IntJoSW", "IntJoSW", "IntJoSW", 
"EJoSW", "EJoSW", "InSW", "InSW", "Affl", "Affl", "Affl", "JoBSW", 
"JoBSW", "AdviSW", "AdviSW", "Aret", "Aret", "FmiS", "FmiS", 
"JoCP", "JoCP", "AdmiSW", "AdmiSW", "CJoSW", "CJoSW", "ASW&PR", 
"CSWR", "IndJoSW", "JoE&CDiSW"), class = "factor"), studyType = c("quasi", 
"sr", "quasi", "rct", "quasi", "rct", "sr", "quasi", "rct", "rct", 
"meta", "quasi", "rct", "sr", "rct", "quasi", "rct", "sr", "quasi", 
"rct", "sr", "quasi", "rct", "quasi", "sr", "meta", "quasi", 
"rct", "sr", "quasi", "rct", "quasi", "rct", "quasi", "rct", 
"sr", "meta", "quasi", "rct", "sr", "quasi", "rct", "quasi", 
"rct", "quasi", "rct", "quasi", "meta", "quasi", "rct"), N = c(1L, 
1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 7L, 7L, 4L, 1L, 3L, 1L, 
2L, 3L, 1L, 3L, 5L, 6L, 1L, 1L, 3L, 1L, 1L, 2L, 1L, 4L, 2L, 1L, 
1L, 6L, 3L, 1L, 2L, 3L, 1L, 3L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 
3L), N.sort = c(2L, 2L, 3L, 3L, 4L, 4L, 4L, 3L, 3L, 1L, 19L, 
19L, 19L, 19L, 1L, 6L, 6L, 6L, 7L, 7L, 7L, 11L, 11L, 2L, 2L, 
7L, 7L, 7L, 7L, 5L, 5L, 3L, 3L, 10L, 10L, 10L, 1L, 6L, 6L, 6L, 
5L, 5L, 4L, 4L, 3L, 3L, 1L, 14L, 14L, 14L)), .Names = c("journal", 
"studyType", "N", "N.sort"), row.names = c(NA, -50L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

试试这个:

j.order <- unique(df$journal[order(df$N.sort, decreasing = TRUE)])
df$journal <- factor(df$journal, levels = j.order)

注意:有点匆忙,稍后会更新解释......

修改

问题是现有的data.frame中有重复的级别。参见:

#Existing levels from posted dput data.frame
table(duplicated(levels(df$journal)))
#FALSE  TRUE 
#   22    28 

我们需要修复两件事,制作唯一级别,然后按数字变量N.sort对它们进行排序。

#make unique order based on N.sort
j.order <- unique(as.character(df$journal)[order(df$N.sort, decreasing = TRUE)])

#reassign journal as factor with new levels
df$journal <- factor(df$journal, levels = j.order)

有序情节: enter image description here