2015-05-02 41 views
3

我已经得到了以下情节: enter image description here散点图有两个传说与相应颜色

,我在红,蓝各色过的话那么我有两个组。除此之外,我有两个传说,每个传说对应一个组。

我的代码如下:

install.packages("wordcloud") 
library(wordcloud) 

textplot(cor_met_u1, cor_met_u2, 1:length(cor_met_u1) ,ylim=c(-1,1), xlim=c(-1,1), col ="red", show.lines=F) 
par(new=T) 
textplot(cor_met_v1, cor_met_v2, 1:length(cor_met_v1),ylim=c(-1,1), xlim=c(-1,1),show.lines=F,col="blue") 

legend("topright", inset=c(-0.1,0), legend=objwoorden, title="Object names",cex=0.7,col="red") 
legend("topright", inset=c(0.1,0), legend=trefwoorden, title="Keywords",cex=0.7,col="blue") 

现在我想以适应下面的东西,但我无法找到如何做到这一点:

  • 的标题传说对象名称:我希望这个图例中的每个单词都是红色的,我希望这个图例中的每个单词在剧情中都有相应的数字作为关键字。

  • 同上:带标题的图例关键词:我希望这个图例中的每个单词都是蓝色的,我希望这个图例中的每个单词都有相应的图表中的数字作为关键字。

  • 我的传说没有足够的空间,一部分的传说没有被绘制。我怎样才能减少积分点的空间,并增加传说的空间?

我的数据(红色部分):

cor_met_u1 <- c(-8.553663e-01, -7.726949e-01, -7.308201e-01, -6.992058e-01, -6.675692e-01, -5.971927e-01, -5.870302e-01, -4.856212e-01, -4.612918e-01, -4.185641e-01, -4.106425e-01, 3.816280e-01, 3.184851e-01, 8.766928e-03, 9.121623e-03, 9.227969e-03, -3.477085e-02, 1.248777e-02, 2.982004e-03, 3.970818e-03, 3.970818e-03, 3.970818e-03, 4.099181e-03, -2.823043e-03, 2.702839e-02,-1.683602e-03, -2.231668e-02, 4.884192e-02, -1.177896e-02, -2.984341e-02, -1.120810e-02, 1.449123e-02, -2.223017e-02, 2.764716e-02, 1.514186e-02, 3.261371e-03, -1.661866e-03, -1.661866e-03, -1.661866e-03, -1.661866e-03, -1.661866e-03, 4.787548e-05, -5.408560e-04, -1.331249e-02, 1.669416e-02, 1.739344e-02) 
cor_met_u2 <- c(-2.246893e-03, -2.632274e-03, -1.049068e-03, -2.192703e-03, -1.948807e-03, -5.081165e-04, 9.637142e-04, -6.389820e-04, -1.113667e-03, -2.423015e-01, -4.794701e-05, -1.412691e-03, -1.321541e-03, -9.755640e-01, -9.682569e-01, -9.530348e-01, -9.129931e-01, -8.893264e-01, -8.197392e-01, -8.077923e-01,-8.077923e-01, -8.077923e-01, -8.069009e-01, -8.060184e-01, -7.557130e-01,-7.496069e-01, -7.100768e-01, -6.772976e-01, -6.075918e-01, -5.945667e-01,-5.296330e-01, -5.198169e-01, -4.598129e-01, -4.484590e-01, -4.466080e-01, -4.401859e-01, -3.982912e-01, -3.982912e-01, -3.982912e-01, -3.982912e-01,-3.982912e-01, -3.956812e-01, -3.681578e-01, -3.640512e-01, -3.532156e-01,-3.064998e-01) 
objwoorden <- c('subcha', 'subchange', 'executant', 'information', 'authorization', 'change', 'origin', 'admi', 'acount', 'start', 'telnummer', 'device', 'mgmt', 'krn', 'uitoef', 'doel', 'titel', 'child', 'calculator', 'bckup', 'execid', 'fgr', 'vanuit','content', 'personeelsnummer', 'enkel', 'niveau', 'value', 'indicator', 'verschil', '1jaar', 'parent', 'jaarmaand','volgnummer', 'parentvolgnummers', 'plt2', 'rsum', 'gebruiksart', 'herstellingskost', 'leeggoedverschil', 'voorraadverschil',     'kasverschil', 'begindatummaand', 'jaarmaand1jaar', 'descr', 'excid') 
+1

你可以发布你的数据的一部分吗? – Jaap

回答

0

一个在你的方法的问题是,你绘制你的阴谋顶部的表。考虑到单词的数量,我会使用ggplot2来绘制图表旁边的表格。您提供的示例数据:

cor_met_u1 <- c(-8.553663e-01, -7.726949e-01, -7.308201e-01, -6.992058e-01, -6.675692e-01, -5.971927e-01, -5.870302e-01, -4.856212e-01, -4.612918e-01, -4.185641e-01, -4.106425e-01, 3.816280e-01, 3.184851e-01, 8.766928e-03, 9.121623e-03, 9.227969e-03, -3.477085e-02, 1.248777e-02, 2.982004e-03, 3.970818e-03, 3.970818e-03, 3.970818e-03, 4.099181e-03, -2.823043e-03, 2.702839e-02,-1.683602e-03, -2.231668e-02, 4.884192e-02, -1.177896e-02, -2.984341e-02, -1.120810e-02, 1.449123e-02, -2.223017e-02, 2.764716e-02, 1.514186e-02, 3.261371e-03, -1.661866e-03, -1.661866e-03, -1.661866e-03, -1.661866e-03, -1.661866e-03, 4.787548e-05, -5.408560e-04, -1.331249e-02, 1.669416e-02, 1.739344e-02) 
cor_met_u2 <- c(-2.246893e-03, -2.632274e-03, -1.049068e-03, -2.192703e-03, -1.948807e-03, -5.081165e-04, 9.637142e-04, -6.389820e-04, -1.113667e-03, -2.423015e-01, -4.794701e-05, -1.412691e-03, -1.321541e-03, -9.755640e-01, -9.682569e-01, -9.530348e-01, -9.129931e-01, -8.893264e-01, -8.197392e-01, -8.077923e-01,-8.077923e-01, -8.077923e-01, -8.069009e-01, -8.060184e-01, -7.557130e-01,-7.496069e-01, -7.100768e-01, -6.772976e-01, -6.075918e-01, -5.945667e-01,-5.296330e-01, -5.198169e-01, -4.598129e-01, -4.484590e-01, -4.466080e-01, -4.401859e-01, -3.982912e-01, -3.982912e-01, -3.982912e-01, -3.982912e-01,-3.982912e-01, -3.956812e-01, -3.681578e-01, -3.640512e-01, -3.532156e-01,-3.064998e-01) 
objwoorden <- c('subcha', 'subchange', 'executant', 'information', 'authorization', 'change', 'origin', 'admi', 'acount', 'start', 'telnummer', 'device', 'mgmt', 'krn', 'uitoef', 'doel', 'titel', 'child', 'calculator', 'bckup', 'execid', 'fgr', 'vanuit','content', 'personeelsnummer', 'enkel', 'niveau', 'value', 'indicator', 'verschil', '1jaar', 'parent', 'jaarmaand','volgnummer', 'parentvolgnummers', 'plt2', 'rsum', 'gebruiksart', 'herstellingskost', 'leeggoedverschil', 'voorraadverschil',     'kasverschil', 'begindatummaand', 'jaarmaand1jaar', 'descr', 'excid') 

# combine the variables in a dataframe 
df <- data.frame(cor_met_u1,cor_met_u2,objwoorden) 
# create a number variable 
df$nr <- seq_along(df$objwoorden) 

# load the required packages 
library(ggplot2) 
library(gridExtra) 

# create the plot (without the table) 
pl <- ggplot(df, aes(x=cor_met_u1, y=cor_met_u2)) + 
    geom_text(position="dodge", aes(label=nr), size=3, color="red") + 
    theme_bw() 

# create the legend table 
tbl <- tableGrob(df[,c("nr","objwoorden")], 
       show.rownames=FALSE, show.colnames=TRUE, 
       gpar.coltext = gpar(col = "red", cex = 1), 
       gpar.coretext = gpar(col = "black", cex = 0.9), 
       gpar.colfill = gpar(fill = NA, col = "white"), 
       gpar.corefill = gpar(fill = NA, col = "white")) 

# combine the plot & table in one plot 
png(filename="plot-plus-table.png", width=1200, height=1000) 
grid.arrange(pl, tbl, nrow=1, widths = c(4/5, 1/5)) 
dev.off() 

这导致了以下情节: enter image description here

用同样的逻辑,你可以添加一个表的蓝色值也是如此。