Hello I recently asked a question about adding additional specific data points to a ggplot that were not included in a data set. Now I am trying to add error bars to those data points but nothing I do seems to work. This is what I have so far:
dat1 <- data.frame(x = "-/-", y = 2837.3333, Well = "0", errors = 334.02993877795)
dat2 <- data.frame(x = "+/+", y = 1911.33333, Well = "0", errors = 103.568010)
dat3 <- data.frame(x = "-/-", y = 137, Well = "8", errors = 158.745)
dat4 <- data.frame(x = "+/+", y = 259.6667, Well = "8", errors = 197.616630)
dat5 <- data.frame(x = "-/-", y = 95.666, Well = "16", errors = 93.0931)
dat6 <- data.frame(x = "+/+", y = 557.6667, Well = "16", errors = 133.82949)
dat7 <- data.frame(x = "-/-", y = 74.333, Well = "32", errors = 66.515)
dat8 <- data.frame(x = "+/+", y = 757.3333, Well = "32", errors = 43.98106)
ggplot(well_data4, aes(x = E2F4, y = Colonies, color = Clone, fill = E2F4)) +
geom_point(aes(color = Clone), size = 2) +
geom_point(data = dat1, aes(x=x, y=y), color = "black", inherit.aes = FALSE, size = 2)+
geom_errorbar(data = dat1, mapping = aes(x = x, ymin = y - errors, ymax = y + errors), width = 0.2, color = "black")+
geom_point(data = dat2, aes(x=x, y=y), color = "black", inherit.aes = FALSE, size = 2)+
geom_point(data = dat3, aes(x=x, y=y), color = "black", inherit.aes = FALSE, size = 2)+
geom_point(data = dat4, aes(x=x, y=y), color = "black", inherit.aes = FALSE, size = 2)+
geom_point(data = dat5, aes(x=x, y=y), color = "black", inherit.aes = FALSE, size = 2)+
geom_point(data = dat6, aes(x=x, y=y), color = "black", inherit.aes = FALSE, size = 2)+
geom_point(data = dat7, aes(x=x, y=y), color = "black", inherit.aes = FALSE, size = 2)+
geom_point(data = dat8, aes(x=x, y=y), color = "black", inherit.aes = FALSE, size = 2)+
all the different geom_points show up perfectly fine, but when I try to add geom_errorbar to one specific it says my aesthetics don't match up. I think it is trying to match it to my original data set well_data4 which is what my graph is made up of, but is there anyway to get it to just pertain to the new data set I made dat1 ? I would like to add them to all of them, hopefully to look like the picture I attached. Thanks so much.
Here's some of the data (i think??)
structure(list(E2F4 = c("+/+", "+/+", "+/+", "-/-", "-/-", "-/-
", "+/+", "+/+", "+/+", "-/-", "-/-", "-/-", "+/+", "+/+", "+/+",
"-/-", "-/-", "-/-", "-/-", "-/-"), Colonies = c(430, 43, 306,
17, 317, 77, 504, 710, 459, 16, 198, 73, 808, 735, 729, 7, 140,
76, 2864, 2769), Clone = c("Control", "Control", "Control", "2",
"7", "11", "Control", "Control", "Control", "2", "7", "11",
"Control", "Control", "Control", "2", "7", "11", "2", "2"), Well
= structure(c(2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 1L, 1L), .Label = c("0", "8", "16", "32"),
class = "factor")), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
We can do a few things to help here:
dat#
frames into a single frame, it makes the code much easier to work with (imo) and therefore easier to maintainx
and y
in it so that the aesthetics match those set in the initial call to ggplot
(E2F4
and Colonies
, respectively)Also, two assumptions:
errors
number should be added to and subtracted from the Colonies
count; andE2F4
that has a newline in your sample data, likely a paste-error, I'll clean it up.Further, I don't know how you got the x-axis the way you did there, I'll add facets and some theming to get it close to your pic.
well_data4 <- structure(list(E2F4 = c("+/+", "+/+", "+/+", "-/-", "-/-", "-/-", "+/+", "+/+", "+/+", "-/-", "-/-", "-/-", "+/+", "+/+", "+/+", "-/-", "-/-", "-/-", "-/-", "-/-"), Colonies = c(430, 43, 306, 17, 317, 77, 504, 710, 459, 16, 198, 73, 808, 735, 729, 7, 140, 76, 2864, 2769), Clone = c("Control", "Control", "Control", "2", "7", "11", "Control", "Control", "Control", "2", "7", "11", "Control", "Control", "Control", "2", "7", "11", "2", "2"), Well = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L), levels = c("0", "8", "16", "32"), class = "factor")), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))
# combine all dat# into a single frame
library(dplyr) # other ways exist if you don't want to use dplyr
datX <- bind_rows(dat1, dat2, dat3, dat4, dat5, dat6, dat7, dat8) %>%
rename(E2F4 = x, Colonies = y) %>%
mutate(
ymin = Colonies - errors,
ymax = Colonies + errors,
Well = factor(Well, levels = levels(well_data4$Well))
)
ggplot(well_data4, aes(x = E2F4, y = Colonies, color = Clone, fill = E2F4)) +
facet_grid(~ Well, switch = "both") +
geom_point(aes(color = Clone), size = 2) +
geom_errorbar(
aes(x = E2F4, ymin = ymin, ymax = ymax),
data = datX, inherit.aes = FALSE) +
labs(title = "HT29 Colony Survival", x = "Concentration of SN-38 (nM)") +
theme(
strip.background = element_blank(), # normally they are gray bg, this removes gray
strip.placement = "outside", # normally b/w axis labels and ata, this shifts below
panel.spacing = unit(0, "pt") # normally clearly framed/spaced, this makes it look like one canvas
)