Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-character plot shapes in ggplot

Tags:

r

ggplot2

Say this is my dataset:

> ( fake = data.frame( id=as.character(18:22), x=rnorm(5), y=rnorm(5) ) )

  id           x           y
1 18  1.93800377  0.67515777
2 19  1.28241814 -0.04164806
3 20 -1.58919444 -0.50885536
4 21 -0.08127943 -1.90003188
5 22  0.78134213  0.17693039

I want a scatterplot of x vs. y in which the plotting shape is the id variable. I tried this:

ggplot( data=fake, aes( x=x, y=y, shape=id) ) + geom_point() +
  scale_shape_manual(values=as.character(fake$id)) + theme_bw()

enter image description here

It appears that only the first character of id is being used as a plotting shape. How can I use the entire string?

like image 205
half-pass Avatar asked May 11 '15 05:05

half-pass


People also ask

What does geom_point () do in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot.

What is Geom_line in Ggplot?

geom_line() connects them in order of the variable on the x axis. geom_step() creates a stairstep plot, highlighting exactly when changes occur. The group aesthetic determines which cases are connected together.

What is AES Ggplot?

Aesthetic Mapping ( aes ) In ggplot2 , aesthetic means “something you can see”. Each aesthetic is a mapping between a visual cue and a variable. Examples include: position (i.e., on the x and y axes) color (“outside” color)


1 Answers

If you want to print text on a plot, use geom_text rather than geom_point

ggplot( data=fake, aes( x=x, y=y, label=id) ) + geom_text() + theme_bw()

The plotting character (shape) used by geom_point can only be a single character. For longer strings, you must use geom_text().

enter image description here

like image 197
MrFlick Avatar answered Oct 09 '22 20:10

MrFlick