R ggplot2 geom_jitter: plotting all zeros - r

In R ggplot2, when I plot all zeros and use geom_jitter(), some variations are automatically added to zeros. How can I undo that? I still want all points at 0 y axis.
y = rep(0,100)
x = rep(c("A","B","C","D"),25)
D = data.frame(x,y)
library(ggplot2)
ggplot(D,aes(x=x,y=y))+geom_boxplot() + geom_jitter()

If you need to keep the dots and spread them horizontally, you can use geom_jitter(height = 0). This will force the vertical variation/jitter to zero, but still allows the points to "jitter" horizontally.
ggplot(D, aes(x = x, y = y)) +
geom_boxplot() +
geom_jitter(height = 0)

Related

How to make a circled bubble plot using ggplot2 coord_polar()?

I have an example data, which does not have x- and y-axis information. I would like to make a bubble plot using R package ggplot2, and arrange the bubbles in a circled manner.
data <- data.frame(group = paste("Group", letters[1:11]),
value = sample(seq(1,100),11))
Thanks a lot.
You can just put a dummy value for y and make group your x values in aes.
ggplot(data, aes(x = group, y = 0, size = value)) +
coord_polar() +
geom_point()

When using `scale_x_log10`, how can I map `geom_text` accurately to `geom_bin2d`?

A great answer on how to label the count on geom_bin2d, can be found here:
Getting counts on bins in a heat map using R
However, when modifying this to have a logarithmic X axis:
library(ggplot2)
set.seed(1)
dat <- data.frame(x = rnorm(1000), y = rnorm(1000))
# plot MODIFIED HERE TO BECOME log10
p <- ggplot(dat, aes(x = x, y = y)) + geom_bin2d() + scale_x_log10()
# Get data - this includes counts and x,y coordinates
newdat <- ggplot_build(p)$data[[1]]
# add in text labels
p + geom_text(data=newdat, aes((xmin + xmax)/2, (ymin + ymax)/2,
label=count), col="white")
This produces labels that are very poorly mapped to their respective points.
How can I correct the geom_text based labels to correctly map to thier respective points?
Apply logarithmic transformation directly on x values, not on scale. Change only one line of your code:
p <- ggplot(dat, aes(x = log10(x), y = y)) + geom_bin2d()
That allows to keep negative values and produces the following plot:

Variable line size using ggplot2

I would like to change the line size with a continuous variable. Now I used the geom_line with aesthetics size. For example:
x <- 1:100
y <- x * x
z <- abs(cos(x * pi / (max(x))))
df <- data.frame(x = x, y = y, z = z)
library(ggplot2)
ggplot(df, aes(x, y, size = z)) + geom_line()
But there are some spaces among segments (see figure below. Please zoom in to see the spaces). it seems ggplot2 uses rectangles to plot each segment.
I have increased the point number, but spaces till exist for bigger curvature.
My question is how to remove these spaces. I really appreciate it for any suggestions.
Adjust the multiplier to your preference:
mult <- 200
ggplot(df, aes(x, y)) + geom_line() + geom_ribbon(aes(ymin=y-mult*z, ymax=y+mult*z))

How to make variable bar widths in ggplot2 not overlap or gap

geom_bar seems to work best when it has fixed width bars - even the spaces between bars seem to be determined by width, according to the documentation. When you have variable widths, however, it does not respond as I would expect, leading to overlaps or gaps between the different bars (as shown here).
To see what I mean, please try this very simple reproducible example:
x <- c("a","b","c")
w <- c(1.2, 1.3, 4) # variable widths
y <- c(9, 10, 6) # variable heights
ggplot() +
geom_bar(aes(x = x, y = y, width = w, fill=x),
stat="identity", position= "stack")
What I really want is for the different bars to be just touching, but not overlapping, like in a histogram.
I've tried adding position= "stack", "dodge", and "fill, but none work. Does the solution lie in geom_histogram or am I just not using geom_bar correctly?
P.s. to see the issue with gaps, try replacing 4 with 0.5 in the above code and see the outcome.
Seems that there isn't any straightforward solution, so we should treat x-axis as continuous in terms of w and manually compute required positions for ticks and bar centers (this is useful):
# pos is an explicit formula for bar centers that we are interested in:
# last + half(previous_width) + half(current_width)
pos <- 0.5 * (cumsum(w) + cumsum(c(0, w[-length(w)])))
ggplot() +
geom_bar(aes(x = pos, width = w, y = y, fill = x), stat = "identity") +
scale_x_continuous(labels = x, breaks = pos)
You can now do this with the mekko package: https://cran.r-project.org/web/packages/mekko/vignettes/mekko-vignette.html

ggplot2: Reading maximum bar height from plot object containing geom_histogram

Like this previous poster, I am also using geom_text to annotate plots in gglot2. And I want to position those annotations in relative coordinates (proportion of facet H & W) rather than data coordinates. Easy enough for most plots, but in my case I'm dealing with histograms. I'm sure the relevant information as to the y scale must be lurking in the plot object somewhere (after adding geom_histogram), but I don't see where.
My question: How do I read maximum bar height from a faceted ggplot2 object containing geom_histogram? Can anyone help?
Try this:
library(plyr)
library(scales)
p <- ggplot(mtcars, aes(mpg)) + geom_histogram(aes(y = ..density..)) + facet_wrap(~am)
r <- print(p)
# in data coordinate
(dc <- dlply(r$data[[1]], .(PANEL), function(x) max(x$density)))
(mx <- dlply(r$data[[1]], .(PANEL), function(x) x[which.max(x$density), ]$x))
# add annotation (see figure below)
p + geom_text(aes(x, y, label = text),
data = data.frame(x = unlist(mx), y = unlist(dc), text = LETTERS[1:2], am = 0:1),
colour = "red", vjust = 0)
# scale range
(yr <- llply(r$panel$ranges, "[[", "y.range"))
# in relative coordinates
(rc <- mapply(function(d, y) rescale(d, from = y), dc, yr))

Resources