ggolot horizontal gaps contour - r

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.

Related

Maintaining linked zoom between subplots using plotly

I would like to create a facet plot with ggplot and show the x-axis with ticks on all facets. Then I want to use ggplotly to convert the result to a plotly graph. However when I use scales='free_x' to add the extra x-axes to the facets, the zoom linkage is lost. i.e. when I zoom on one of the facets it doesn't zoom on the others automatically. Is there a way to add that functionality back in?
This is a reprex showing the problem:
library(ggplot2)
library(ggthemes)
library(plotly)
p <- ggplot(mtcars, aes(mpg, hp)) + geom_point() + facet_wrap(~carb, ncol = 1, scales='free_x') +
theme_tufte() + theme(axis.line=element_line()) +
scale_x_continuous(limits=c(10,35)) + scale_y_continuous(limits=c(0,400))
ggplotly(p)
Notice here when I zoom in on the top plot, the other plots remain unchanged:

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)

Plot density with ggplot2 without line on x-axis

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)

Rug plot does not work when specifying alpha colors?

This works:
plot(1:10, col=rgb(0,0,0,1))
rug(1:10, col=rgb(0,0,0,1))
And this does not work (no rugs are additionally plotted):
plot(1:10, col=rgb(0,0,0,0.9))
rug(1:10, col=rgb(0,0,0,0.9))
Why? And how can I fix this?
The reason why I need this is because I would like to add alpha colors so that overlapping "rugs" are darker. Without beeing able to specify alpha colors, I am only able to plot this:
plot(rep(1:10,100), rep(1:10,100), col=rgb(0,0,0,1))
rug(jitter(rep(1:10,100)), col=rgb(0,0,0,1))
# This is quite easy to do with ggplot2
# data
df = data.frame(x = rep(1:10,100), y = rep(1:10,100))
# code
library(ggplot2)
ggplot(df, aes(x=x, y=y)) +
geom_point() +
geom_rug(sides="b", position="jitter", alpha=0.2, colour="blue") +
scale_x_continuous(breaks=seq(0,10,by=2)) +
scale_y_continuous(breaks=seq(0,10,by=2))

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