2

我可以使用 ggplot2 绘制气泡图。现在我想聚集一列然后做气泡图。我怎样才能做到这一点?这是情节的样子:图像看起来像这样

阴谋

这是我的虚拟数据

x=data.frame(
group=c("A","A","B","B","C","C","C","D","D"),
item=c("apple","pear","apple","peach","orange","cherry","apple","banna","apple"),
count=c(3,5,5,1,9,7,3,2,4))

这是我的脚本:

library(ggplot2)

xf=x$item
xfu=unique(xf)
x$item=factor(xf,levels=xfu)
p<-ggplot(x)
p + geom_point(aes(x = group, y = item,size= count),las=2,shape=16,
alpha=0.80) +
scale_size_continuous(breaks=c(1,2,3,4,5,6,7,8,9),
labels=c("1","2","3","4","5","6","7","8","9"))+
theme(axis.text.x = element_text(angle = 45, hjust = 
1.1,size=11,colour="black",face="bold"),
axis.text.y = element_text(size=11,colour="black"),
axis.title.y = element_text(size=12,colour =
"black",face="bold",vjust=0.12))+
labs(x="",y = "Item")

现在我想对项目数据进行层次聚类,以获得更明显的模式。项目的顺序并不重要。我怎样才能做到这一点?

4

1 回答 1

1

如果您询问如何放大气泡以重叠,则需要在参数size= 之外aes()显式设置(如下所示)。我已经通过geom_text()电话添加了标签。

但是,如果您希望这些列根据某种顺序聚集在一起,则需要一个连续变量来进行绘图(需要更多信息来演示)

xf=x$item
xfu=unique(xf)
x$item=factor(xf,levels=xfu)
p<-ggplot(x)
p + geom_point(aes(x = group, y = item), size=15+x$count*7, las=2,shape=21, fill="white", alpha=0.80) +
geom_text(aes(x = group, y = item, label=count), fontface="bold", size=7) +
  theme(axis.text.x = element_text(angle = 45, hjust = 
                                     1.1,size=11,colour="black",face="bold"),
        axis.text.y = element_text(size=11,colour="black"),
        axis.title.y = element_text(size=12,colour =
                                      "black",face="bold",vjust=0.12),
        panel.grid.major = element_line(linetype = "dashed", size=1, color="black"))+
  labs(x="",y = "Item")

在此处输入图像描述

于 2013-12-02T05:08:19.680 回答