Getting "missing aesthetics error" while using ggplot2's geom_errorbar - r

my code looks like this
Q <- c(1.624e-05, 1.583e-05, 1.540e-05, 1.512e-05, 1.487e-05, 1.420e-05, 1.334e-05, 1.270e-05, 1.239e-05, 1.180e-05, 1.081e-05, 9.720e-06, 8.880e-06, 7.770e-06, 6.740e-06, 5.790e-06, 4.430e-06, 3.410e-06)
diP <- c(4443.93, 4022.10, 3698.37, 3423.69, 3237.30, 2943.00, 2668.32, 2501.55, 2383.83, 2148.39, 1952.19, 1618.65, 1422.45, 1177.20, 1010.43, 814.23, 559.17, 343.35)
eQ <- c(1.6e-06, 1.6e-06, 1.6e-06, 1.5e-06, 1.5e-06, 1.4e-06, 1.3e-06, 1.3e-06, 1.2e-06, 1.2e-06, 1.1e-06, 1.0e-06, 9.0e-07, 8.0e-07, 7.0e-07, 6.0e-07, 5.0e-07, 4.0e-07)
ediP <- seq(10,10,length=18)
ggplot() + stat_smooth(aes(diP[10:18],Q[10:18]),method = "lm",col="red",se=F) + geom_point(aes(diP,Q)) +
geom_errorbar(aes(ymin=Q-eQ, ymax=Q+eQ), width=.1) +
xlab(expression(paste(Delta,"p (Pa)"))) + ylab(expression(paste("Q (",m^3,"/s)")))
When I run all, I get the following error:
Error: geom_errorbar requires the following missing aesthetics: x or
y, xmin and xmax
Ty in advance!

You could provide x=diP as the x variable, but I'm going to recommend two best practices for working with ggplot: (1) use the data argument explicitly (don't grab variables from the global workspace) (2) allow aesthetics to be inherited whenever it makes sense. This gets me to:
dd <- data.frame(Q, diP, eQ, ediP)
ggplot(dd, aes(x=diP, y=Q)) +
stat_smooth(data=dd[10:18,],
method = "lm",col="red",se=FALSE) +
geom_point() +
geom_errorbar(aes(ymin=Q-eQ, ymax=Q+eQ), width=.1) +
xlab(expression(paste(Delta,"p (Pa)"))) +
ylab(expression(paste("Q (",m^3,"/s)")))
by specifying data=dd and the mapping in the initial ggplot(), you are allowing these mappings to be inherited by subsequent geoms. I overrode the data argument in the stat_smooth to make sure only the appropriate subset of the data got used.

Related

boxCox Error in model.frame.default : 'data' must be a data.frame, environment, or list

This data is from an excel CSV file.
I want to see if a transformation is necessary, but my problem is that I keep getting this message:
Error in model.frame.default(formula = comment$Number.of.Comments ~ comment$Character.Count + : 'data' must be a data.frame, environment, or list
The following is my code:
comment <- read.csv('AdAnalysis3.csv', header = TRUE, fileEncoding = "UTF-8-BOM")
commentfit <- lm(comment$Number.of.Comments ~ comment$Character.Count + comment$Number.of.Shares + comment$Number.of.Likes + comment$Type.of.Ad + comment$Dealing.with.Life + comment$Christlike.Attributes + comment$Spiritual.Learning, data = comment)
library(car)
boxCox(commentfit)
I get the following message immediately after boxCox(commentfit):
Any suggestions?
You haven't given us a reproducible example, but my guess is that you have confused car::boxCox() by including comment$ in your formula. In general it's better (for a number of reasons including clarity) to specify a linear model with just the variable names, i.e.:
commentfit <- lm(Number.of.Comments ~ Character.Count + Number.of.Shares +
Number.of.Likes + Type.of.Ad + Dealing.with.Life +
Christlike.Attributes + Spiritual.Learning,
data = comment)

I need to know why I get the error 'unexpected input in "p<-ggplot(data=mov2, aes(x=Genre,y=Gross % US))" '

I want to prepare the plot's data and aes layers. But this code doesn't run
p <- ggplot(data = mov2, aes(x = Genre, y = Gross % US))
when aes layers is taken off, it's working
p <- ggplot(data = mov2)
p <- ggplot(data = mov2, aes(x = Genre, y = Gross % US)) # this code got error
v <- ggplot(data = movies, aes(x = Genre, y = CriticRating)) #this code is working
Error: unexpected input in "p<-ggplot(data=mov2, aes(x=Genre,y=Gross % US))"
Most R code will get confused with columns that have spaces or weird symbols like %. You need to surround those with backticks to R knows that's supposed to be a column name. Try
p <- ggplot(data=mov2, aes(x=Genre,y=`Gross % US`))

How to use a custom-defined function to change a text label in geom_text()

I have some data, and I want to use some variables from stat_count() to label a bar plot.
This is what I want to do:
library(ggplot2)
library(scales)
percent_and_count <- function(pct, cnt){
paste0(percent(pct), ' (', cnt, ')')
}
ggplot(aes(x=Type)) +
stat_count(aes(y=(..prop))) +
geom_text(aes(y=(..prop..), label=percent_and_count(..prop.., ..count))),
stat='count')
However, I get this error, since it can't find the function in what I assume is either some base packages or the data frame:
Error in eval(expr, envir, enclos) : could not find function "percent_and_count"
I get this error if I do percent(..prop..) as well, although it is fine with scales::percent(..prop..). I did not load my function from a package.
If all else fails, I can do
geom_text(aes(y=(..prop..), label=utils::getAnywhere('percent_and_count')$objs[[1]]((..prop..),(..count..))))
But this seems needlessly roundabout for what should be a stupidly simple task.
You can use bquote and aes_:
# Sample data
set.seed(2017);
df <- data.frame(
Type = sample(6, 100, replace = T)
);
library(ggplot2);
library(scales);
# Your custom function
percent_and_count <- function(pct, cnt){
paste0(percent(pct), ' (', cnt, ')')
}
ggplot(df, aes(x = Type)) +
stat_count(aes(y = ..prop..)) +
geom_text(
stat = "count",
aes_(
y = ~(..prop..),
label = bquote(.(percent_and_count)((..prop..), (..count..)))))
Explanation: bquote(.(percent_and_count)(...)) ensures that percent_and_count is found (as terms .(...) are evaluated in the parent environment). We then use aes_ to ensure that quoted expressions (either with ~ or bquote) are properly evaluated.
Still not pretty, but probably more straighforward than using utils::getAnywhere.

R qqplot aes_ in a function and a function of the data column

I have a data.frame with many columns of the form containing the aggregated values for each time point of variables x,y,z,w ...an:
Time x_mean x_sd y_mean y_sd z_mean y_sd w_mean w_sd ...
Now I want to write a function that plots the mean and a confidence band of +/-1SD around it, with ggplot2. Currently my code looks like:
plotfunc <- function(ds1,val) {
val_mean <- paste(val,"_mean",sep="")
val_p_sd <- paste(val,"_mean + ",val,"_sd",sep="")
val_m_sd <- paste(val,"_mean - ",val,"_sd",sep="")
ggplot() + geom_line(data=ds1,aes_q(x=as.name("TIME"),y=as.name(val_mean),color="good")) +
geom_ribbon(data=ds1,aes_q(x=as.name("TIME"),ymin=as.name(val_m_sd),ymax=as.name(val_p_sd),alpha=0.3,fill="good"))
}
And I call it with:
plotfunc(df,"x")
It complains:
Error in eval(expr, envir, enclos) : object 'x_mean - x_sd' not found
How do I get the upper and lower bounds? Do I need to use substitute or quote ?
I used aes_string and corrected some syntax errors in your function code:
- color "good" unkown
- the color argument, which is specified to a fixed value, has hence to be outside the aes function
df = data.frame("TIME"=11:20, "x_mean"=rnorm(10, mean=10), "x_sd"=rnorm(10, mean=1, sd=0.1),
"y_mean"=rnorm(10, mean=12), "y_sd"=rnorm(10, mean=2, sd=0.2))
plotfunc <- function(ds1,val) {
val_mean <- paste(val,"_mean",sep="")
val_p_sd <- paste(val,"_mean + ",val,"_sd",sep="")
val_m_sd <- paste(val,"_mean - ",val,"_sd",sep="")
ggplot() + geom_line(data=ds1,aes_string(x=as.name("TIME"),y=as.name(val_mean)),color="red") +
geom_ribbon(data=ds1,aes_string(x=as.name("TIME"),ymin=as.name(val_m_sd),ymax=as.name(val_p_sd)),alpha=0.3,fill="blue")
}
plotfunc(df,"x")
Is that OK for you?

creating heatmap with R with eye-tracker data

I have a table composed by the following data
frame,X,Y
which is the resulting data from several eye tracking analysis.
Now I would like to create a Heatmap using R, like the following
I tried several script found online, none of them gave me that result.
How can I do?
Here some sample data
Ignore the first two columns
task,visualization,frame,X,Y
1,b,1,383,221
1,b,1,632,356
1,b,1,947,663
1,b,1,546,206
1,b,1,488,272
1,b,1,578,752
1,b,1,415,261
1,b,1,693,158
1,b,1,684,528
1,b,1,592,67
1,b,1,393,180
1,b,1,1033,709
1,b,1,1080,739
1,b,1,711,523
1,b,1,1246,49
1,b,1,742,69
1,b,1,601,370
1,b,10,902,684
1,b,10,517,241
1,b,10,583,86
1,b,10,582,754
1,b,10,426,257
1,b,10,575,229
1,b,10,697,150
1,b,10,379,520
1,b,10,390,286
1,b,10,618,396
1,b,10,710,143
1,b,10,383,188
1,b,10,1026,713
1,b,10,1078,625
1,b,10,713,521
You can get this type of plot quite easily using stat_bin2d from ggplot2:
library(ggplot2)
ggplot(dat, aes(x = X, y = Y)) + stat_bin2d(bins = 10)
This does simple binning, as #RomanLustrik suggested you could also perform some kind of kernel smoothing. This can also be done using ggplot2:
ggplot(dat, aes(x = X, y = Y)) +
stat_density2d(geom = "tile", aes(fill = ..density..), contour = FALSE) +
geom_point()
Note that dat is the example data you gave, geting your data into a data.frame:
dat = read.table(textConnection("task,visualization,frame,X,Y
1,b,1,383,221
1,b,1,632,356
1,b,1,947,663
1,b,1,546,206
1,b,1,488,272
1,b,1,578,752
1,b,1,415,261
1,b,1,693,158
1,b,1,684,528
1,b,1,592,67
1,b,1,393,180
1,b,1,1033,709
1,b,1,1080,739
1,b,1,711,523
1,b,1,1246,49
1,b,1,742,69
1,b,1,601,370
1,b,10,902,684
1,b,10,517,241
1,b,10,583,86
1,b,10,582,754
1,b,10,426,257
1,b,10,575,229
1,b,10,697,150
1,b,10,379,520
1,b,10,390,286
1,b,10,618,396
1,b,10,710,143
1,b,10,383,188
1,b,10,1026,713
1,b,10,1078,625
1,b,10,713,521"), header = TRUE, sep = ",")

Resources