Plot density with ggplot2 without line on x-axis - r

I use ggplot2::ggplot for all 2D plotting needs, including density plots, but I find that when plotting a number of overlapping densities with extreme outliers on a single space (in different colors) the line on the x-axis becomes a little distracting.
My question is then, can you remove the bottom section of the density plot from being plotted? If so, how?
You can use this example:
library(ggplot2)
ggplot(movies, aes(x = rating)) + geom_density()
Should turn out like this:

How about using stat_density directly
ggplot(movies, aes(x = rating)) + stat_density(geom="line")

You can just draw a white line over it:
ggplot(movies, aes(x = rating)) +
geom_density() +
geom_hline(color = "white", yintercept = 0)

Related

How to keep coloured geom_sina points within geom_violin plots?

I succeeded to jitter points in violins by combining geom_violin and geom_sina (left plot in the figure above), but when I try to color the points, they are jittered on several columns outside the violins (right plot in figure above).
What I would like to get is the left plot with colored points (I do not care if they are mixed (I mean not grouped by color).
Here is a demo script using mtcars dataset (I do not know mtcars dataset in detail, then apologize if I did some crazy use of the variables).
library(ggplot2)
library(ggforce)
data(mtcars)
p <- ggplot(mtcars, aes(x=factor(vs), y=mpg)) + geom_violin()
p + geom_sina(alpha = 0.5)
p + geom_sina(aes(colour = factor(cyl)), alpha = 0.5)
Thanks to teunbrand.
p + geom_sina(aes(colour = factor(cyl), group = factor(vs)), alpha = 0.5)
makes it.

ggolot horizontal gaps contour

I've been trying to plot contour plots using ggplot2 and csv file. I can't figure out why there are horizontal gaps showing up across the image.
Here is the code:
library(ggplot2)
plot2 <- ggplot(data=thirtyfour,aes( x = X.m., y = Z.m., z = t_ca.2))
plot2
plot2 + geom_tile(aes(fill = t_ca.2) )+
scale_fill_continuous(limits=c(0,0.0219),
breaks=seq(0,0.0219, by=0.01),
low="blue",
high="yellow")
In the geom_tile aesthetic try adjusting the height parameter.
+geom_tile(aes(fill=t_ca.2, height=1)) +...
Otherwise, please provide reproducible example code.

ggplot2 in R: fill underneath a geom_smooth line

I am trying to fill in a portion of a plot underneath a geom_smooth() line.
Example:
In the example the data fits on that curve. My data is not as smooth. I want to use geom_point() and a mix of geom_smooth() and geom_area() to fill in the area under the smoothed line while leaving the points above.
A picture of my data with a geom_smooth():
In other words, I want everything underneath that line to be filled in, like in Image 1.
Use predict with the type of smoothing being used. geom_smooth uses loess for n < 1000 and gam for n > 1000.
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth() +
geom_ribbon(aes(ymin = 0,ymax = predict(loess(hwy ~ displ))),
alpha = 0.3,fill = 'green')
Which gives:

Graphing an average of existing data in R

The the graph I wish to emulate is this:
The graph I have now is this:
What kind of geom would I use to emulate the black line in the first chart?
I am currently using geom_smooth but am aware I might be on the wrong track:
p <- ggplot(df_test1, aes(time, reading))
p + geom_point(alpha = 1/4, colour = "#7F0019")+geom_smooth(colour = "black")+
scale_x_date(breaks="month", labels=date_format("%b"))
Using geom_line results in the following chart:

R - Smoothing color and adding a legend to a scatterplot

I have a scatterplot in R. Each (x,y) point is colored according to its z value. So you can think of each point as (x,y,z), where (x,y) determines its position and z determines its color along a color gradient. I would like to add two things
A legend on the right side showing the color gradient and what z values correspond to what colors
I would like to smooth all the color using some type of interpolation, I assume. In other words, the entire plotting region (or at least most of it) should become colored so that it looks like a huge heatmap instead of a scatterplot. So, in the example below, there would be lots of orange/yellow around and then some patches of purple throughout. I'm happy to further clarify what I'm trying to explain here, if need be.
Here is the code I have currently, and the image it makes.
x <- seq(1,150)
y <- runif(150)
z <- c(rnorm(mean=1,100),rnorm(mean=20,50))
colorFunction <- colorRamp(rainbow(100))
zScaled <- (z - min(z)) / (max(z) - min(z))
zMatrix <- colorFunction(zScaled)
zColors <- rgb(zMatrix[,1], zMatrix[,2], zMatrix[,3], maxColorValue=255)
df <- data.frame(x,y)
x <- densCols(x,y, colramp=colorRampPalette(c("black", "white")))
df$dens <- col2rgb(x)[1,] + 1L
plot(y~x, data=df[order(df$dens),],pch=20, col=zColors, cex=1)
Here are some solutions using the ggplot2 package.
# Load library
library(ggplot2)
# Recreate the scatterplot from the example with default colours
ggplot(df) +
geom_point(aes(x=x, y=y, col=dens))
# Recreate the scatterplot with a custom set of colours. I use rainbow(100)
ggplot(df) +
geom_point(aes(x=x, y=y, col=dens)) +
scale_color_gradientn(colours=rainbow(100))
# A 2d density plot, using default colours
ggplot(df) +
stat_density2d(aes(x=x, y=y, z=dens, fill = ..level..), geom="polygon") +
ylim(-0.2, 1.2) + xlim(-30, 180) # I had to twiddle with the ranges to get a nicer plot
# A better density plot, in my opinion. Tiles across your range of data
ggplot(df) +
stat_density2d(aes(x=x, y=y, z=dens, fill = ..density..), geom="tile",
contour = FALSE)
# Using custom colours. I use rainbow(100) again.
ggplot(df) +
stat_density2d(aes(x=x, y=y, z=dens, fill = ..density..), geom="tile",
contour = FALSE) +
scale_fill_gradientn(colours=rainbow(100))
# You can also plot the points on top, if you want
ggplot(df) +
stat_density2d(aes(x=x, y=y, z=dens, fill = ..density..), geom="tile",
contour = FALSE) +
geom_point(aes(x=x, y=y, col=dens)) +
scale_colour_continuous(guide=FALSE) # This removes the extra legend
I attach the plots as well:
Also, using ggplot2, you can use color and size together, as in:
ggplot(df, aes(x=x, y=y, size=dens, color=dens)) + geom_point() +
scale_color_gradientn(name="Density", colours=rev(rainbow(100))) +
scale_size_continuous(range=c(1,15), guide="none")
which might make it a little clearer.
Notes:
The expression rev(rainbow(100)) reverses the rainbow color scale,
so that red goes with the larger values of dens.
Unfortunately, you cannot combine a continuous legend (color) and a
discrete legend (size), so you would normally get two legends. The
expression guide="none" hides the size legend.
Here's the plot:

Resources