Reputation: 8366
I have a line graph using ggplot2 with three lines (with variable names, say, 'A', 'B' and 'C' in my data frame). I want to add a geom_smooth using method=loess
, but I want to add the regression line based on the minimum of 'B' and 'C'. Is there a way to do this?
To illustrate, here's a mock code:
names <- c('n1', 'n2', 'n3', 'n4', 'n5')
aline <- c(0.18, 0.21, 0.23, 0.20, 0.16)
bline <- c(0.50, 0.40, 0.30, 0.20, 0.10)
cline <- c(0.14, 0.20, 0.30, 0.35, 0.33)
min_bc <- c(0.14, 0.20, 0.30, 0.20, 0.10)
df <- data.frame(name, aline, bline, cline)
df.m <- melt(df)
g <- ggplot(df.m, aes(group=1, names, value, colour=variable))
g <- g + geom_line(aes(group=variable))
g <- g + geom_point(aes(colour=variable), alpha=0.4)
I want to add a regression line using aline
and min_bc
, without actually plotting min_bc
.
Additionally, I would like to throw this in: In general, I may have some data, and I want to want to plot (in the same graph) different lines (or points, bars, etc.) using different transformations of the data. Is there any comprehensive document where I can get the broad picture of how to do such things in R/ggplot?
Upvotes: 1
Views: 6780
Reputation: 60944
Normally, one sets the dataset relevant for a plot in the main call to ggplot
:
ggplot(data, aes()) + geom_point()
However, you can also set the dataset individually for the geom
:
ggplot(data1, aes()) + geom_point(data = data2)
Using this technique, you can precompute the dataset off which you want to plot the LOESS, and feed that to geom_smooth()
. The following example confirms this hypothesis:
df1 = data.frame(x = 1:100, y = runif(100))
df2 = data.frame(x = 1:100, y = runif(100) + 1)
ggplot(df1, aes(x, y)) + geom_point() + geom_smooth(data = df2)
In this example, both datasets have the same column names. If this is not the case, you need to also tweak the aes
setting within geom_smooth
.
Do keep in mind that plotting a smoothing of another dataset than the one you plot beneath it could make things very unclear.
Upvotes: 6