如何可视化群组和子群组的频率?

3

我需要使用一个组变量As和子组变量ADs绘制频率数据的图表。最好的可视化频率的方式是饼图还是马赛克图?在ggplot2中是否有相关函数?

df <- data.frame(As=c('GeA','GeA','GeA', 'GA'), 
             ADs=c('A44','A33','A37','A141'),
             freq=c(501,65,50,103))

#    As  ADs freq
# 1 GeA  A44  501
# 2 GeA  A33   65
# 3 GeA  A37   50
# 4  GA A141  103

以下是一些想法:

image1

然而,有没有一种方法可以在一个图表中区分组和子组?

在提出的解决方案中,下面的两个图表看起来很有前途。

饼图&瓷砖图

piecharttileGraph

我已经使用了M--建议的以下代码。

df.2 <- df
df.2$ymax <- with(df.2, ave(freq, As, FUN=cumsum))
df.2$ymin <- lag(df.2$ymax, default = 0)
df.2$ymin <- ifelse(lag(as.character(df.2$As), default = 0) != df.2$As, 0, df.2$ymin)

df.legend <- df.2[with(df.2, order(As)), ]

library(ggplot2)
# Pie Chart
ggplot(df.2) + 
  geom_rect(aes(fill=As, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) +
  geom_rect(aes(fill=ADs, ymax=ymax, ymin=ymin, xmax=3, xmin=0)) +
  xlim(c(0, 4)) + 
  theme(aspect.ratio=1) +
  coord_polar(theta="y") +
  scale_x_continuous(breaks=c(0,3), labels=c("ADs", "As")) + 
  annotate("text", x=rep(1.5,4), y=c(50, 350,530,590), 
           label= as.character(df.legend$ADs)) + 
  annotate("text", x=rep(3.5,2), y=c(50, 350), 
           label= as.character(unique(df.legend$As))) + 
  theme(legend.position="none", axis.title.x=element_blank(),
        axis.title.y=element_blank())

# Tile Graph
ggplot(df.2) + 
  geom_rect(aes(fill=As, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) +
  geom_rect(aes(fill=ADs, ymax=ymax, ymin=ymin, xmax=3, xmin=0)) +
  xlim(c(0, 4)) + theme(aspect.ratio=1) +
  scale_x_continuous(breaks=c(1.5,3.5), labels=c("ADs", "As")) + 
  annotate("text", x=rep(1.5,4), y=c(50, 350,530,590), 
           label= paste(as.character(df.legend$ADs), df.legend$freq,sep= " = ")) + 
  annotate("text", x=rep(3.5,2), y=c(50, 350), 
           label= as.character(unique(df.legend$As))) + 
  theme(legend.position="none", axis.title.x=element_blank(),
        axis.title.y=element_blank())

然而,我没有得到相同的输出结果。
饼图和瓦片图。
消息:'x'的比例已经存在。添加另一个比例尺'x',将替换现有比例尺。
请问可能出现什么问题?所使用的软件包版本有什么区别吗?

堆积条形图 - M--
我对如何使用堆叠条形图同时可视化组和子组没有很清晰的概念。你能画个草图然后分享给我吗? - Prradep
1个回答

3

堆叠条形图:

您可以使用堆叠条形图:

library(ggplot2)
ggplot(data = df, aes(x = As, y = freq, fill = ADs)) + 
       geom_bar(stat = "identity")

您可以添加以下内容以在绘图中获得标签:

p +   geom_text(aes(label = paste(ADs, freq, sep=": ")), 
        position = position_stack(vjust = 0.5), size = 3) + #subgroups
       stat_summary(fun.y = sum, aes(label = ..y.., group = As), geom = "text") + #groups
        theme(legend.position="none")

enter image description here

下面两个答案是针对这篇帖子的。

瓷砖图:

为此,我们需要调整数据:

  df.2 <- df
  df.2$ymax <- with(df.2, ave(freq, As, FUN=cumsum))
  df.2 <- df.2[with(df.2, order(As)), ]
  
  #for some reason lag function does not work properly in R 3.3.3
  library(data.table)
  setDT(df.2)[, ymin:=c(0,ymax[-.N])]  

  
  df.legend <- df.2[with(df.2, order(As)), ]

然后我们可以再次使用ggplot
 ggplot(df.2) + 
   geom_rect(aes(fill=As, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) +
   geom_rect(aes(fill=ADs, ymax=ymax, ymin=ymin, xmax=3, xmin=0)) +
   xlim(c(0, 4)) + theme(aspect.ratio=1) +
   scale_x_continuous(breaks=c(1.5,3.5), labels=c("ADs", "As")) + 
   annotate("text", x=rep(1.5,4), y=c(50, 350,530,590), 
          label= paste(as.character(df.legend$ADs), df.legend$freq,sep= " = ")) + 
   annotate("text", x=rep(3.5,2), y=c(50, 350), 
        label= as.character(unique(df.legend$As))) + 
      theme(legend.position="none", axis.title.x=element_blank(),
     axis.title.y=element_blank())

enter image description here

Pie Chart:

ggplot(df.2) + 
 geom_rect(aes(fill=As, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) +
 geom_rect(aes(fill=ADs, ymax=ymax, ymin=ymin, xmax=3, xmin=0)) +
 xlim(c(0, 4)) + 
 theme(aspect.ratio=1) +
 coord_polar(theta="y") +
 scale_x_continuous(breaks=c(0,3), labels=c("ADs", "As")) + 
 annotate("text", x=rep(1.5,4), y=c(50, 350,530,590), 
        label= as.character(df.legend$ADs)) + 
 annotate("text", x=rep(3.5,2), y=c(50, 350), 
        label= as.character(unique(df.legend$As))) + 
 theme(legend.position="none", axis.title.x=element_blank(),
     axis.title.y=element_blank())

enter image description here


谢谢绘图。能否看到每个子组的占比将会很好。 - Prradep
请问您使用的ggplot2软件包版本是多少?由于某种原因,我在最后两个图中没有得到颜色。 - Prradep
@Prradep 编辑你的问题。添加你得到的结果,这样它就可以重新打开。我会尽快添加我的会话信息和软件包版本。 - M--
我已经找到了输出不同的原因,原因是 ymin 值不同。在更改值 (df.legend$ymin <- c(0,103,501,566)) 后,我得到了输出结果。请编辑答案以便我可以接受它。 - Prradep
否则,在 df.legend 上使用 lag 函数将产生所需的输出! - Prradep
@Prradep 我使用了 data.table 包。它在我的另一台机器上可以工作,但在我的笔记本电脑上却不能。不确定 lag 函数到底发生了什么事情。但现在这个问题得到了解决。 - M--

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接