17

如何使用 GNU R 创建一个分类气泡图,类似于系统映射研究中使用的(见下文)?

映射研究中使用的分类气泡图

编辑:好的,这是我到目前为止所尝试的。首先,我的数据集(Var1 到 x 轴,Var2 到 y 轴):

> grid
                         Var1                      Var2 count
1              Does.Not.apply            Does.Not.apply    53
2               Not.specified            Does.Not.apply    15
3   Active.Learning..general.            Does.Not.apply     1
4      Problem.based.Learning            Does.Not.apply     2
5              Project.Method            Does.Not.apply     4
6         Case.based.Learning            Does.Not.apply    22
7               Peer.Learning            Does.Not.apply     6
10                      Other            Does.Not.apply     1
11             Does.Not.apply             Not.specified    15
12              Not.specified             Not.specified    15
21             Does.Not.apply Active.Learning..general.     1
23  Active.Learning..general. Active.Learning..general.     1
31             Does.Not.apply    Problem.based.Learning     2
34     Problem.based.Learning    Problem.based.Learning     2
41             Does.Not.apply            Project.Method     4
45             Project.Method            Project.Method     4
51             Does.Not.apply       Case.based.Learning    22
56        Case.based.Learning       Case.based.Learning    22
61             Does.Not.apply             Peer.Learning     6
67              Peer.Learning             Peer.Learning     6
91             Does.Not.apply                     Other     1
100                     Other                     Other     1

然后,尝试绘制数据:

# Based on http://flowingdata.com/2010/11/23/how-to-make-bubble-charts/
grid <- subset(grid, count > 0)
radius <- sqrt( grid$count / pi )
symbols(grid$Var1, grid$Var2, radius, inches=0.30, xlab="Research type", ylab="Research area")
text(grid$Var1, grid$Var2, grid$count, cex=0.5)

结果如下:我有什么

问题:轴标签错误,虚线网格线丢失。

4

3 回答 3

14

这是 ggplot2 解决方案。首先,将半径作为新变量添加到您的数据框中。

grid$radius <- sqrt( grid$count / pi )

您应该使用绘图内的点和文本标签的大小来完美匹配。

library(ggplot2)
ggplot(grid,aes(Var1,Var2))+
  geom_point(aes(size=radius*7.5),shape=21,fill="white")+
  geom_text(aes(label=count),size=4)+
  scale_size_identity()+
  theme(panel.grid.major=element_line(linetype=2,color="black"),
        axis.text.x=element_text(angle=90,hjust=1,vjust=0))

在此处输入图像描述

于 2013-04-05T19:30:00.610 回答
2

这里是使用levelplotfrom的版本latticeExtra

library(latticeExtra)
levelplot(count~Var1*Var2,data=dat,
          panel=function(x,y,z,...)
          {
            panel.abline(h=x,v=y,lty=2)
            cex <- scale(z)*3
            panel.levelplot.points(x,y,z,...,cex=5)
            panel.text(x,y,label=z,cex=0.8)
          },scales=(x=list(abbreviate=TRUE))) ## to get short labels

在此处输入图像描述

要获得与计数成比例的气泡大小,您可以这样做

library(latticeExtra)
levelplot(count~Var1*Var2,data=dat,
          panel=function(x,y,z,...)
          {
            panel.abline(h=x,v=y,lty=2)
            cex <- scale(z)*3
            panel.levelplot.points(x,y,z,...,cex=5)
            panel.text(x,y,label=z,cex=0.8)

          })

我不显示它,因为在修复大小的情况下渲染不清晰。

于 2013-04-05T19:35:03.997 回答
1

这将使您开始通过将刻度线添加到您的 xaxis。

要添加行,只需在每个级别添加一行

ggs <- subset(gg, count > 0)
radius <- sqrt( ggs$count / pi )

# ggs$Var1 <- as.character(ggs$Var1)

# set up your tick marks 
#  (this can all be put into a single line in `axis`, but it's placed separate here to be more readable)
#--------------
# at which values to place the x tick marks
x_at <- seq_along(levels(gg$Var1))
# the string to place at each tick mark
x_labels <-   levels(gg$Var1)


# use xaxt="n" to supress the standard axis ticks 
symbols(ggs$Var1, ggs$Var2, radius, inches=0.30, xlab="Research type", ylab="Research area", xaxt="n")
axis(side=1, at=x_at, labels=x_labels)

text(ggs$Var1, ggs$Var2, ggs$count, cex=0.5)

另外,请注意,grid我没有调用对象,而是调用了它gg,然后调用ggs了子集。 grid是 中的一个函数R。虽然“允许”用对象覆盖函数,但不建议这样做,并且可能会导致烦人的错误。

于 2013-04-05T19:29:28.983 回答