为什么 ggplot2 饼图分面会混淆分面标签

2024-01-06

我有两种类型的数据,如下所示: 类型 1(http://dpaste.com/1697615/plain/ http://dpaste.com/1697615/plain/)

Cluster-6   abTcells    1456.74119
Cluster-6   Macrophages 5656.38478
Cluster-6   Monocytes   4415.69078
Cluster-6   StemCells   1752.11026
Cluster-6   Bcells  1869.37056
Cluster-6   gdTCells    1511.35291
Cluster-6   NKCells 1412.61504
Cluster-6   DendriticCells  3326.87741
Cluster-6   StromalCells    2008.20603
Cluster-6   Neutrophils 12867.50224
Cluster-3   abTcells    471.67118
Cluster-3   Macrophages 1000.98164
Cluster-3   Monocytes   712.92273
Cluster-3   StemCells   557.88648
Cluster-3   Bcells  599.94109
Cluster-3   gdTCells    492.61994
Cluster-3   NKCells 524.42522
Cluster-3   DendriticCells  647.28811
Cluster-3   StromalCells    876.27875
Cluster-3   Neutrophils 1025.24105

然后输入二,(http://dpaste.com/1697602/plain/ http://dpaste.com/1697602/plain/)。 这些值与上面类型 1 中的 Cluster-6 相同:

abTcells    1456.74119
Macrophages 5656.38478
Monocytes   4415.69078
StemCells   1752.11026
Bcells  1869.37056
gdTCells    1511.35291
NKCells 1412.61504
DendriticCells  3326.87741
StromalCells    2008.20603
Neutrophils 12867.50224

但为什么在使用此代码处理类型 1 数据时:

library(ggplot2);
library(RColorBrewer);   
filcol <- brewer.pal(10, "Set3")
dat <- read.table("http://dpaste.com/1697615/plain/")
ggplot(dat,aes(x=factor(1),y=dat$V3,fill=dat$V2))+
   facet_wrap(~V1)+
   xlab("") +
   ylab("") +
   geom_bar(width=1,stat="identity",position = "fill") +
   scale_fill_manual(values = filcol,guide = guide_legend(title = "")) +
   coord_polar(theta="y")+
   theme(strip.text.x = element_text(size = 8, colour = "black", angle = 0))

准备好的数据:

> dput(dat)
structure(list(V1 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Cluster-3", 
"Cluster-6"), class = "factor"), V2 = structure(c(1L, 5L, 6L, 
9L, 2L, 4L, 8L, 3L, 10L, 7L, 1L, 5L, 6L, 9L, 2L, 4L, 8L, 3L, 
10L, 7L), .Label = c("abTcells", "Bcells", "DendriticCells", 
"gdTCells", "Macrophages", "Monocytes", "Neutrophils", "NKCells", 
"StemCells", "StromalCells"), class = "factor"), V3 = c(1456.74119, 
5656.38478, 4415.69078, 1752.11026, 1869.37056, 1511.35291, 1412.61504, 
3326.87741, 2008.20603, 12867.50224, 471.67118, 1000.98164, 712.92273, 
557.88648, 599.94109, 492.61994, 524.42522, 647.28811, 876.27875, 
1025.24105)), .Names = c("V1", "V2", "V3"), class = "data.frame", row.names = c(NA, 
-20L))

Generated this following figures: enter image description here

请注意,Facet 标签放错了位置,Cluster-3 应该是 Cluster-6, 其中中性粒细胞所占比例较大。

我该如何解决这个问题?

处理2型数据时完全没有问题。

library(ggplot2)                  
df <- read.table("http://dpaste.com/1697602/plain/");
library(RColorBrewer); 
filcol <- brewer.pal(10, "Set3")

ggplot(df,aes(x=factor(1),y=V2,fill=V1))+
   geom_bar(width=1,stat="identity")+coord_polar(theta="y")+
    theme(axis.title = element_blank())+
     scale_fill_manual(values = filcol,guide = guide_legend(title = "")) +
      theme(strip.text.x = element_text(size = 8, colour = "black", angle = 0))

准备好的数据:

> dput(df)
structure(list(V1 = structure(c(1L, 5L, 6L, 9L, 2L, 4L, 8L, 3L, 
10L, 7L), .Label = c("abTcells", "Bcells", "DendriticCells", 
"gdTCells", "Macrophages", "Monocytes", "Neutrophils", "NKCells", 
"StemCells", "StromalCells"), class = "factor"), V2 = c(1456.74119, 
5656.38478, 4415.69078, 1752.11026, 1869.37056, 1511.35291, 1412.61504, 
3326.87741, 2008.20603, 12867.50224)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, 
-10L))

这是因为您在中使用了数据框名称aes(...)。这解决了问题。

ggplot(dat,aes(x=factor(1),y=V3,fill=V2))+
  facet_wrap(~V1)+
  xlab("") +
  ylab("") +
  geom_bar(width=1,stat="identity",position = "fill") +
  scale_fill_manual(values = filcol,guide = guide_legend(title = "")) +
  coord_polar(theta="y")+
  theme(strip.text.x = element_text(size = 8, colour = "black", angle = 0))

在定义方面时,您引用V1在默认数据集的上下文中,以及ggplot按级别按字母顺序排序(因此“Cluster-3”排在第一位)。在您致电时aes(...)你参考dat$V3直接,所以 ggplot 会脱离默认数据集的上下文到原始数据帧。其中,Cluster-6 是第一个。

作为一般性评论,人们应该never参考数据在aes(...)在定义的数据集上下文之外data=.... So:

ggplot(data=dat, aes(y=V3...))       # good
ggplot(data=dat, aes(y=dat$V3...))   # bad

你的问题是为什么第二个选项不好的一个完美例子。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 ggplot2 饼图分面会混淆分面标签 的相关文章

随机推荐