1
votes

I'm trying to draw a plot in ggplot (geom_point), and I am able to draw it, but I would like to manipulate the appearance of a specific symbol based on some variables.

I would like to rotate the symbol (pch = 22) by an angle defined by a data.frame. Pch = 22 looks like a rectangle. Additionally I would like to change the width of the rectagle, also according to the value defined in a dataframe.

Here is some sample data:

plotdata <- data.frame(x=c(4,6,7,10), 
            y=c(5,6,8,9), angle=c(pi/3, 2*pi/3, pi, pi/6), 
            widthparameter = c(2, 3, 5, 7))

Basically, I want the width to change in a way that is proportion to a the width parameter. So in this example data, the width will change in proportion to the range 2-7. For example, if the width could be 1mm for the value of "widthparameter" 2, and the width could be 3mm for the value of "widthparameter" 7. So "widthparameter" of 3 and 5 will be proportionally somewhere between 2mm and 3 mm.

I'm new to R so manipulating symbols like this in ggplot seems to me like an impossibly difficult task.

Please help,

1
your best bet is probably to create a new geom + scale. I once had those for drawing small regular polygons, but that was years ago and the internals have changed too much to be of any use nowbaptiste

1 Answers

1
votes

There are a number of components to this

library(tidyverse)
ggplot(plotdata, aes(x, y, size=widthparameter))+geom_point() 

Will generate a plot with different sizes per the width parameter. Additionally you could scale by area - lots more information here http://docs.ggplot2.org/current/scale_size.html

ggplot(plotdata, aes(x, y, size=widthparameter))+geom_point()+scale_size_area()

On the rotational aspect, you are going to have a difficult time - plus using a square (pch = 22) and rotating it doesn't mean much when every quarter turn looks the same... May be better to turn the angle to a factor and plot a different shape for each factor level - or use a something like scale_colour_gradientn(...) to color the angles on a continuous scale.

There is loads of information for making figures using ggplot2, my stab at explaining a small portion of it is http://ben-williams.github.io/updated_ggplot_figures.html maybe it will be of use while you learn R.