geom_dumbbell with coord_trans - r

I am trying to generate a dumbbell plot in ggplot, while also trying to log transform the x and y axes. System throws an error that I can't understand. Looking for some help.
Here is the sample code:
#create the data
df <- data.frame(x = c(1, 2, 3, 100),
xend = c(2, 4, 6, 110),
y = c(1, 2, 3, 100))
#plot the untransformed dumbbell plot
library('ggalt')
ggplot(df, aes(x = x, xend = xend, y = y)) +
geom_dumbbell()
#Try the plot with coordinate transformation
ggplot(df, aes(x = x, xend = xend, y = y)) +
geom_dumbbell() +
coord_trans(x = 'log2', y = 'log2')
Throws error:
Error in [.data.frame(df, , c("alpha", "colour", "size", "linetype")) :
undefined columns selected
If I amend coord_trans so that coord_trans(x = 'log2', xend = 'log2', y = 'log2), I get the error:
Error in coord_trans(x = "log2", xend = "log2", y = "log2") :
unused argument (xend = "log2")
What I want is the equivalent of this, with geom_dumbbell() rather than geom_point():
ggplot(df, aes(x = x, y = y)) +
geom_point() +
coord_trans(x = 'log2', y = 'log2')
Any thoughts on how I can get geom_dumbbell() to work with coord_trans()?

That is weird, I don't know why it doesn't work with geom_dumbbell. I also didn't know that geom_dumbbell exists! So here is a hack I've been doing for years using geom_linerange and geom_point to build the components:
ggplot(df, aes(x=x, y = y)) +
geom_linerange(aes(xmin=x, xmax=xend)) +
geom_point() +
geom_point(aes(x=xend)) +
coord_trans(x = 'log2', y = 'log2')

Related

geom_area with fill colour based on value

I'd like to make a geom_area plot with the fill colour based on the y (or any other) value, similar to the geom_density_ridges_gradient function of the ggridges package. I could achieve this with multiple geom_cols but I want to have the nice smooth geom_area style. Do you have any idea?
This code illustrates what I want to do:
data <- data.frame(x = 1:100,
y = rnorm(100, 20,3))
#I'd like to have an area plot with the fill colour based on the y values
ggplot(data = data, aes(x = x, y = y))+
geom_area(aes(fill = y))
#As in a bar plot, but with a smooth area, not a composite of rectangles
ggplot(data = data, aes(x = x, y = y))+
geom_col(aes(fill = y))
Thanks a lot!
You can use approx to get a huge number of interpolated values and plot them as very thin vertical geom_segments
data2 <- as.data.frame(approx(data$x, data$y, seq(1, 100, len = 5000)))
ggplot(data = data2, aes(x = x, y = y))+
geom_segment(aes(xend = x, yend = 0, colour = y), linewidth = 0.1) +
geom_area(fill = NA, color = "black") +
scale_color_viridis_c() +
theme_minimal(base_size = 20)

Plot customised vertical lines up to the curve

I want the vertical lines to not go beyond the curve line after they intersect
Example data:
x <- 1:50
dat <- data.frame(x = x, y = 1 - exp(-x/43)^4)
ggplot(dat, aes(x = x, y = y)) +
geom_line() +
geom_vline(xintercept = c(10, 20, 30),
lty = "dashed")
Use geom_segment instead:
ggplot(dat, aes(x = x, y = y)) +
geom_line() +
geom_segment(aes(x = x, xend = x, y = min(dat$y), yend = y),
data = dat[ dat$x %in% c(10, 20, 30), ],
lty = "dashed")

ggplot2: geom_bar with custom y limits [duplicate]

This question already has answers here:
geom_bar bars not displaying when specifying ylim
(4 answers)
Closed 17 days ago.
I want to draw a bar chart with ggplot2 along with custom y limits.
Type <- LETTERS[1:5]
Y <- c(99, 99.5, 99.0, 98.8, 98.5)
df <- data.frame(Type, Y)
The following code works fine for bar chart:
library(ggplot2)
ggplot(data = df, mapping = aes(x = Type, y = Y, fill = Type)) +
geom_bar(stat = "identity") +
theme_bw()
However, I'm not able to set the y limits. See the code below.
ggplot(data = df, mapping = aes(x = Type, y = Y, fill = Type)) +
geom_bar(stat = "identity") +
scale_y_continuous(limits = c(90, 100)) +
theme_bw()
ggplot(data = df, mapping = aes(x = Type, y = Y, fill = Type)) +
geom_bar(stat = "identity") +
ylim(90, 100) +
theme_bw()
Edited
I guess this behavior is due to stat = "identity".
Alternative, using coord_cartesian:
ggplot(data = df, mapping = aes(x = Type, y = Y, fill = Type)) +
geom_bar(stat = "identity") +
coord_cartesian(ylim = c(90, 100)) +
theme_bw()
Gives you:
Solution using geom_rect() instead of geom_bar():
# Generate data
Type <- LETTERS[1:5]
Y <- c(99, 99.5, 99.0, 98.8, 98.5)
df <- data.frame(Type, Y)
# Plot data
library(ggplot2)
ggplot() +
geom_rect(data = df,
aes(xmin = as.numeric(Type) - 0.3,
xmax = as.numeric(Type) + 0.3,
ymin = 90, ymax = Y,
fill = Type)) +
scale_x_continuous(label = df$Type, breaks = 1:nrow(df))
In geom_rect() specify x coordinates as as.numeric(X) -/+ value; ymin coordinates as wanted lower limit and ymax as actual Y values.

Using different data in ggplot's geom_rug than I use in the rest of the plot

I am having trouble getting geom_rug to plot some data into an existing plot. Here's an example plot, where I am comparing some visit day to the magnitude of some measurement.
test <- data.frame(
visit = rep(c(0, 1.5, 3.5, 6.5, 12), 5),
mag = rnorm(n = 25)
)
ggplot(test, aes(x = visit, y = mag)) + geom_point()
Which generates the following plot.
I also have some other data, that I'd like to add just as extra marks on the x axis.
vac <- data.frame(
visit = c(2, 4, 6, 8)
)
For reasons I don't understand, I get no plot at all when I run the following code.
ggplot(test, aes(x = visit, y = mag)) + geom_point() +
geom_rug(data=vac, aes(x = visit))
I presume I have messed up on syntax somehow, but I can't seem to figure out what I am doing wrong here. Any suggestions?
You should specify inherit.aes = FALSE in the geom_rug() line, otherwise it inherits y = mag from the main ggplot() call.
ggplot(test, aes(x = visit, y = mag)) +
geom_point() +
geom_rug(data=vac, aes(x = visit), inherit.aes = F)
I would try either this:
ggplot(test, aes(x = visit, y = mag)) + geom_point() +
geom_rug(data=vac, aes(x = visit,y = NULL))
or perhaps a better option this:
ggplot() +
geom_point(data = test,aes(x = visit,y = mag)) +
geom_rug(data=vac, aes(x = visit))

Manually set y axis tick labels in a facet wrap forest plot

I'm producing a facet wrapped forest plot and I'd like to manually set the y-axis labels.
Here's my example data:
set.seed(1)
df <- data.frame(x=rnorm(10),y=c(1:5,1:3,1:2),
group=c(rep("a",5),rep("b",3),rep("c",2)),
name=c(paste("a",1:5,sep=""),paste("b",1:3,sep=""),paste("c",1:2,sep="")))
df$xmin <- df$x-runif(10,0.5,0.7)
df$xmax <- df$x+runif(10,0.5,0.7)
And here's the code I'm trying:
library(ggplot2)
p <- ggplot(df,aes(y = y, x = x))+
geom_point()+
scale_y_discrete(limits = df$name, expand = c(.1,0))+
facet_wrap(~group,ncol=3,scales="free")+
geom_segment(aes(x = xmin, xend = xmax, y = y, yend = y))+
geom_vline(lty=2, aes(xintercept=0), colour = 'red')
Which produces this figure:
As you can see the y-axis tick labels of the middle and right most facets are not consistent with df$name.
ggplot(df,aes(y = name, x = x)) +
geom_point() +
facet_wrap(~group,ncol=3,scales="free") +
geom_segment(aes(x = xmin, xend = xmax, y = name, yend = name)) +
geom_vline(lty=2, aes(xintercept=0), colour = 'red')

Resources