Reputation: 119
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,
Upvotes: 3
Views: 2148
Reputation: 336
Rotation is possible by plotting characters rather than markers using geom_text. In the example below i've used the unicode character for a rectangle (\u25AF) which is a close match to the symbol (pch=22) you requested. The angle parameter is in degrees, so need to convert from your radians.
library(ggplot2)
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))
ggplot(plotdata, aes(x, y, size=widthparameter)) +
geom_text(label = "\u25FB",aes(angle=angle *180/pi))
Upvotes: 2
Reputation: 2050
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.
Upvotes: 2