Reputation: 726
When I run the code below I get the error Error in FUN(X[[i]], ...) : object 'typoft' not found
. I have two geom_texts, because I am laying more text on top of the plot. Can anyone help me here and let me know why there is an error? I need to be able to use the second geom_text.
Please share your thoughts and thank you in advance.
FYI labdat is a dataframe that I am using to plot certain text.
CODE:
x = c(-.87,-.87,-.87,-.29,-.29,-.29)
y = c(-.75,-.75,-.75,-.18,-.18,-.18)
label = c(.1,0,.3,.2,.1,1)
trade = c("S","M","L","S","M","L")
labdat <- data.frame(x=x,y=y,label=label,trade = trade)
ggplot(gb, aes(x = y, y = y1, fill = typeoft)) +
geom_bin2d(bins = 10, aes(alpha = ..count..)) +
scale_fill_manual(values = c("blue","black")) +
geom_text(bins = 10, stat = "bin2d", aes(label = round(100*..density..,1)),
size = 2,check_overlap = TRUE) +
facet_wrap(~type, nrow = 1) +
geom_abline(slope=1, intercept=0) +
scale_alpha_continuous(range = c(.05,1)) +
theme(panel.background = element_rect(fill = "white")) +
theme(legend.position = "none") +
geom_text(data = labdat,aes(x = x,y = y,label = label))
DATA(Sample):
gb <- structure(list(type = c("M", "M", "S", "S", "M", "S", "S", "S",
"M", "S", "M", "M", "M", "S", "S", "M", "S", "S", "S", "M", "M",
"S", "M", "S", "S", "M", "M", "M", "S", "M", "S", "M", "S", "S",
"S", "M", "S", "M", "M", "S", "S", "S", "S", "M", "S", "M", "M",
"S", "M", "M", "S", "M", "M", "S", "M", "S", "S", "S", "S", "M",
"S", "S", "S"), y = c(0.0173, 0.0358, 0.0203, 0.0012, 0.0219,
0.0137, 0.0345, 0.0267, 0.0135, 0.0254, 0.0179, 0.0313, 0.0268,
0.0294, 0, 0.0365, 0.0144, 0.0252, 0.0217, 0.0182, 0.0298, 0.0135,
0.0204, 0.0152, 0.0236, 0.032, 0.0426, 0.0233, 0.0136, 0.011,
0.0212, 0.0247, 0.0374, 0.0312, 0.0314, 0.0162, 0.0011, 0.0213,
0, 0.0231, 0.0121, 0.0304, 0.0059, 0.0371, 0.0209, 0.028, 0.0166,
0.0224, 0.0257, 0.0262, 0.0209, 0.0139, 0.0187, 0.0125, 0.0335,
0.0301, 0, 0.0176, 0.0269, 0.0184, 0.0204, 0.0165, 0.0158), y1 = c(0.0318,
0.0227, 0.0412, 0.0012, 0.0187, 0.0166, 0.0317, 0.0272, 0.027,
0.0259, 0.0144, 0.0276, 0.0267, 0.035, 0.0218, 0.0183, 0.0139,
0.0223, 0.0227, 0.0275, 0.0259, 0.0317, 0.026, 0.0228, 0.0258,
0.036, 0.0504, 0.0165, 0.0107, 0.0012, 0.0258, 0.0273, 0.0277,
0.0283, 0.0327, 0.0331, 0.0224, 0.0248, 0.0245, 0.0213, 0.0315,
0.03, 0.0269, 0.0389, 0.0302, 0.025, 0.0291, 0.0213, 0.0335,
0.027, 0.0269, 0.0142, 0.0286, 0.0103, 0.0373, 0.034, 0.0285,
0.0103, 0.0278, 0.0188, 0.0062, 0.028, 0.0146), typeoft = c("e",
"c", "e", "c", "c", "e", "c", "e", "e", "e", "c", "c", "c", "e",
"e", "c", "c", "c", "e", "e", "c", "e", "e", "e", "e", "e", "e",
"c", "c", "c", "e", "e", "c", "c", "e", "e", "e", "e", "e", "c",
"e", "c", "e", "e", "e", "c", "e", "c", "e", "e", "e", "e", "e",
"c", "e", "e", "e", "c", "e", "e", "c", "e", "c")), class = "data.frame", row.names = c(NA,
-63L))
Upvotes: 3
Views: 1688
Reputation: 12155
Your problem is coming from the fact that when you define an aes
in the ggplot()
call, those settings are inherited by all geom_*
that come after it if they're not overwritten.
We can see this clearly if we reduce your problem to it's minimal form. We can replicate your problem with the last geom_text
alone:
ggplot(gb, aes(x = y, y = y1, fill = typeoft)) +
geom_text(data = labdat, aes(x = x,y = y,label = label))
Error in FUN(X[[i]], ...) : object 'typeoft' not found
This is happening because when you define the aes
in ggplot
, you set a value for x
, y
, and fill
. When you call aes
in geom_text
, the values for x
and y
are overwritten, but the value for fill is not. So the aes
for geom_text
actually looks like this: aes(x = x, y = y, label = label, fill = typeoft)
. But since you don't have a variable named typeoft
in the object labdat
, it returns an error.
We can stop this behavior by giving your geom_text
the argument inherit.aes = FALSE
:
# This works!
ggplot(gb, aes(x = y, y = y1, fill = typeoft)) +
geom_text(data = labdat,aes(x = x, y = y, label = label), inherit.aes = FALSE)
Now, the aes
for geom_text
will only include what you tell it to have.
Upvotes: 7