Setting z-limits in ggplot2 `geom_hex()` - r

Is there a way to manually set z-limits in ggplot2 2d bin functions such as geom_hexbin()?
For example;
library(ggplot2)
dat <- data.frame(
x = rnorm(1000),
y = rnorm(1000)
)
ggplot(dat, aes(x, y)) +
geom_hex()
Is there a way to manually set the z-limits? The intended effect is to manually control the points at which the colour scales begin and end.
Thank you!
For extra points, I would also like to change the colours of the colour scale.

You want scale_fill_gradient:
library(ggplot2)
dat <- data.frame(
x = rnorm(1000),
y = rnorm(1000)
)
ggplot(dat, aes(x, y)) +
geom_hex() +
scale_fill_gradient(limits = c(1, 2))

Related

Symmetric y-axis limits for barchart in ggplot2

I would like to make the y-axis of a bar chart symmetric, so that it's easier to see if positive or negative changes are bigger. Since otherwise this is a bit distorted. I do have working code although it's a bit clumsy and I thought it would be great if I could directly do this in the first ggplot() call. So as to say that ylim directly is symmetrical.
set.seed(123)
my.plot <- ggplot( data = data.table(x = 1:10,
y = rnorm(10,0, 2)), aes(x=x, y=y)) +
geom_bar(stat="identity")
rangepull <- layer_scales(my.plot)$y
newrange <- max(abs(rangepull$range$range))
my.plot +
ylim(newrange*-1, newrange)
What about this :
library(ggplot2)
library(data.table)
set.seed(123)
my.data = data.table(x = 1:10, y = rnorm(10,0, 2))
my.plot <- ggplot(data = my.data)+aes(x=x, y=y) +
geom_bar(stat="identity")+ylim((0-abs(max(my.data$y))),(0+max(abs(my.data$y))))
my.plot
You may want to consider using ceiling:
set.seed(123)
library(ggplot2)
library(data.table)
dT <- data.table(x = 1:10, y = rnorm(10,0, 2))
my.plot <- ggplot(dT, aes(x=x, y=y)) +
geom_bar(stat="identity") +
ylim(-ceiling(max(abs(dT$y))), ceiling(max(abs(dT$y))))
This will give you:
> my.plot

How to make ggplot lines run to the edge?

I have data (depth over time) that I want to display with a line plot. For clarity, I want to zoom in on a section but still show the user that the data continues outside the bounds of the plot. So I want the lines to stop at the plot's edge, rather than at the last point. This is straightforward enough in base graphics but I can't make it work in ggplot. Here's an example with base:
d <- data.frame(x = 1:10, y = 1:10)
plot(d$x, d$y, xlim = c(2,9))
lines(d$x, d$y)
A similar approach with ggplot doesn't work; the lines stop at the last point. Example:
d <- data.frame(x = 1:10, y = 1:10)
ggplot(d, aes(x, y)) + geom_point() + geom_line() + xlim(2,9)
Is there a way to get lines to run to the plot's edge in ggplot? Thanks.
try this
d <- data.frame(x = 1:10, y = 1:10)
ggplot(d, aes(x, y)) + geom_point() + geom_line() + coord_cartesian(xlim = c(0,9))
if you want a straight line, abline would be easiest
d <- data.frame(x = 1:10, y = 1:10)
ggplot(d, aes(x, y)) + geom_point() + geom_abline() + xlim(2,9)

Colour lines by mean of value pairs in ggplot2

When mapping colour to lines in ggplot2, e.g.:
x = data.frame(x = 1:6, y = c(0,0,10,10,0,0))
ggplot(x, aes(x, y, col=y)) + geom_line(size=5)
.. the lines colours are mapped to the first data point of each line segment. Is there any easy way to get ggplot to calculate the mean value of both points instead (ie. so the sloping lines are both scaled to the colour for 5)?
Similar idea as #Richard, but use the zoo package.
library(zoo)
x = data.frame(x = 1:6, y = c(0,0,10,10,0,0))
ggplot(x, aes(x, y, col=rollmean(y, 2, fill = 0))) + geom_line(size=5)
Does this do what you want?
x = data.frame(x = 1:6, y = c(0,0,10,10,0,0))
x$c <- rowMeans(cbind(x$y, c(x$y[-1], NA)))
ggplot(x, aes(x, y, col=c)) + geom_line(size=5)

Align x axes of box plot and line plot using ggplot

Im trying to align the x-axes of a bar plot and line plot in one window frame using ggplot. Here is the fake data I'm trying to do it with.
library(ggplot2)
library(gridExtra)
m <- as.data.frame(matrix(0, ncol = 2, nrow = 27))
colnames(m) <- c("x", "y")
for( i in 1:nrow(m))
{
m$x[i] <- i
m$y[i] <- ((i*2) + 3)
}
My_plot <- (ggplot(data = m, aes(x = x, y = y)) + theme_bw())
Line_plot <- My_plot + geom_line()
Bar_plot <- My_plot + geom_bar(stat = "identity")
grid.arrange(Line_plot, Bar_plot)
Thank you for your help.
#eipi10 answers this particular case, but in general you also need to equalize the plot widths. If, for example, the y labels on one of the plots take up more space than on the other, even if you use the same axis on each plot, they will not line up when passed to grid.arrange:
axis <- scale_x_continuous(limits=range(m$x))
Line_plot <- ggplot(data = m, aes(x = x, y = y)) + theme_bw() + axis + geom_line()
m2 <- within(m, y <- y * 1e7)
Bar_plot <- ggplot(data = m2, aes(x = x, y = y)) + theme_bw() + axis + geom_bar(stat = "identity")
grid.arrange(Line_plot, Bar_plot)
In this case, you have to equalize the plot widths:
Line_plot <- ggplot_gtable(ggplot_build(Line_plot))
Bar_plot <- ggplot_gtable(ggplot_build(Bar_plot))
Bar_plot$widths <-Line_plot$widths
grid.arrange(Line_plot, Bar_plot)
The gridlines on the x axes will be aligned if you use scale_x_continuous to force ggplot to use limits you specify.
My_plot <- ggplot(data = m, aes(x = x, y = y)) + theme_bw() +
scale_x_continuous(limits=range(m$x))
Now, when you add the layers, the axes will share the common scaling.

ggplot can not group bars

Who can tell me why ggplot can't give me grouped bars?
ggplot(df, aes(x = factor(labels), y = srednia, dodge=factor(group))) +
labs(title = gen, size=3)+ ylab("Fold change")+ xlab("Linnia komórkowa") +
geom_bar(aes(fill=factor(group)),stat="identity",position ="dodge") +
geom_errorbar(aes(ymin=minus, ymax=plus))
Grouped bars I means something like this (paint art):
Thank you in advance!
I guess you can achieve this by changing the scale for the x axis. Here's a reproducible example and a possible solution.
# packages
require(plyr)
require(ggplot2)
# generate data
set.seed(123)
df <- data.frame(labels=LETTERS[1:6],
group=rep(1:3, each=2),
srednia=runif(6))
# limits for x axis
mylims <- head(unlist(dlply(df, .(group), function(x) c(levels(factor(x$labels)), "space"))), -1)
# additional space between groups
ggplot(df, aes(x = factor(labels), y = srednia, dodge=factor(group))) +
geom_bar(aes(fill=factor(group)),stat="identity") +
scale_x_discrete(limits=mylims, breaks=levels(factor(df$labels)))
# removing space within group
ggplot(df, aes(x = factor(labels), y = srednia, dodge=factor(group))) +
geom_bar(aes(fill=factor(group)),stat="identity", width=1) +
scale_x_discrete(limits=mylims, breaks=levels(factor(df$labels)))

Resources