带有刻面和组填充的条形图中的顺序因子变量
Order factor variable in barplot with facet and group fill
我的数据是 table()
四个变量的输出(数据如下)。
我想为每个 V3 绘制 N,由 V1 填充并由 V2 分面。但是之前,V1应该被V4排序。
temp$V1 <- reorder(temp$V1, temp$V4)
ggplot(temp, aes(x=V3, y=N, fill=V1)) +
geom_bar(binwidth = 10, stat="identity") +
ylab("") + xlab("") +
facet_grid(. ~ V2) +
scale_fill_discrete(name = "Journal") +
theme(legend.title = element_text(colour="black", size=14),
legend.direction="horizontal",
legend.position="bottom",
strip.text.x = element_text(size = 14),
axis.text = element_text(colour="black", size=10),
axis.ticks.x = element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank()
)
但是条形元素仍然没有按照我想要的输出规则排序
顺序应该是JBF、JFI、JMCB、JF、JFE、RFS。我究竟做错了什么?尺寸是否太多?
这是数据:
V1 V2 V3 V4 N
JBF 1998 with 0 97
JFI 1998 with 0 21
JMCB 1998 with 0 62
JBF 1999 with 0 93
JFI 1999 with 0 20
JMCB 1999 with 0 37
JBF 2000 with 0 112
JFI 2000 with 0 29
JMCB 2000 with 0 65
JBF 2009 with 0 247
JFI 2009 with 0 40
JMCB 2009 with 0 126
JBF 2010 with 0 299
JFI 2010 with 0 27
JMCB 2010 with 0 110
JBF 2011 with 0 311
JFI 2011 with 0 34
JMCB 2011 with 0 120
JBF 1998 without 0 29
JMCB 1998 without 0 9
JBF 1999 without 0 62
JMCB 1999 without 0 16
JBF 2000 without 0 37
JFI 2000 without 0 2
JMCB 2000 without 0 20
JBF 2009 without 0 158
JFI 2009 without 0 2
JMCB 2009 without 0 23
JBF 2010 without 0 153
JFI 2010 without 0 2
JMCB 2010 without 0 28
JBF 2011 without 0 133
JFI 2011 without 0 1
JMCB 2011 without 0 12
JF 1998 with 1 133
JFE 1998 with 1 91
RFS 1998 with 1 44
JF 1999 with 1 134
JFE 1999 with 1 101
RFS 1999 with 1 72
JF 2000 with 1 147
JFE 2000 with 1 101
RFS 2000 with 1 74
JF 2009 with 1 154
JFE 2009 with 1 181
RFS 2009 with 1 284
JF 2010 with 1 128
JFE 2010 with 1 179
RFS 2010 with 1 218
JF 2011 with 1 103
JFE 2011 with 1 249
RFS 2011 with 1 189
JF 1998 without 1 12
JFE 1998 without 1 2
JF 1999 without 1 18
JFE 1999 without 1 2
RFS 1999 without 1 3
JF 2000 without 1 8
JFE 2000 without 1 6
RFS 2000 without 1 2
JF 2009 without 1 6
JFE 2009 without 1 4
RFS 2009 without 1 4
JFE 2010 without 1 9
RFS 2010 without 1 8
JF 2011 without 1 3
JFE 2011 without 1 7
RFS 2011 without 1 9
编辑:dput(temp)
的输出
structure(list(V1 = structure(c(3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L,
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 1L, 3L, 1L, 3L, 2L,
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 6L, 4L, 5L, 6L, 4L, 5L,
6L, 4L, 5L, 6L, 4L, 5L, 6L, 4L, 5L, 6L, 4L, 5L, 6L, 4L, 6L, 4L,
5L, 6L, 4L, 5L, 6L, 4L, 5L, 4L, 5L, 6L, 4L, 5L), .Label = c("JMCB",
"JFI", "JBF", "JFE", "RFS", "JF"), class = c("ordered", "factor"
)), V2 = c("1998", "1998", "1998", "1999", "1999", "1999", "2000",
"2000", "2000", "2009", "2009", "2009", "2010", "2010", "2010",
"2011", "2011", "2011", "1998", "1998", "1999", "1999", "2000",
"2000", "2000", "2009", "2009", "2009", "2010", "2010", "2010",
"2011", "2011", "2011", "1998", "1998", "1998", "1999", "1999",
"1999", "2000", "2000", "2000", "2009", "2009", "2009", "2010",
"2010", "2010", "2011", "2011", "2011", "1998", "1998", "1999",
"1999", "1999", "2000", "2000", "2000", "2009", "2009", "2009",
"2010", "2010", "2011", "2011", "2011"), V3 = c("with", "with",
"with", "with", "with", "with", "with", "with", "with", "with",
"with", "with", "with", "with", "with", "with", "with", "with",
"without", "without", "without", "without", "without", "without",
"without", "without", "without", "without", "without", "without",
"without", "without", "without", "without", "with", "with", "with",
"with", "with", "with", "with", "with", "with", "with", "with",
"with", "with", "with", "with", "with", "with", "with", "without",
"without", "without", "without", "without", "without", "without",
"without", "without", "without", "without", "without", "without",
"without", "without", "without"), V4 = c("0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"),
N = c(97L, 21L, 62L, 93L, 20L, 37L, 112L, 29L, 65L, 247L,
40L, 126L, 299L, 27L, 110L, 311L, 34L, 120L, 29L, 9L, 62L,
16L, 37L, 2L, 20L, 158L, 2L, 23L, 153L, 2L, 28L, 133L, 1L,
12L, 133L, 91L, 44L, 134L, 101L, 72L, 147L, 101L, 74L, 154L,
181L, 284L, 128L, 179L, 218L, 103L, 249L, 189L, 12L, 2L,
18L, 2L, 3L, 8L, 6L, 2L, 6L, 4L, 4L, 9L, 8L, 3L, 7L, 9L)), .Names = c("V1",
"V2", "V3", "V4", "N"), class = c("data.table", "data.frame"), row.names = c(NA,
-68L), .internal.selfref = <pointer: 0x318e998>)
唯一缺少的命令是在绘图之前重新定义 temp
:
temp <- temp[order(temp$V1), ]
因为在这种情况下,temp
是真正有序的,而不仅仅是虚拟的。
我的数据是 table()
四个变量的输出(数据如下)。
我想为每个 V3 绘制 N,由 V1 填充并由 V2 分面。但是之前,V1应该被V4排序。
temp$V1 <- reorder(temp$V1, temp$V4)
ggplot(temp, aes(x=V3, y=N, fill=V1)) +
geom_bar(binwidth = 10, stat="identity") +
ylab("") + xlab("") +
facet_grid(. ~ V2) +
scale_fill_discrete(name = "Journal") +
theme(legend.title = element_text(colour="black", size=14),
legend.direction="horizontal",
legend.position="bottom",
strip.text.x = element_text(size = 14),
axis.text = element_text(colour="black", size=10),
axis.ticks.x = element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank()
)
但是条形元素仍然没有按照我想要的输出规则排序
顺序应该是JBF、JFI、JMCB、JF、JFE、RFS。我究竟做错了什么?尺寸是否太多?
这是数据:
V1 V2 V3 V4 N
JBF 1998 with 0 97
JFI 1998 with 0 21
JMCB 1998 with 0 62
JBF 1999 with 0 93
JFI 1999 with 0 20
JMCB 1999 with 0 37
JBF 2000 with 0 112
JFI 2000 with 0 29
JMCB 2000 with 0 65
JBF 2009 with 0 247
JFI 2009 with 0 40
JMCB 2009 with 0 126
JBF 2010 with 0 299
JFI 2010 with 0 27
JMCB 2010 with 0 110
JBF 2011 with 0 311
JFI 2011 with 0 34
JMCB 2011 with 0 120
JBF 1998 without 0 29
JMCB 1998 without 0 9
JBF 1999 without 0 62
JMCB 1999 without 0 16
JBF 2000 without 0 37
JFI 2000 without 0 2
JMCB 2000 without 0 20
JBF 2009 without 0 158
JFI 2009 without 0 2
JMCB 2009 without 0 23
JBF 2010 without 0 153
JFI 2010 without 0 2
JMCB 2010 without 0 28
JBF 2011 without 0 133
JFI 2011 without 0 1
JMCB 2011 without 0 12
JF 1998 with 1 133
JFE 1998 with 1 91
RFS 1998 with 1 44
JF 1999 with 1 134
JFE 1999 with 1 101
RFS 1999 with 1 72
JF 2000 with 1 147
JFE 2000 with 1 101
RFS 2000 with 1 74
JF 2009 with 1 154
JFE 2009 with 1 181
RFS 2009 with 1 284
JF 2010 with 1 128
JFE 2010 with 1 179
RFS 2010 with 1 218
JF 2011 with 1 103
JFE 2011 with 1 249
RFS 2011 with 1 189
JF 1998 without 1 12
JFE 1998 without 1 2
JF 1999 without 1 18
JFE 1999 without 1 2
RFS 1999 without 1 3
JF 2000 without 1 8
JFE 2000 without 1 6
RFS 2000 without 1 2
JF 2009 without 1 6
JFE 2009 without 1 4
RFS 2009 without 1 4
JFE 2010 without 1 9
RFS 2010 without 1 8
JF 2011 without 1 3
JFE 2011 without 1 7
RFS 2011 without 1 9
编辑:dput(temp)
structure(list(V1 = structure(c(3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L,
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 1L, 3L, 1L, 3L, 2L,
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 6L, 4L, 5L, 6L, 4L, 5L,
6L, 4L, 5L, 6L, 4L, 5L, 6L, 4L, 5L, 6L, 4L, 5L, 6L, 4L, 6L, 4L,
5L, 6L, 4L, 5L, 6L, 4L, 5L, 4L, 5L, 6L, 4L, 5L), .Label = c("JMCB",
"JFI", "JBF", "JFE", "RFS", "JF"), class = c("ordered", "factor"
)), V2 = c("1998", "1998", "1998", "1999", "1999", "1999", "2000",
"2000", "2000", "2009", "2009", "2009", "2010", "2010", "2010",
"2011", "2011", "2011", "1998", "1998", "1999", "1999", "2000",
"2000", "2000", "2009", "2009", "2009", "2010", "2010", "2010",
"2011", "2011", "2011", "1998", "1998", "1998", "1999", "1999",
"1999", "2000", "2000", "2000", "2009", "2009", "2009", "2010",
"2010", "2010", "2011", "2011", "2011", "1998", "1998", "1999",
"1999", "1999", "2000", "2000", "2000", "2009", "2009", "2009",
"2010", "2010", "2011", "2011", "2011"), V3 = c("with", "with",
"with", "with", "with", "with", "with", "with", "with", "with",
"with", "with", "with", "with", "with", "with", "with", "with",
"without", "without", "without", "without", "without", "without",
"without", "without", "without", "without", "without", "without",
"without", "without", "without", "without", "with", "with", "with",
"with", "with", "with", "with", "with", "with", "with", "with",
"with", "with", "with", "with", "with", "with", "with", "without",
"without", "without", "without", "without", "without", "without",
"without", "without", "without", "without", "without", "without",
"without", "without", "without"), V4 = c("0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"),
N = c(97L, 21L, 62L, 93L, 20L, 37L, 112L, 29L, 65L, 247L,
40L, 126L, 299L, 27L, 110L, 311L, 34L, 120L, 29L, 9L, 62L,
16L, 37L, 2L, 20L, 158L, 2L, 23L, 153L, 2L, 28L, 133L, 1L,
12L, 133L, 91L, 44L, 134L, 101L, 72L, 147L, 101L, 74L, 154L,
181L, 284L, 128L, 179L, 218L, 103L, 249L, 189L, 12L, 2L,
18L, 2L, 3L, 8L, 6L, 2L, 6L, 4L, 4L, 9L, 8L, 3L, 7L, 9L)), .Names = c("V1",
"V2", "V3", "V4", "N"), class = c("data.table", "data.frame"), row.names = c(NA,
-68L), .internal.selfref = <pointer: 0x318e998>)
唯一缺少的命令是在绘图之前重新定义 temp
:
temp <- temp[order(temp$V1), ]
因为在这种情况下,temp
是真正有序的,而不仅仅是虚拟的。