R ggplot2包中的散点图:移动轴和更改字体标签大小

时间:2015-09-12 17:38:36

标签: r ggplot2

我尝试使用ggplot2

复制以下图表

我想从该图表中做出的一个改变是为每个点及其标签赋予颜色。这是我到目前为止所尝试的内容:

library(ggplot2)
library(directlabels)
Z <- c("Label1", "Label2", "Label3", "Label4", "Label5", "Label6", "Label7",        
"Label8", "Label9", "Label10", "Label11", "Label12", "Label13", "Label14",
"Label15", "Label16", "Label17", "Label18", "Label19", "Label20", "Label21",
"Label22", "Label23", "Label24")

X <- c(10.32582421, 9.772686421, -13.99202201, 3.803952545, 7.775395482,
-11.82234956, -24.27906403, -6.864457678, -24.62853773, 15.3562638,
-6.476057462, 9.576414602, -5.504090215, 29.74512913, 9.046116821,
15.79954557, -39.61679645, -0.90307239, 21.12503086, 15.30221473, 
13.40781808, -6.803226537, -4.045907666, -0.134057007)

Y <- c(0.037608141, 0.010581738, 0.117730985, 0.022347258, 0.069347278, 
0.026699666, 0.028739498, 0.040611306, 0.036626248, 0.034854158,
0.039310836, 0.03122964, 0.009422296, 0.021935924, 0.050006846, 
0.036285691, 0.016796701, 0.057764277, 0.028421772, 0.042726693,
0.037513217, 0.058422072, 0.066859355, 0.078158403)

mychart <- data.frame(Z, X, Y)

q <- ggplot(mychart, aes(X, Y)) + geom_point(aes(colour = Z)) + theme_bw()
direct.label(q)

我得到以下结果:

我有三件事情我很难解决:

  1. 我想删除灰色象限线。
  2. 我想移动轴,使它们在图表中居中,图形分布在4个象限中。
  3. 我想减少标签字体大小 - 我怀疑这就是为什么有些人不会接近他们的分数。

1 个答案:

答案 0 :(得分:0)

  1. theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))

  2. 使用+xlim(min,max)+ylim(min,max)设置绘图的轴限制。然后,您可以使用+geom_hline(y=yvalue)+geom_vline(x=xvalue)将水平线和垂直线添加到地块中,以指定四个象限。

  3. 不使用+direct.label(q),而是使用+geom_text(aes(label=q,size=sizevalue),其中'sizevalue'是一个确定标签大小的数值(因此您可以试验一下)。

  4. 编辑:试试这个代码,它应该修复你的点标签。 (我不知道如何将轴标签移动到您绘制的线条,也不知道将原始轴移动到绘图中心的本地方式。抱歉!):

    ggplot(mychart, aes(X, Y)) + 
        geom_point(aes(colour = Z)) + 
        theme(panel.grid.major = element_blank(), 
              panel.grid.minor = element_blank(), 
              panel.background = element_blank(), 
              axis.line = element_line(colour = "black")) + 
        xlim(-40,40) + 
        ylim(0,0.12) + 
        geom_hline(y=0.04) + 
        geom_vline(y=0) + 
        geom_text(aes(x=X,y=Y+0.003,label=Z,color=Z)) + 
        theme(legend.position="none")
    

    编辑2:geom_text中的抖动

    ggplot(mychart, aes(X, Y, colour=Z)) + 
        geom_point() + 
        theme(panel.grid.major = element_blank(), 
              panel.grid.minor = element_blank(), 
              panel.background = element_blank(), 
              axis.line = element_line(colour = "black")) + 
        geom_text(aes(label=Z),
                  position = position_jitter(width=2, height=0.005)) +
        xlim(-40,40) + 
        ylim(0,0.12) + 
        geom_hline(y=0.04) + 
        geom_vline(y=0) + 
        theme(legend.position="none")