How can I label points in this scatterplot?

RLabelPointScatter Plot

R Problem Overview


Can you help me on putting labels on the following graph?

http://img849.imageshack.us/img849/7594/snap1034.png

The code i use is:

valbanks<-scan("banks.txt", what=list(0,0,""), sep="", skip=1, comment.char="#")
valbanks

valj2007<-valbanks[[1]]
valj2009<-valbanks[[2]]
namebank<-valbanks[[3]]

percent_losses<-(valj2009-valj2007)/valj2007
percent_losses

abs_losses<-(valj2007-valj2009)
abs_losses

plot(abs_losses, percent_losses,main="Absolute Losses vs. Relative Losses(in%)",xlab="Losses (absolute, in miles of millions)",ylab="Losses relative (in % of January´2007 value",col="blue", pch = 19, cex = 1, lty = "solid", lwd = 2,text(percet_losses, abs_losses,namebank))

text(percet_losses, abs_losses,labels=namebank, cex= 0.7, offset = 10)

and the data is:

Value_Jan_2007	Value_Jan_2009	Bank	#
49	16	Morgan_Stanley
120	4.6	RBS
76	10.3	Deutsche_Bank
67	17	Credit_Agricole
80	26	Societé_Generale
91	7.4	Barclays
108	32.5	BNP-Paribas
93	26	Unicredit
116	35	UBS
75 27	Credit_Suise
100	35	Goldman_Sachs
116	64	Santander
255	19	Citigroup
165	85	JP_Morgan
215	97	HSBC

Thank you very much

What is the option to put the label at the bottom, right, up, left,... ?

Summarizing: I want a graph similar that the one that is in the second message from this post but there is no the code there.

What code i have to put?


if i put text(percet_losses, abs_losses,namebank), i obtain nothing: the graph continue without changes.

Could you show me the code i need to put?

Thanks

R Solutions


Solution 1 - R

Your call to text() doesn't output anything because you inverted your x and your y:

plot(abs_losses, percent_losses, 
     main= "Absolute Losses vs. Relative Losses(in%)",
     xlab= "Losses (absolute, in miles of millions)",
     ylab= "Losses relative (in % of January´2007 value)",
     col= "blue", pch = 19, cex = 1, lty = "solid", lwd = 2)

text(abs_losses, percent_losses, labels=namebank, cex= 0.7)

Now if you want to move your labels down, left, up or right you can add argument pos= with values, respectively, 1, 2, 3 or 4. For instance, to place your labels up:

 text(abs_losses, percent_losses, labels=namebank, cex= 0.7, pos=3)

enter image description here

You can of course gives a vector of value to pos if you want some of the labels in other directions (for instance for Goldman_Sachs, UBS and Société_Generale since they are overlapping with other labels):

 pos_vector <- rep(3, length(namebank))
 pos_vector[namebank %in% c("Goldman_Sachs", "Societé_Generale", "UBS")] <- 4
 text(abs_losses, percent_losses, labels=namebank, cex= 0.7, pos=pos_vector)

enter image description here

Solution 2 - R

For just plotting a vector, you should use the following command:

text(your.vector, labels=your.labels, cex= labels.size, pos=labels.position)

Solution 3 - R

I have tried directlabels package for putting text labels. In the case of scatter plots it's not still perfect, but much better than manually adjusting the positions, specially in the cases that you are preparing the draft plots and not the final one - so you need to change and make plot again and again -.

Solution 4 - R

You should use labels attribute inside plot function and the value of this attribute should be the vector containing the values that you want for each point to have.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMika IkeView Question on Stackoverflow
Solution 1 - RplannapusView Answer on Stackoverflow
Solution 2 - RMohammadView Answer on Stackoverflow
Solution 3 - RAliView Answer on Stackoverflow
Solution 4 - RLucia PasarinView Answer on Stackoverflow