Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 突出显示刻面图中没有数据的位置_R_Ggplot2_Bar Chart_Facet_Geom Text - Fatal编程技术网

R 突出显示刻面图中没有数据的位置

R 突出显示刻面图中没有数据的位置,r,ggplot2,bar-chart,facet,geom-text,R,Ggplot2,Bar Chart,Facet,Geom Text,在ggplot中镶嵌条形图时,x轴包括所有因子级别。然而,并非每个组中都存在所有水平。此外,可能存在零值,因此仅从条形图无法区分无数据的x轴值和y轴值为零的x轴值。考虑下面的例子: library(tidyverse) set.seed(43) site <- c("A","B","C","D","E") %>% sample(20, replace=T) %>% sort() year <- c("2010","2011","2012","2013","2014

在ggplot中镶嵌条形图时,x轴包括所有因子级别。然而,并非每个组中都存在所有水平。此外,可能存在零值,因此仅从条形图无法区分无数据的x轴值和y轴值为零的x轴值。考虑下面的例子:

 library(tidyverse)
 set.seed(43)
 site <- c("A","B","C","D","E") %>% sample(20, replace=T) %>% sort()
 year <- c("2010","2011","2012","2013","2014","2010","2011","2012","2013","2014","2010","2012","2013","2014","2010","2011","2012","2014","2012","2014")
 isZero = rbinom(n = 20, size = 1, prob = 0.40)
 value <- ifelse(isZero==1, 0, rnorm(20,10,3)) %>% round(0)
 df <- data.frame(site,year,value)

ggplot(df, aes(x=year, y=value)) +
  geom_bar(stat="identity") +
  facet_wrap(~site)
库(tidyverse)
种子(43)
站点%sample(20,replace=T)%>%sort()

年份因此,以下是您提出的方法的一个示例:

# Tabulate sites vs year, take zero entries
tab <- table(df$site, df$year)
idx <- which(tab == 0, arr.ind = T)

# Build new data.frame
missing <- data.frame(site = rownames(tab)[idx[, "row"]],
                      year = colnames(tab)[idx[, "col"]],
                      value = 1,
                      label = "N.D.") # For 'no data'

ggplot(df, aes(year, value)) +
  geom_col() +
  geom_text(data = missing, aes(label = label)) +
  facet_wrap(~site)

ggplot(df, aes(x=year, y=value)) +
  geom_bar(stat="identity") +
  facet_wrap(~site, scales = "free_x")