How to plot points around a circle in R - r

I am trying to plot some data on directions which vary from 0 to360 deg. The most intuitive way of doing this is around a circle where I can plot each point (I only have 13 points to plot).
cont=c(319,124,182,137,55,302,221,25,8,36,132,179,152)
My data for one plot
I tried following the ggplot2 guides and have not got it to work. I'm not very good at ggplot though...
(my dataframe is called "data")
ggplot(data, aes(x=1), ) + coord_polar(theta = "y") +geom_point(y=cont)

It works adding y to the ggplot mapping
data <- data.frame(cont = cont)
ggplot(data, aes(x=1, y = cont)) + coord_polar(theta = "y") + geom_point()
You can add other ggplot parameters to improve the appearence.

Have you tried polar.plot from plotrix library?

Related

Plotting multiple wordcloud (ggwordcloud) with other types of plots in ggplot2

I am trying to plot several wordclouds in a scatterplot and I wonder if one can control the position of a wordcloud in ggplot?
As an example the code below overlays both wordclouds around the origin of the plot.
Say I want to place the second wordcloud at x=4 and y =35. Is that possible?
library(ggplot2)
library(ggwordcloud)
ggplot() +
geom_point(mtcars,mapping=aes(wt,mpg)) +
geom_text_wordcloud(love_words_small,mapping=aes(label=word)) +
geom_text_wordcloud(mtcars,mapping=aes(label=rownames(mtcars))) +
theme_minimal()
I was looking for the exact same thing. Looks like you can simply add the x and y aesthetic arguments. Ie.
ggplot() +
geom_point(mtcars,mapping=aes(wt,mpg)) +
geom_text_wordcloud(love_words_small,mapping=aes(label=word)) +
geom_text_wordcloud(mtcars,mapping=aes(label=rownames(mtcars), x=4,y=35)) +
theme_minimal()
What I did may be more generally helpful for folks, which is to pass x and y vectors:
library(tidyverse)
library(ggwordcloud)
ggplot(data = mtcars %>% mutate(car_names = rownames(mtcars)) %>%
group_by(cyl),
mapping = aes(label=car_names, x=mpg, y=disp)) +
geom_text_wordcloud()
Perhaps you could save the wordclouds as separate plots, and then add them to one plot with cowplot or gridExtra or any of the packages that lets you combine plots?

Heatscatter plot for multiple values on the same day using stat_density_2d in r

I would like a plot that looks like this:
Using ggplot. The one above was made from heatscatter in the LSD package.
I tried using this code:
p = ggplot(data = emr.ext.melt, aes(Date,NDVI))
p + geom_point() + stat_density2d(aes(fill=..level..), geom="polygon") +
scale_fill_gradient(low="blue", high="green")+ scale_y_continuous(limits = c(-1, 1))
But, got this weird plot. I just want a scatter plot colored based on the density of points for that given day. I do not want to use hexplot either.
Thank you for your help!

3-variables plotting heatmap ggplot2

I'm currently working on a very simple data.frame, containing three columns:
x contains x-coordinates of a set of points,
y contains y-coordinates of the set of points, and
weight contains a value associated to each point;
Now, working in ggplot2 I seem to be able to plot contour levels for these data, but i can't manage to find a way to fill the plot according to the variable weight. Here's the code that I used:
ggplot(df, aes(x,y, fill=weight)) +
geom_density_2d() +
coord_fixed(ratio = 1)
You can see that there's no filling whatsoever, sadly.
I've been trying for three days now, and I'm starting to get depressed.
Specifying fill=weight and/or color = weight in the general ggplot call, resulted in nothing. I've tried to use different geoms (tile, raster, polygon...), still nothing. Tried to specify the aes directly into the geom layer, also didn't work.
Tried to convert the object as a ppp but ggplot can't handle them, and also using base-R plotting didn't work. I have honestly no idea of what's wrong!
I'm attaching the first 10 points' data, which is spaced on an irregular grid:
x = c(-0.13397460,-0.31698730,-0.13397460,0.13397460,-0.28867513,-0.13397460,-0.31698730,-0.13397460,-0.28867513,-0.26794919)
y = c(-0.5000000,-0.6830127,-0.5000000,-0.2320508,-0.6547005,-0.5000000,-0.6830127,-0.5000000,-0.6547005,0.0000000)
weight = c(4.799250e-01,5.500250e-01,4.799250e-01,-2.130287e+12,5.798250e-01,4.799250e-01,5.500250e-01,4.799250e-01,5.798250e-01,6.618956e-01)
any advise? The desired output would be something along these lines:
click
Thank you in advance.
From your description geom_density doesn't sound right.
You could try geom_raster:
ggplot(df, aes(x,y, fill = weight)) +
geom_raster() +
coord_fixed(ratio = 1) +
scale_fill_gradientn(colours = rev(rainbow(7)) # colourmap
Here is a second-best using fill=..level... There is a good explanation on ..level.. here.
# load libraries
library(ggplot2)
library(RColorBrewer)
library(ggthemes)
# build your data.frame
df <- data.frame(x=x, y=y, weight=weight)
# build color Palette
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")), space="Lab")
# Plot
ggplot(df, aes(x,y, fill=..level..) ) +
stat_density_2d( bins=11, geom = "polygon") +
scale_fill_gradientn(colours = myPalette(11)) +
theme_minimal() +
coord_fixed(ratio = 1)

overlay rotated density plot

I'm struggling to overlap rotated density plot onto the original scatterplot. Here are 2 plots I have:
require(ggplot2); set.seed(1);
df1 <- data.frame(ID=paste0('ID',1:1000), value=rnorm(1000,500,100))
p1 <- ggplot(data = df1, aes(x=reorder(ID, value), y=value)) +
geom_point(size=2, alpha = 0.7)+
coord_trans(y="log10")
p2 <- ggplot(data = df1, aes(x=value)) +
coord_trans(x="log10") +
geom_density() +
coord_flip()
p1
p2
First, there's a little problem with the density plot that its vertical axis is not log10-transformed. But main issue is that I can't find how to draw it on the previous plot keeping correct coordinates.
Because you are using coord_flip on your second plot you are effectively trying to plot two different values onto the same x axis (density and ID). There are plenty of posts discouraging this, here's one for example: How do I plot points with two different y-axis ranges on the same panel in the same X axis?.

Plotting density plots on the negative y axis with ggplot2, R

I'm trying to plot densities on both sides of the x axis - trying to visualize reads mapping to both strands of a genomic region.
I have two problems :
I want to use ggplot2 but
I don't know how to extract the data from the plot object, or invert the plot that has already been made.
Any help greatly appreciated!
Your question is very unclear, but I found the "densities on both sides of the x axis" potentially interesting. So maybe you're trying to do something like this:
d <- data.frame(x = rnorm(1000),x1 = rnorm(1000,sd = 0.5))
ggplot(data = d,aes(x = x)) +
geom_density() +
geom_density(aes(x = x1,y = -(..density..)))

Resources