Error plotting in a second series in ggplot2 - r

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))

Related

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

ggplot2: Violin Plot with Stat="Identity"

I'm trying to use ggplot to create a violin plot where instead of the widths of the violins being controlled by a density function, they directly represent the count of relevant elements.
I think this can be accomplished through setting geom_violin(stat="identity"), but R then complains
> ggplot(allData, aes(x = tool, y = length)) + geom_violin(stat="identity")
Warning: Ignoring unknown parameters: trim, scale
Error in eval(substitute(list(...)), `_data`, parent.frame()) :
object 'violinwidth' not found
Trying to add aes(violinwidth=0.2*count), as this answer suggests, gives
> ggplot(allData, aes(x = tool, y = length)) + geom_violin(stat="identity", aes(violinwidth=0.2*count))
Warning: Ignoring unknown parameters: trim, scale
Warning: Ignoring unknown aesthetics: violinwidth
Error in FUN(X[[i]], ...) : object 'count' not found
And while I can set violinwidth to just a constant, this makes the violins just rectangles. How can I fix this?
When I run this with some sample data, it generates the plots ok with and without the changes to stat and violinwidth. Is your count a column in allData?
library(ggplot2)
dt <- data.frame(category = rep(letters[1:2], each = 10),
response = runif(20),
count = rpois(20, 5))
ggplot(dt, aes(x = category, y = response)) + geom_violin()
ggplot(dt, aes(x = category, y = response)) +
geom_violin(stat = "identity", aes(violinwidth = 0.1*count))

how to add mean value in ggplot2

I am new to R, I have a plot that shows the percprof stats in each state of all counties, but when I am trying to add a mean value to each plot, it is not working:
library(ggplot2)
data(midwest)
percprof_mean <- sd(midwest$percprof)
ggplot(midwest, aes(x=percprof, y=..density..))
+ geom_histogram(binwidth = 0.5, color = "white") + facet_grid(state ~.)
+ stat_summary(fun.y=mean,geom="line",lwd=2,aes(group=1))
did I use stat_summary function wrong here? I get error says:
Error: stat_summary requires the following missing aesthetics: y
plyr based solution.
library(ggplot2)
library(plyr)
data(midwest)
nn <- ddply(midwest, "state", transform,
state_mean = mean(percprof))
ggplot(nn) +
geom_histogram(aes(percprof, y=..density..),binwidth = 0.5, color = "white") +
geom_vline(aes(xintercept = state_mean),data=nn,linetype = 5) + facet_grid(state~.)

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

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 )

Stat_function on ggplot doesn't work

I have a problem with ggplot, especially with stat_function.
I want to plot my distribution, then I want to calculate the Chi-square theoretical distribution and plot it on my actual plot.
Without ggplot, I made this with a simple lines()
res.lk <- apply (as.matrix(baf_matrix$res.fst), 2, FUN = function (x) baf_matrix$res.fst*4 / (mean(baf_matrix$res.fst)))
hist(res.lk, probability = T, breaks = 100,main = "LK distribution",
xlab ="LK")
x = c(0:as.integer(max(res.lk)))
lines(x,dchisq(x,df=4),lwd=3,col="orange")
Now to make it with ggplot I do this:
Y = as.data.frame(res.lk)
ggplot(data = Y , aes( x = Lk )) + geom_histogram( binwidth=.1, color="black", fill="white" ) + stat_function( fun = dchisq(c(0:as.integer(max(res.lk))),df=4), lwd = 3, col = "orange") + theme_bw()
And I get this error : Warning message:
Computation failed in stat_function():
'what' must be a function or character string
This is what my data looks like :
The Lk distribution
I'm trying to fix it but I didn't find how. Can somebody help me please?? Thank you a lot in advance.
Note: it would really help if you included example data for us to work with.
The argument fun has to be a function. You're passing a vector. Also, because the distribution line only depends on a single value in the data, it'd be better to use annotate:
xseq <- seq(max(Y$res.lk))
ggplot(data = Y, aes(x = Lk)) +
geom_histogram(binwidth = .1, color="black", fill="white") +
annotate(
geom = "line",
x = xseq,
y = dchisq(xseq, df = 4),
width = 3,
color = "orange"
) +
theme_bw()

Resources