Reputation: 205
I currently have the figure shown below:
However, two vertical lines at the end of (horizontal) line is too long for me. I tried to change the number for both "height" and "width", but changing the number did not work.
Can anyone make the vertical lines shorter?
Many thanks in advance!
*** Here is the code that can reproduce the plot.
my_data <- data.frame(mean = c(0.04, 0.015, -0.04),
stdev = c(0.019, 0.019, 0.02),
Type = factor(c("A",
"B",
"C")))
# points at which to evaluate the Gaussian densities
x <- seq(-0.1, 0.1, by = 0.001)
# build list of Gaussian density vectors based on means and standard deviations
pdfs <- mapply(dnorm, mean = my_data$mean, sd = my_data$stdev, MoreArgs = list(x = x),
SIMPLIFY = FALSE)
# add group names
names(pdfs) <- my_data$Type
# convert list to dataframe
pdfs <- do.call(cbind.data.frame, pdfs)
# Summary stat
x.com <- data.frame(pdfs, x)
sum_stat <- matrix(NA, ncol = 4, nrow = 3)
for (i in 1:3){
sum_stat[i,1] <- qnorm(0.025 , mean=my_data[i,1], sd=my_data[i,2])
sum_stat[i,2] <- x.com[,4][x.com[,i]==max(x.com[,i])]
sum_stat[i,3] <- qnorm(0.975, mean=my_data[i,1], sd=my_data[i,2])
}
sum_stat <- data.frame(sum_stat)
sum_stat[,4] <- colnames(x.com)[1:3]
sum_stat[,4] <- as.factor(sum_stat[,4])
colnames(sum_stat) <- c("left", "max", "right", "Type")
colnames(sum_stat)[4] <- "Type"
# convert dataframe to tall format
# library(tidyr)
pdfs$x <- x
tall_df <- gather(pdfs, Type, density, -x)
tall_df$Type
tt <- transform(tall_df,
Type=factor(Type,levels=c("A",
"B",
"C")))
ggplot(tt) +
# geom_line(aes(x = x, y = density, color = Type)) +
geom_point(data = sum_stat,
aes(x = max, y = 0, alpha=0.4),
show.legend = FALSE, color = "black") +
geom_errorbarh(data = sum_stat,
aes(xmin = left, xmax = right, y = 0, alpha=0.4),
height = 0.01, show.legend = FALSE, color = "black") +
facet_wrap(~ Type, ncol = 1) +
theme(strip.background = element_blank(),
strip.text.x = element_blank())
I am updating the post, because I cannot show you what my Console says. I cleared the the global environment, and then run the code that I attached here. I do not have any problem running it. In my case, the head and tail of tt look like this:
> tt <- transform(tall_df,
+ Type=factor(Type,levels=c("A",
+ "B",
+ "C")))
> head(tt)
x Type density
1 -0.100 A 3.407570e-11
2 -0.099 A 5.014963e-11
3 -0.098 A 7.360166e-11
4 -0.097 A 1.077220e-10
5 -0.096 A 1.572238e-10
6 -0.095 A 2.288385e-10
> tail(tt)
x Type density
598 0.095 C 2.547469e-09
599 0.096 C 1.815481e-09
600 0.097 C 1.290591e-09
601 0.098 C 9.151661e-10
602 0.099 C 6.473296e-10
603 0.100 C 4.567360e-10
One thing that I wanted to mention is: I would like to keep the range of y, but just make the vertical lines at the end of errorbar shorter.
Upvotes: 2
Views: 482
Reputation: 125028
Fix the scale of your y-axis, e.g. add + scale_y_continuous(limits = c(-.2, .2))
. Then you can adjust the length of the vertical lines with height
. Otherwise ggplot2
always readjusts the scale of the y-axis to the chosen height
so that it appears as if height
has no impact at all.
ggplot(tt) +
# geom_line(aes(x = x, y = density, color = Type)) +
geom_point(data = sum_stat,
aes(x = max, y = 0, alpha=0.4),
show.legend = FALSE, color = "black") +
geom_errorbarh(data = sum_stat,
aes(xmin = left, xmax = right, y = 0, alpha=0.4),
height = 0.01, show.legend = FALSE, color = "black") +
scale_y_continuous(limits = c(-.2, .2)) +
facet_wrap(~ Type, ncol = 1) +
theme(strip.background = element_blank(),
strip.text.x = element_blank())
Created on 2020-03-11 by the reprex package (v0.3.0)
Upvotes: 2