简体   繁体   中英

ggplot2 bubbleplot with clustering data

I can do a bubble plot using ggplot2. now I want to clustering one column then do the bubble plot. how can I do that? Here is what the plot look like: the image look like this one

情节

Here is my dummy data

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

Here are my scripts:

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

Now I want to do a hierarchical clustering on the item data so as to get a more apparent pattern about it. The order of item is not important. How can I do that?

If you are asking how you scale up the bubbles to overlap, you need to explicitly set size= outside the aes() parameter (as below). I've added the labels with a geom_text() call.

If you want the columns to cluster together based on some order, though, you need a continuous variable to plot against (need more info to demonstrate)

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM