为什么ggplot2饼图构面会混淆构面标签 [英] Why ggplot2 pie-chart facet confuses the facet labelling

查看:97
本文介绍了为什么ggplot2饼图构面会混淆构面标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种看起来像这样的数据: 类型1( http://dpaste.com/1697615/plain/)

I have two types of data that looks like this: Type 1 (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/) . 这些值与上述类型1中的Cluster-6相同:

And type two, (http://dpaste.com/1697602/plain/). These values are identical with Cluster-6 in type 1 above:

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数据:

But why when dealing with type 1 data with this code:

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))

生成以下数字:

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

Notice that the Facet label is misplaced, Cluster-3 should be Cluster-6, where Neutrophils takes larger proportions.

我该如何解决问题?

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

When dealing with type 2 data have no problem at all.

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(...)中使用了数据框名称.这样可以解决问题.

It's because you use the data frame name in aes(...). This fixes the problem.

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是第一个.

In defining the facets, you reference V1 in the context of the default dataset, and ggplot sorts alphabetically by level (so "Cluster-3" comes first). In your call to aes(...) you reference dat$V3 directly, so ggplot goes out of the context of the default dataset to the original dataframe. There, Cluster-6 is first.

作为一般性评论,不应从不引用aes(...)定义的数据集上下文之外的aes(...)中的数据.所以:

As a general comment, one should never reference data in aes(...) outside the context of the dataset defined with data=.... So:

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

您的问题是第二种选择为什么不好的一个很好的例子.

Your problem is a perfect example of why the second option is bad.

这篇关于为什么ggplot2饼图构面会混淆构面标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆