I am creating a point plot and I wanted to add a bar to the bottom of a point plot. I can't seem to find out how to do this in the ggplot documentation. I was hoping to add at bar the spanned the entire x-axis with a set y-axis value. Here is an example of the data I am working with
d=data.frame(drink=c("coffee","tea","water"), mean=c(5,6,9), lower=c(4.5,5.6,8.7), upper=c(5.5,6.3,9.5))
and here is the code I am using
ggplot() +
geom_errorbar(data=d, mapping=aes(x=drink, ymin=upper, ymax=lower), width=0.2, size=1, color="blue") +
geom_point(data=d, mapping=aes(x=drink, y=mean), size=4, shape=21, fill="white") +
scale_y_continuous(n.breaks = 10) + ylim(0, 12)
Here is what the plot currently looks like
and this is what I want to add
The annotate() function allows you to directly specify a layer without intermediate data.frame. In ggplot2, the -Inf/Inf values for continuous variables indicate to place something at the extremes.
library(ggplot2)
d=data.frame(drink=c("coffee","tea","water"),
mean=c(5,6,9),
lower=c(4.5,5.6,8.7),
upper=c(5.5,6.3,9.5))
ggplot(d) +
geom_errorbar(
mapping=aes(x=drink, ymin=upper, ymax=lower),
width=0.2, size=1, color="blue") +
geom_point(
mapping=aes(x=drink, y=mean),
size=4, shape=21, fill="white") +
scale_y_continuous(n.breaks = 10, limits = c(0, 12)) +
annotate("rect", xmin = -Inf, xmax = Inf,
ymin = -Inf, ymax = 1, fill = "black")
Created on 2021-09-13 by the reprex package (v2.0.1)
Related
I had to flip the axis of my line, but still need the geom_area to be under the curve. However I cannot figure out how to do so.
This is the line of code I tried
ggplot(PalmBeachWell, aes(x=Date, y=Depth.to.Water.Below.Land.Surface.in.ft.)) +
geom_area(position= "identity", fill='lightblue') +
theme_classic() +
geom_line(color="blue") +
scale_y_reverse()
and here is what i got
One option would be to use a geom_ribbon to fill the area above the curve which after applying scale_y_reverse will result in a fill under the curve.
Using some fake example data based on the ggplot2::economics dataset:
library(ggplot2)
PalmBeachWell <- economics[c("date", "psavert")]
names(PalmBeachWell) <- c("Date", "Depth.to.Water.Below.Land.Surface.in.ft.")
ggplot(PalmBeachWell, aes(x = Date, y = Depth.to.Water.Below.Land.Surface.in.ft.)) +
geom_ribbon(aes(ymin = Depth.to.Water.Below.Land.Surface.in.ft., ymax = Inf),
fill = "lightblue"
) +
geom_line(color = "blue") +
scale_y_reverse() +
theme_classic()
This question already has answers here:
geom_bar bars not displaying when specifying ylim
(4 answers)
R - ggplot2: geom_area loses its fill if limits are defined to max and min values from a data.frame
(1 answer)
Closed 6 days ago.
I am using ggplot2 to draw a line chart with the following R codes:
library(ggplot2)
X <-c(0, 1, 2, 3, 4, 5)
Y <-c(1, 2, 3, 2, 1, 2)
df <- data.frame(X=X, Y=Y)
ggplot(data=df, mapping=aes(x=X, y=Y))+
geom_line(colour="violet", size=3, linetype=2)+
geom_point(shape=23, fill="blue", color="darkred", size=3)
and the output is fine like this:
However, I would like to color the under density curve and I change the codes like these:
ggplot(data=df, mapping=aes(x=X, y=Y))+geom_line(colour="violet", size=3, linetype=2)+geom_point(shape=23, fill="blue", color="darkred", size=3)+geom_area("darkseagreen1")
and I got output like this:
This is not ideal so I try to set the tick marks interval using the following code:
ggplot(data=df, mapping=aes(x=X, y=Y))+geom_line(colour="violet", size=3, linetype=2)+geom_point(shape=23, fill="blue", color="darkred", size=3)+geom_area(fill = "darkseagreen1")+scale_y_continuous(breaks = seq(71.4, 72.9, by=0.3), limits=c(71.4,72.7))
It becomes normal but the color is gone like this:
Can anyone help me with this error? Thank you.
You can use geom_area() if you set your limits using coord_cartesian() (see e.g. https://stackoverflow.com/a/3606798/12957340 for more details).
For example:
library(ggplot2)
X <-c(0, 1, 2, 3, 4, 5)
Y <-c(1, 2, 3, 2, 1, 2)
df <- data.frame(X=X, Y=Y)
# starting plot
ggplot(data=df, mapping=aes(x=X, y=Y)) +
geom_line(colour="violet", size=3, linetype=2) +
geom_point(shape=23, fill="blue", color="darkred", size=3)
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
ggsave("example.pdf")
#> Saving 7 x 5 in image
# with geom_area()
ggplot(data=df, mapping=aes(x=X, y=Y)) +
geom_line(colour="violet", size=3, linetype=2) +
geom_point(shape=23, fill="blue", color="darkred", size=3) +
geom_area(fill = "darkseagreen1")
ggsave("example2.pdf")
#> Saving 7 x 5 in image
# add y-axis limits (the problem)
ggplot(data=df, mapping=aes(x=X, y=Y)) +
geom_line(colour="violet", size=3, linetype=2) +
geom_point(shape=23, fill="blue", color="darkred", size=3) +
geom_area(fill = "darkseagreen1") +
scale_y_continuous(breaks = seq(0, 3, by=0.3), limits = c(1, 2))
ggsave("example3.pdf")
#> Saving 7 x 5 in image
# add y-axis limits without losing data (a solution)
ggplot(data=df, mapping=aes(x=X, y=Y)) +
geom_line(colour="violet", size=3, linetype=2) +
geom_point(shape=23, fill="blue", color="darkred", size=3) +
geom_area(fill = "darkseagreen1") +
scale_y_continuous(breaks = seq(0, 3, by=0.3)) +
coord_cartesian(ylim = c(1,2))
ggsave("example4.pdf")
#> Saving 7 x 5 in image
Created on 2023-02-08 with reprex v2.0.2
You want to use geom_ribbon() instead of geom_area(). Try this code instead:
ggplot(data = df, mapping = aes(x = X, y = Y)) +
geom_line(colour = "violet", size = 3, linetype = 2) +
geom_point(shape = 23, fill = "blue", color = "darkred", size = 3) +
scale_y_continuous(breaks = seq(71.4, 72.9, by = 0.3), limits = c(71.4, 72.7)) +
geom_ribbon(aes(ymin = 71.4, ymax = Y), fill = "darkseagreen1")
I have made a bar graph and would like it to have a log-scaled y axis. However, when I try to add the code for this, it for some reason inverses most of the bars... Can anyone explain why this may be and how to remedy this?
The code I use is as follows:
Graph1 <- ggplot(Data, aes(x=Temp, y=Mean, fill=Exposure)) +
geom_bar(position=position_dodge(), stat='identity', color="black",) +
xlab("Temperature (°C)") +
ylab("Cd concentration (µg/g)") +
facet_wrap(.~Day, scales="free_y", labeller=labeller(Day=supp.labsDAY), nrow = 1, ncol = 4)+
geom_errorbar( aes(x=Temp, ymin=Mean-SEM, ymax=Mean+SEM), width = 0.2, position=position_dodge(.9))
Graph1+ scale_y_log10()
I have a grid of plots, all with the same y and x-axis scale. The plots represent time in the x-axe and mean values in the y-axe with their standard errors. My problem is that some errorbars are not entirely within the plot margins, and I wonder if there is some way to represent the part of the errorlines that are within the plot margins. Below I give a fake example and code to play with:
df <- data.frame(time=seq(-15,15,1),
mean=c(0.49,0.5,0.53,0.55,0.57,0.59,0.61,0.63,0.65,0.67,0.69,0.71,0.73,0.75,0.77,0.79,0.77,0.75,0.73,0.71,0.69,0.67,0.65,0.63,0.61,0.59,0.57,0.55,0.53,0.51,0.49),
sd=c(0.09,0.087,0.082,0.08,0.023,0.011,0.010,0.009,0.008,0.007,0.006,0.005,0.004,0.003,0.002,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.010,0.011,0.023,0.08,0.084,0.087,0.09))
Plot <- ggplot(df, aes(x=time, y=mean)) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.3) +
geom_point(size=1) +
geom_line () +
theme_bw() +
scale_y_continuous(limits = c(0.49, 0.85), breaks = c(0.5, 0.65,0.8))
Plot
You need to set coord_cartesian limits rather than scale_y_continuous limits:
ggplot(df, aes(x=time, y=mean)) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.3) +
geom_point(size=1) +
geom_line () +
theme_bw() +
scale_y_continuous(breaks = c(0.5, 0.65,0.8)) +
coord_cartesian(ylim = c(0.49, 0.85))
I created a plot using 2 separate data sets so that I could create different errorbars. The first data set has error bars that go down only whereas the second data set has error bars that go up only. This prevents unnecessary overlap in the plot. I also used a compound shape for one of the groups.
I want to create a legend based on these shapes (not a colour), but I can't seem to figure it out. Here is the plot code.
p <-ggplot()
p + geom_point(data=df.figure.1a, aes(x=Hour, y=Mean), shape=5, size=4) +
geom_point(data=df.figure.1a, aes(x=Hour, y=Mean), shape=18, size=3) +
geom_errorbar(data=df.figure.1a, aes(x=Hour, y=Mean, ymin = Mean - SD, ymax = Mean), size=0.7, width = 0.4) +
geom_point(data=df.figure.1b, aes(x=Hour, y=Mean), shape=17, size=4) +
geom_errorbar(data=df.figure.1b, aes(x=Hour, y=Mean, ymin = Mean, ymax = Mean + SD), size=0.7, width = 0.4)