Create a scatterplot with errorbars with ggplot - r

I'm new to R and I was trying to make a scatterplot and add some error bars to the points but I don't know how to.
p <- ggplot(data = success, aes(x = Percent, y = No.Fleas)) +
geom_point(col = "blue") +
geom_smooth(method = lm, se = FALSE) +
geom_errorbar(x = x, ymin = 9.997, ymax = 52.21)
p

Related

How to color the area between two geom_smooth lines?

I have 3 columns in a data frame from which I want to create a visualisation with geom_smooth() :
ggplot(my_data_frame) +
aes(x = fin_enquete,
y = intentions,
colour = candidat) +
geom_point(alpha = 1/6,
shape = "circle",
size = .5L) +
geom_smooth(mapping = aes(y = erreur_inf),
size = .5L,
span = .42,
se = F) +
geom_smooth(mapping = aes(y = erreur_sup),
size = .5L,
span = .42,
se = F) +
geom_smooth(method = "loess",
size = 1.5L,
span = .42,
se = F) +
labs(x = "Date de fin d'enquĂȘte",
y = "Pourcentage d'intentions de vote") +
theme_minimal() +
theme(text = element_text(family = "DIN Pro")) +
coord_cartesian(expand = F) +
easy_remove_legend()
3 lines with geom_smooth
I would like to color the area between the upper and the lower line. I know the geom_ribbon() function but I am not sure I can use it in this situation.
Does anybody have a solution?
Have a nice day!
You could use geom_ribbon and calculate the loess model yourself within the geom_ribbon call?
Toy random data
dat <- data.frame(x=1:100, y=runif(100), y2=runif(100)+1, y3=runif(100)+2)
Now suppose we want a smoothed ribbon between y and y3, with y2 drawn as a line between them:
ggplot( dat , aes(x, y2)) +
geom_ribbon(aes(ymin=predict(loess(y~x)),
ymax=predict(loess(y3~x))), alpha=0.3) +
geom_smooth(se=F)
You could use lapply() smooth to calculate the range of df values such as (5,11,13) to calculate the smooths and plot only the two edges of the se.
Sample code:
library(ggplot2)
ggplot(data = mtcars,
mapping = aes(x = wt,
y = mpg)) +
geom_point(size = 2)+
lapply(c(5,11, 13), function (i) {
geom_smooth(
data = ~ cbind(., facet_plots = i),
method = lm,
se=F,
formula = y ~ splines::bs(x, i)
)
})+
#facet_wrap(vars(facet_plots))
geom_ribbon(
stat = "smooth",
method = "loess",
se = TRUE,
alpha = 0, # or, use fill = NA
colour = "black",
linetype = "dotted")+
theme_minimal()
Plot:

Confidence interval graph in ggplot: how to make it more clearer?

I have continuous data (spectra) and I would like to determine the significance of the differences. I tried to do it using confidence intervals in r, ggplot.
Here is the code:
ggplot(df, aes(x = df$wn, y = df$value)) +
geom_line(aes(x = df$wn, y = df$value, colour = group)) +
geom_ribbon(aes(x = df$wn, ymin = df$lower, ymax = df$upper, fill = group))
I have around 15 spectra and the graph looks indistinguishable:
How could I make it clearer?

Color in ggplot, continuous value supplied to discrete scale

I am plotting points over a heat map produced in ggplot2. delta is a data frame containing points to be plotted over heat map. The variable plt stores the ggplot image.
The heat map is produced by the code from this link (could not post here because of text limitation). Also, the reproducible code for all required data frames is in the link.
https://justpaste.it/65iu7
Now to superimpose points over the heat map, I used the code below:
plt0 <- plt + geom_point(data = delta, aes(x = dP/100, y = dT, z = NULL, color = rcp, shape = future))
plt0
It gives error:
Error: Continuous value supplied to discrete scale.
If I remove color = future from the above code, it works. But I need to have color coded points as this code produces:
ggplot()+geom_point(data = delta, aes(x = dP/100, y = dT, z = NULL, color = rcp, shape = future))
What is producing this error and how can I solve it?
Is it ok like this?
plt <- ggplot() + geom_tile(data=new.data, aes(x = hh/100, y = tt, fill=W)) +
geom_contour(data=new.data, bins = 10,
aes(x = hh/100, y = tt, #color = ..level..,
z = floor(W)),
show.legend = FALSE) +
ylab("Change in temperature in degree Celsius") +
xlab("percentage change in precipitation") +
scale_fill_gradientn(name = "W (in m3/year)",
values = scales::rescale(quantile(new.data$W)),
limits = c(min(new.data$W),max(new.data$W)),
breaks = seq(round(min(new.data$W)/1000000)*1000000,
round(max(new.data$W)/1000000)*1000000,
(round(max(new.data$W)/1000000)*1000000-round(min(new.data$W)/1000000)*1000000)/3),
colors = rainbow(7), guide = "colorbar") +
scale_x_continuous(breaks = seq(-0.3,0.3, 0.1), label = scales::percent) +
scale_y_continuous(breaks = seq(-1, 6, 1)) +
ggtitle("Variation of average annual sediment production with \n temperature and precipitation")+
guides(fill = guide_colorbar(barwidth = 0.5, barheight = 10))
plt
plt +
geom_point(data = delta, aes(x = dP/100, y = dT,
color = rcp, shape = future))
I did not change the code except for the first line. Instead of :
plt<-ggplot(new.data, aes(x = hh/100, y = tt, z = floor(W))) + geom_tile(aes(fill = W)) + ...
Used
plt<-ggplot()+ geom_tile(new.data, aes(x = hh/100, y = tt, fill = W)) + ...
This is to ascertain that we call empty ggplot and then add geom_tile with new.data and since the ggplot has not been assigned any data as default, we can later on add delta for geom_point. The output is as follows:

How to pass data and aesthetics across ggplot2 functions

I have to create a figure with ggplot2 that has roughly this structure:
p <- ggplot() +
geom_rect(data = regions,aes(xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf),
fill = "yellow",alpha = 0.1) +
geom_line(data = data, aes(x=dt, y = y, color = case)) +
geom_point(data = data, aes(x=dt, y = y, color = case)) +
facet_grid(groups ~ ., scale="free_y")
geom_vline(x=as.numeric(dates_start), color = "orange3",linetype="dashed") +
geom_vline(x=as.numeric(dates_end), color = "orange3",linetype="dashed")
p
Is there anyway I can avoid having to pass all the details in geom_point? since they are the same as the one used in geom_line?

Drawing only boundaries of stat_smooth in ggplot2

When using stat_smooth() with geom_point is there a way to remove the shaded fit region, but only draw its outer bounds? I know I can remove the shaded region with something like:
geom_point(aes(x=x, y=y)) + geom_stat(aes(x=x, y=y), alpha=0)
but how can I make the outer bounds of it (outer curves) still visible as faint black lines?
You can also use geom_ribbon with fill = NA.
gg <- ggplot(mtcars, aes(qsec, wt))+
geom_point() +
stat_smooth( alpha=0,method='loess')
rib_data <- ggplot_build(gg)$data[[2]]
ggplot(mtcars)+
stat_smooth(aes(qsec, wt), alpha=0,method='loess')+
geom_point(aes(qsec, wt)) +
geom_ribbon(data=rib_data,aes(x=x,ymin=ymin,ymax=ymax,col='blue'),
fill=NA,linetype=1)
...and if for some reason you don't want the vertical bars, you can just use two geom_line layers:
ggplot(mtcars)+
stat_smooth(aes(qsec, wt), alpha=0,method='loess')+
geom_point(aes(qsec, wt)) +
geom_line(data = rib_data,aes(x = x,y = ymax)) +
geom_line(data = rib_data,aes(x = x,y = ymin))
There are most likely easier ways, but you may try this as a start. I grab data for the confidence interval with ggbuild, which I then use in geom_line
# create a ggplot object with a linear smoother and a CI
library(ggplot2)
gg <- ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "lm")
gg
# grab the data from the plot object
gg_data <- ggplot_build(gg)
str(gg_data)
head(gg_data$data[[2]])
gg2 <- gg_data$data[[2]]
# plot with 'CI-lines' and the shaded confidence area
ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = TRUE, size = 1) +
geom_line(data = gg2, aes(x = x, y = ymin), size = 0.02) +
geom_line(data = gg2, aes(x = x, y = ymax), size = 0.02)
# plot with 'CI-lines' but without confidence area
ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, size = 1) +
geom_line(data = gg2, aes(x = x, y = ymin), size = 0.02) +
geom_line(data = gg2, aes(x = x, y = ymax), size = 0.02)

Resources