Scatterplot: Error in FUN(X[[i]], ...) : object 'Group' not found - r

I'm trying to plot some data using ggplot and I'm having some problems with the significant lines and asterisk.
This is the code I am using:
p <- ggplot(Hematoxilin_tumor_necrosis, aes(x=total, y=necro, colour = Group))+
labs(y="Necrotic area",x="Total area")+
theme_minimal()
path = data.frame(x=c(78,79,79,78),y=c(22,22,34,34))
p + geom_point(size=0.7)+
geom_smooth(method=lm, se = F, size=0.8) +
scale_color_manual(values=c("#999999","#333333"))+
#Adding asterisks
geom_path(data = path, aes(x = x,y = y)) +
annotate("text",x = 80, y = 27, label="*", cex=7)
Which gives me the following error:
Error in FUN(X[[i]], ...) : object 'Group' not found
I know that the problem is in the geom_path(data = path, aes(x = x,y = y)) but I am kind of lost. I am new in ggplot so I expect some simple problem.
Any advice?

aesthetics are inherited by default. The geom_path is trying to look for the Group variable on the path dataset to get the color. You should use inherit.aes = FALSE on the geom_path:
geom_path(data = path, aes(x = x,y = y), inherit.aes = FALSE )

Related

the error codes from ggplot while using geom_tex at the same time

I have two df that I used for plotting.
df <- data.frame(x2=rnorm(100),y2=rnorm(100)) %>% mutate(Class=case_when(x2>0 ~1,
TRUE ~0))
df2<- structure(list(xpos = -2.5, ypos = -2, annotateText = structure(1L, .Label = "Text", class = "factor"),
hjustvar = 0, vjustvar = 0), class = "data.frame", row.names = c(NA,
-1L))
My plotting codes are:
ggplot(df, aes(x2, y2, colour=Class)) + geom_point()+
geom_text(data = df2, aes(x=xpos,y=ypos,hjust=hjustvar,
vjust=vjustvar,label=(annotateText)))
When I run it, I got error codes: Error in FUN(X[[i]], ...) : object 'Class' not found. If I removed colour=Class then my codes run. What did I do wrong? I really want to have the color difference for different groups. Any suggestion?
The color = Class can be moved into geom_point as the subsequent layers can check for inheritance from the top layer as by default inherit.aes = TRUE in those layers
library(ggplot2)
ggplot(df, aes(x2, y2)) +
geom_point(aes(color = Class)) +
geom_text(data = df2, aes(x = xpos,y = ypos,hjust = hjustvar,
vjust = vjustvar,label= annotateText))
Your second geometry is inheriting the aesthetics you specified in your initial call to ggplot()
You need to specify inherit.aes = FALSE in your call to geom_text()
Ggplot geometries inherit the aesthetics from the initial call to ggplot(). Your aesthetic colour = Class that you specify in your call to ggplot() is inherited by geom_point(), resulting in the desired behaviour. Your error occurs because geom_text() also inherits this colour = Class, and subsequently crashes because Class isn't defined in the data it's using - df2.
You can fix this by specifying in geom_text() that you don't want to inherit any aesthetics from the initial call:
ggplot(df, aes(x2, y2, colour=Class)) + geom_point() +
geom_text(data = df2, aes(x=xpos,y=ypos,hjust=hjustvar,
vjust=vjustvar,label=(annotateText)),
inherit.aes = FALSE)

Error plotting in a second series in ggplot2

I want to plot a scatter plot with two different series:
Ej4 = read.xlsx("C:\\Users\\Ulia\\Downloads\\Parte 5.xlsx", sheet = 4)
fix(Ej4)
graf = ggplot(Ej4, aes(x1,x1)) + geom_point(alpha = 0.8)
graf
variable = data.frame(y = c(6,9,6,17,12), y2= c(6,9,6,17,12))
variable
grafica2 = graf + geom_point() +
geom_point(data = variable, colour ="blue")
grafica2
But R shows this error:
Error in FUN(X[[i]], ...) : object 'x' not found
There's no problem plotting graf, but I can't understand why R tells me there's is an error with grafica2
PD: Ej4 is a dataframe with numerical variables, with the exactly same size as 'variable'
There's another way:
ggplot() + geom_point(data = Ej4, aes(x1,y1)) + geom_point(data = variable, aes(y,y2))

How do I plot flights data from nycflights13 so that x=airlines, y = dep_delay?

When I try to plot x = airlines, y = dep_delay, I get an error message.
My hypothesis is that delays are caused by the inefficiency of the airlines above and beyond any other factors. I simply want to plot these two variables, I get an error message.
I try this code but it doesn't work.
ggplot(data = flights, mapping = aes(x = airlines, y= dep_delay)) +
geom_point() +
geom_smooth(se = FALSE)
ggplot(data = flights, mapping = aes(x = airlines, y = dep_delay)) +
geom_point() +
geom_smooth(se = FALSE)
Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (336776): x
You are using two "+" instead of a single "+" sign

geom_text / geom_label with the bquote function

My data :
dat <- data_frame(x = c(1,2,3,4,5,6), y = c(2,2,2,6,2,2))
I wish to display this expression beside the point (x=4,y=6) :
expression <- bquote(paste(frac(a[z], b[z]), " = ", .(dat[which.max(dat$y),"y"] %>% as.numeric())))
But, when I am using this expression with ggplot :
ggplot() +
geom_point(data = dat, aes(x = x, y = y)) +
geom_label(data = dat[which.max(dat$y),], aes(x = x, y = y, label = expression))
I get this error message :
Error: Aesthetics must be either length 1 or the same as the data (1): label
You could use the following code (keeping your definitions of the data and the expression):
Not related to your question, but: it is always better to define aesthetics in the ggplot-call and get it reused in the subsequent function calls. If needed, you may override the definitions, like done below in geom_label
ggplot(data = dat, aes(x = x, y = y)) +
geom_point() +
geom_label(data = dat[4,], label = deparse(expression), parse = TRUE,
hjust = 0, nudge_x = .1)
hjust and nudge_x are used to position the label relative to the point. One could argue to use nudge_y as well to get the whole label in the picture.
yielding this plot:
Please let me know whether this is what you want.

Object not being found when using ggplot2 in R

While creating a shot chart in R, I've been using some open source stuff from Todd W. Schneider's BallR court design (https://github.com/toddwschneider/ballr/blob/master/plot_court.R)
along with another Stack Overflow post on how to create percentages within hexbins (How to replicate a scatterplot with a hexbin plot in R?).
Both sources have been really helpful for me.
When I run the following lines of code, I get a solid hexbin plot of percent made for shots for the different locations on the court:
ggplot(shots_df, aes(x = location_y-25, y = location_x, z = made_flag)) +
stat_summary_hex(fun = mean, alpha = 0.8, bins = 30) +
scale_fill_gradientn(colors = my_colors(7), labels = percent_format(),
name = "Percent Made")
However, when I include the BallR court design code snippet, which is shown below:
ggplot(shots_df, aes(x=location_y-25,y=location_x,z=made_flag)) +
stat_summary_hex(fun = mean, alpha = 0.8, bins = 30) +
scale_fill_gradientn(colors = my_colors(7), labels=percent_format(),
name="Percent Made") +
geom_path(data = court_points,
aes(x = x, y = y, group = desc, linetype = dash),
color = "#000004") +
scale_linetype_manual(values = c("solid", "longdash"), guide = FALSE) +
coord_fixed(ylim = c(0, 35), xlim = c(-25, 25)) +
theme_court(base_size = 22)
I get the error: Error in eval(expr, envir, enclos) : object 'made_flag' not found, even though that the made_flag is 100% in the data frame, shots_df, and worked in the original iteration. I am lost on how to fix this problem.
I believe your problem lies in the geom_path() layer. Try this tweek:
geom_path(data = court_points, aes(x = x, y = y, z = NULL, group = desc, linetype = dash))
Because you set the z aesthetic at the top, it is still inheriting in geom_path() even though you are on a different data source. You have to manually overwrite this with z = NULL.

Resources