Here is my example.
library(ggplot2)
my_df <- data.frame(x = c(1,2,3), y = c(4,5,6), y_min = c(1,1,2),
y_max = c(7,8,8))
ggplot(data = my_df)+
geom_line(aes(x = x, y = y, color = "blue"))+
geom_ribbon(aes(ymin =y_min, ymax =y_max, x= x), fill = "grey70")
I would like to get back something resembling line plot and confidence interval around it, but I am only getting confidence interval (ribbon) not the main line.
What am I missing?
We could set alpha = 0.3 to see the line and change grey70 to grey grey12:
ggplot(my_df) + geom_line(aes(y=y, x=x, colour = "blue"))+
geom_ribbon(aes(ymin=y_min, ymax=y_max, x=x, fill = "band"), alpha = 0.3)+
scale_colour_manual("",values="blue")+
scale_fill_manual("",values="grey12")
Related
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)
I'm using the special pch (21-25) to make a scatter plot with ggplot since I want the fill of the points to vary with factor levels, but I also want the border of the points to be fixed as black. I wish to add an error bar to each of the points and I want the error bar to be the same color as the fill of the points. However, since I'm fixing the colour aesthetic of points to black and geom_errorbar() uses this to plot the color of the error bars I can't seem to figure out how to get the result I want.
Here is a simple example where I'm getting the undesired result (black color in the error bars):
library(ggplot2)
test <- cbind.data.frame(
x = rnorm(10),
y = rnorm(10),
stdv = sd(rnorm(10)),
fl = c(rep("foo", 5), rep("bar", 5))
)
ggplot(data = test, aes(x = x, y = y, colour = fl, fill = fl, ymin = y - stdv, ymax = y + stdv)) +
geom_point(shape = 21, size = 3) +
scale_colour_manual(values = rep("black", nrow(test))) +
geom_errorbar()
You can try with new_scale_color() from ggnewscale package (for sure this a trick I learnt from #AllanCameron):
library(ggplot2)
library(ggnewscale)
#Data
test <- cbind.data.frame(
x = rnorm(10),
y = rnorm(10),
stdv = sd(rnorm(10)),
fl = c(rep("foo", 5), rep("bar", 5))
)
#Plot
ggplot(data = test, aes(x = x, y = y,
colour = fl,
fill = fl,
ymin = y - stdv,
ymax = y + stdv)) +
geom_point(shape = 21, size = 3) +
scale_colour_manual(values = rep("black", nrow(test))) +
new_scale_color()+
geom_errorbar(aes(color=fl))
Output:
Any reason you're using scale_colour_manual() instead of just using colour = "black"? If your actual example allows you to use that, then, you don't need ggnewscale:
library(ggplot2)
test <- cbind.data.frame(
x = rnorm(10),
y = rnorm(10),
stdv = sd(rnorm(10)),
fl = c(rep("foo", 5), rep("bar", 5))
)
ggplot(data = test, aes(x = x, y = y, colour = fl, fill = fl, ymin = y - stdv, ymax = y + stdv)) +
geom_point(shape = 21, size = 3, colour = "black") +
geom_errorbar()
Created on 2020-12-11 by the reprex package (v0.3.0)
Say if have the following plot.
library(ggplot2)
n <- 1169
df22 <- data.frame(x = 1:n, val = seq(0, 0.5, length.out = n), type = 1)
ggplot(df22, aes(x = x, y = val)) +
geom_ribbon(aes(ymax = val, ymin = 0, fill = type, group = type))
Instead of the blue color i would like to have a Gradient fill (from blue to red - vertically. So starting with blue at the bottom and red on top with a Parameter to Control the smoothness of Color change).
I found the following resource:
https://ggplot2.tidyverse.org/reference/scale_gradient.html
Unfortunately, it didnt work out for me as my data is not continous(?).
The following code will do it (but horizontally):
library(scales) # for muted
ggplot(df22, aes(x = x, y = val)) +
geom_ribbon(aes(ymax = val, ymin = 0, group = type)) +
geom_col(aes(fill = val)) +
scale_fill_gradient2(position="bottom" , low = "blue", mid = muted("blue"), high = "red",
midpoint = median(df22$val))
If you want to make it vertically, you may flip the coordinates using coord_flip() upside down.
ggplot(df22, aes(x = val, y = x)) +
geom_ribbon(aes(ymax = val, ymin = 0)) +
coord_flip() +
geom_col(aes(fill = val)) +
scale_fill_gradient2(position="bottom" , low = "blue", mid = muted("blue"), high = "red",
midpoint = median(df22$val))
Or, if you want it to be horizontal with a vertical gradient (as you requested), you might need to go around it by playing with your data and using the geom_segment() instead of geom_ribbon(), like the following:
vals <- lapply(df22$val, function(y) seq(0, y, by = 0.001))
y <- unlist(vals)
mid <- rep(df22$x, lengths(vals))
d2 <- data.frame(x = mid - 1, xend = mid + 1, y = y, yend = y)
ggplot(data = d2, aes(x = x, xend = xend, y = y, yend = yend, color = y)) +
geom_segment(size = 1) +
scale_color_gradient2(low = "blue", mid = muted("blue"), high = "red", midpoint = median(d2$y))
This will give you the following:
Hope you find it helpful.
I want to plot a data set where the size of the points are proportional to the x-variable and have a regression line with a 95% prediction interval. The "sample" code I have written is as follows:
# Create random data and run regression
x <- rnorm(40)
y <- 0.5 * x + rnorm(40)
plot.dta <- data.frame(y, x)
mod <- lm(y ~ x, data = plot.dta)
# Create values for prediction interval
x.new <- data.frame(x = seq(-2.5, 2.5, length = 1000))
pred <- predict(mod,, newdata = x.new, interval = "prediction")
pred <- data.frame(cbind(x.new, pred))
# plot the data w/ regression line and prediction interval
p <- ggplot(pred, aes(x = x, y = upr)) +
geom_line(aes(y = lwr), color = "#666666", linetype = "dashed") +
geom_line(aes(y = upr), color = "#666666", linetype = "dashed") +
geom_line(aes(y = fit)) +
geom_point(data = plot.dta, aes(y = y, size = x))
p
This produces the following plot:
Obviously, the legend is not too helpful here. I would like to have one entry in the legend for the points, say, labeled "data", one grey, dashed line labeled "95% PI" and one entry with a black line labeled "Regression line."
As Hack-R alluded in the provided link, you can set the breaks and labels for scale_size() to make that legend more meaningful.
You can also construct a legend for all your geom_line() calls by adding linetype into your aes() and use a scale_linetype_manual() to set the values, breaks and labels.
ggplot(pred, aes(x = x, y = upr)) +
geom_line(aes(y = lwr, linetype = "dashed"), color = "#666666") +
geom_line(aes(y = upr, linetype = "dashed"), color = "#666666") +
geom_line(aes(y = fit, linetype = "solid")) +
geom_point(data = plot.dta, aes(y = y, size = x)) +
scale_size(labels = c("Eensy-weensy", "Teeny", "Small", "Medium", "Large")) +
scale_linetype_manual(values = c("dashed" = 2, "solid" = 1), labels = c("95% PI", "Regression Line"))
Is it possible to create great number of different kinds of "red" color. For better understanding I am expecting following, but instead of grey I desire to have "red" or "red-black".
mypalette <- rev(grey.colors(10000, start = 0.1, end = 0.5, gamma = 4))
plot(1:length(mypalette),1:length(mypalette), col=mypalette, pch=16)
The color packages I know have limited range of colors. Any idea will be appreciated.
If I understand what you want, try colorRampPalette. It returns a function that outputs the requested number of colours between the two you specified.
reds <- colorRampPalette(c("black","red"))
reds(5)
[1] "#000000" "#3F0000" "#7F0000" "#BF0000" "#FF0000"
Here are some ggplot alternatives
library(ggplot2)
df <- data.frame(x = rnorm(100), y = rnorm(100), z = rnorm(100), z2 = factor(1:5))
# colour set by continuous variable
ggplot(data = df, aes(x = x, y = y, colour = z)) +
geom_point() +
scale_colour_gradient(low = "red", high = "white")
library(RColorBrewer)
ggplot(data = df, aes(x = x, y = y, colour = z)) +
geom_point() +
scale_colour_gradientn(colours = brewer.pal(5, "Reds"))
# colour set by discrete variable
ggplot(data = df, aes(x = x, y = y, colour = z2)) +
geom_point() +
scale_colour_brewer(palette = "Reds")