我遇到了这个奇怪的错误..
一些测试数据:
library(ggplot2)
library(dplyr)
test <- structure(list(group = structure(c(1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L), .Label = c("G", "N", "P"), class = "factor"),
var = structure(c(1L, 1L, 2L, 3L, 4L, 4L, 4L, 4L, 2L, 2L,
2L, 3L, 2L, 2L, 2L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 5L, 1L, 1L,
2L, 1L, 1L, 1L, 3L, 3L, 3L, 5L, 3L, 3L, 3L, 4L, 4L, 4L, 1L,
1L, 2L, 2L, 2L, 3L), .Label = c("a", "b", "c", "d", "e"), class = "factor"),
group2 = structure(c(3L, 1L, 3L, 3L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 2L,
3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L,
4L, 1L, 1L, 2L, 3L, 4L), .Label = c("O", "P", "Q", "R"), class = "factor"),
cor = c(0.270075198428616, 0.262097140096646, -0.331312784846655,
-0.343984945812309, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA)), row.names = c(NA, -45L), .Names = c("group", "var",
"group2", "cor"), class = "data.frame")
我想制作这个情节:
test %>%
ggplot(., aes(x=group2, y=cor)) +
geom_bar(stat="identity", position="dodge", aes(fill=var, group=var)) +
geom_text(aes(label = round(cor,2),
vjust = ifelse(cor >= 0, 0, 1),
group=var),
position = position_dodge(width=1)) +
theme_bw(base_size=18) +
facet_wrap(~group, scales="free_x")
..导致:单位错误(x,default.units):’x’和’units’必须有长度> 0
而且只使用1:40行:
test %>% slice(1:40) %>%
ggplot(., aes(x=group2, y=cor)) +
geom_bar(stat="identity", position="dodge", aes(fill=var, group=var)) +
geom_text(aes(label = round(cor,2),
vjust = ifelse(cor >= 0, 0, 1),
group=var),
position = position_dodge(width=1)) +
theme_bw(base_size=18) +
facet_wrap(~group, scales="free_x")
因此,第40行只有两个级别,而第41行则是第三级.但是怎么会导致这个错误呢?或者还有其他我看不到的东西?
最佳答案 如果擦除facet_wrap或者如果不是所有P-group都是NA,则错误也会消失(例如,在0.2中更改cor的最后一个条目).显然,如果一个方面是空的(只包含NA),geom_text就无法处理.
如果你真的想要包含空面,这是一个解决方法:
1.用0替换空面中至少1个(或全部?)NA.条形长度为0,所以没问题
2.现在,图表中的标签为零.通过使用alpha = ifelse(cor == 0,0,1),您将使该标签完全透明.
码:
library(ggplot2)
library(dplyr)
test <- structure(list(group = structure(c(1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L), .Label = c("G", "N", "P"), class = "factor"),
var = structure(c(1L, 1L, 2L, 3L, 4L, 4L, 4L, 4L, 2L, 2L,
2L, 3L, 2L, 2L, 2L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 5L, 1L, 1L,
2L, 1L, 1L, 1L, 3L, 3L, 3L, 5L, 3L, 3L, 3L, 4L, 4L, 4L, 1L,
1L, 2L, 2L, 2L, 3L), .Label = c("a", "b", "c", "d", "e"), class = "factor"),
group2 = structure(c(3L, 1L, 3L, 3L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 2L,
3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L,
4L, 1L, 1L, 2L, 3L, 4L), .Label = c("O", "P", "Q", "R"), class = "factor"),
cor = c(0.270075198428616, 0.262097140096646, -0.331312784846655,
-0.343984945812309, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
0)), row.names = c(NA, -45L), .Names = c("group", "var",
"group2", "cor"), class = "data.frame")
test %>%
ggplot(., aes(x=group2, y=cor)) +
geom_bar(stat="identity", position="dodge", aes(fill=var, group=var)) +
geom_text(aes(label = round(cor,2),
vjust = ifelse(cor >= 0, 0, 1),
group=var, alpha=ifelse(cor == 0, 0, 1)),
position = position_dodge(width=1)) +
theme_bw(base_size=18) +
facet_wrap(~group, scales="free_x")
如果要删除geom_text的图例,只需添加show_guide = FALSE即可.
编辑:如果透明度不正常,@ beetroot提出了这个不错的选择:用size = ifelse替换alpha = ifelse(cor == 0,0,1)(cor == 0,NA,3))