Scatterplot with two legends with corresponding colors

I have the following plot: enter image description here

So, I have two groups of words that I colored red and blue. Also, I have two legends where each legend corresponds to one group.

My code looks like this:

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

      

Now I would like to adapt the following things, but I cannot find how to do it:

  • Legend with title Object names: I would like every word in this legend to be red, and I would like every word in this legend to have a corresponding number in the plot as a key.

  • Same as above: legend with title Keywords: I would like every word in this legend to be blue. I would like every word in this legend to have the corresponding number in the plot as a key.

  • My legends don't have enough space, some legends are not built. How can I decrease the space of the points plot and increase the space for the legends?

My data (red part):

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

      

+3


source to share


1 answer


One of the problems with your approach is that you are composing a table on top of your plot. Given the word count, I would use ggplot2

to build a table next to the plot. With the data examples you provided:

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

      



this leads to the following conspiracy: enter image description here

With the same logic, you can also add a table for blue values.

0


source







All Articles