R 删除geom_平铺上的空行

R 删除geom_平铺上的空行,r,ggplot2,R,Ggplot2,我有一个包含5列的数据框: N1 <- an integer between c(125,100,80,70,60,50,40,30,20) N2 <- an integer between c(1,5,10,15,20,25,30,35,40,50,60,80,100) Type <- Two different types Rang <- a number crit <- a character, only one value 我只想在一个图形中包含所

我有一个包含5列的数据框:

N1 <- an integer between c(125,100,80,70,60,50,40,30,20)

N2 <- an integer between c(1,5,10,15,20,25,30,35,40,50,60,80,100)

Type <- Two different types

Rang <- a number

crit <- a character, only one value
我只想在一个图形中包含所有这些信息,因此我希望使用ggplot和
geom_tile
如下所示:

p <- ggplot(Rang_final)
p <- (p
      + geom_tile(data=Rang_final[Rang_final$Type=="SST-T_2m",], aes(x=N1, y=N2, fill=rang))
      + geom_tile(data=Rang_final[Rang_final$Type=="T_2m-SST",], aes(x=N2, y=N1, fill=rang))
      + scale_fill_gradient2(name="Rang", low="deepskyblue",mid="yellow",high="red", midpoint=100, na.value = "grey50")
      + theme(axis.title.x = element_text(size=14, face="bold"),
          axis.title.y = element_text(size=14, face="bold"),
          strip.text.x = element_text(size=14, face="bold"),
          strip.text.y = element_text(size=14, face="bold"),
          axis.text=element_text(size=14),
          axis.title.y=element_text(size=14, face="bold"),
          legend.key=element_rect(size=0.5, colour="black"),
          legend.text=element_text(size=10),
          legend.margin=unit(0,"lines"),
          legend.key.size=unit(0.8,"cm"),
          legend.text.align=0)
  + theme_bw()

)

print(p)

p这是我的建议。当您将N1和N2转换为因子时,x轴和y轴编号不符合顺序的原因是您没有指定所需的顺序。这里我使用了
overflow
包,因此您的数据帧称为mydf,它与您发布的内容相同。我认为你需要做两件事

1) 重新排序因子

你需要的是以正确的方式重新排列你的因素。当您将角色转换为脚本中的因子时,发生了类似的情况

#> mydf$N1 <- as.factor(mydf$N1)
#> levels(mydf$N1)
#[1] "20"  "30"  "40"  "50"  "60"  "70"  "80"  "100" "125"
#> unclass(mydf$N1)
#[1] 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 9 9
#attr(,"levels")
#[1] "20"  "30"  "40"  "50"  "60"  "70"  "80"  "100" "125"
在这里您可以看到“20”在unclass中有1个,而“125”有9个。这就是你想要的订单

#> levels(mydf$N1)
#[1] "20"  "30"  "40"  "50"  "60"  "70"  "80"  "100" "125"
#> unclass(mydf$N1)
#[1] 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 9 9
#attr(,"levels")
#[1] "20"  "30"  "40"  "50"  "60"  "70"  "80"  "100" "125"
2) 数据子集

这可能/可能没有必要。但是,到目前为止,我认为这是必要的。以下情况似乎不太好

  + geom_tile(data=Rang_final[Rang_final$Type=="SST-T_2m",], aes(x=N1, y=N2, fill=rang))
  + geom_tile(data=Rang_final[Rang_final$Type=="T_2m-SST",], aes(x=N2, y=N1, fill=rang))
所以,我做了一个技巧来整理你的数据

 # Subset data using dplyr
 america <- filter(mydf, Type == "SST-T_2m")
 brazil <- filter(mydf, Type == "T_2m-SST")

 # Reverse N1 and N2 in brazil (So N1 is actually N2, and N2 is actually N1)
 colnames(brazil) <- c("N2","N1", "Type", "rang", "crit")
 brazil[,c(2,1,3:5)]
使用dplyr的子集数据 美国
library(dplyr)
# Sort mydf by N1
mydf <- arrange(mydf, N1)

# Convert N1 to factor
mydf$N1 <- as.factor(mydf$N1)
#> levels(mydf$N1)
#[1] "20"  "30"  "40"  "50"  "60"  "70"  "80"  "100" "125"
#> unclass(mydf$N1)
#[1] 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 9 9
#attr(,"levels")
#[1] "20"  "30"  "40"  "50"  "60"  "70"  "80"  "100" "125"
  + geom_tile(data=Rang_final[Rang_final$Type=="SST-T_2m",], aes(x=N1, y=N2, fill=rang))
  + geom_tile(data=Rang_final[Rang_final$Type=="T_2m-SST",], aes(x=N2, y=N1, fill=rang))
 # Subset data using dplyr
 america <- filter(mydf, Type == "SST-T_2m")
 brazil <- filter(mydf, Type == "T_2m-SST")

 # Reverse N1 and N2 in brazil (So N1 is actually N2, and N2 is actually N1)
 colnames(brazil) <- c("N2","N1", "Type", "rang", "crit")
 brazil[,c(2,1,3:5)]
 canada <- rbind(america, brazil)
 canada <- arrange(canada,N1)
 canada$N1 <- as.factor(canada$N1)

 canada <- arrange(canada,N2)
 canada$N2 <- as.factor(canada$N2)
x <- ggplot(canada)
x <- (x
  + geom_tile(aes(x=N1, y=N2, fill =rang))
  + scale_fill_gradient2(name="Rang", low="deepskyblue",mid="yellow",high="red",     midpoint=100, na.value = "grey50")
  + theme(axis.title.x = element_text(size=14, face="bold"),
      axis.title.y = element_text(size=14, face="bold"),
      strip.text.x = element_text(size=14, face="bold"),
      strip.text.y = element_text(size=14, face="bold"),
      axis.text=element_text(size=14),
      axis.title.y=element_text(size=14, face="bold"))
  + theme_bw()

 )