Error in as.double(y) : cannot coerce type 'environment' to vector of type 'double' in ggplot command in R - r

I am trying to make a plot between two different variables (RodC independent varaible and TickPrev dependent varaible) using the ggplot command. I have the following packages installed: pacman, party, rio, tidyverse,dplyr,MASS.
gg1314 <- ggplot(df1314, aes(RodC, TickPrev, color=YearS)) +
geom_jitter() +
labs(title = "Tick Prevalence vs Captured Rodents 2013-2014",
x="Tick Prevalence (%)",
y="Nº of Captured Rodents") +
scale_color_discrete(name="YearSeason")
The code above worked perfectly fine, but when I deleted a few rows to conduct a similar analysis with the same variables, the code start to give the following error:
ggNBS <- ggplot(dfNBS, aes(RodC, TickPrev, colors=Year)) +
geom_jitter() +
labs(title = "Tick Prevalence vs Captured Rodents NBS",
x="Tick prevalence (%)",
y="Nº of captured rodents") +
scale_color_discrete(name="Year")
Error in as.double(y) : cannot coerce type 'environment' to vector
of type 'double'
The only difference between the dataframes is that I eliminated some rows. But the variables remained the same.
enter image description here
How can I solve this.
Thank you very much
Image from df1314. dfNBS is the same, but in the MiceS and YearS it only contains information that had NBS or NBS2.

I found the answer. I was also using %>% plot() so that the plot of the graphic was done right way. After removing that line it solved the problem. :) Thank you for the help.
However now, the plot doens't present colors, all the points are black and shouldn't be.

Related

Problem with R code: Computation failed in `stat_signif()`: not enough 'y' observations

I am trying to compare two groups with 47 observations in each and tried to include the significance level using ggsignif but it gives me this error message :
Computation failed in stat_signif():
not enough 'y' observations
This is the data :enter image description here
The first group being "Un-C_SL" / "Un-C_RL" / "Un-C" and the second one that doesn't appear in the image is "Vp_SL" / "Vp_RL" / "Vp"
This is the code I used:
ggplot(UnC_Vp_2, aes(x = Condition, y = Biomass)) + geom_boxplot() + geom_signif(comparisons = list("Un-C", "Vp"), map_signif_level = TRUE)
I was able to get the box plot but not the significance level. What should I do?
The comparisons = argument needs a list() of pairwise comparisons that are each contained in a c(), so it should work if you change it to comparisons = list(c("Un-C", "Vp")).
See this github issue also. I found this question when I had the same problem; I had forgotten it needed to be a list and not just c() and I agree that a more informative error message would help.

R: $ cannot be used for atomic vectors (box plot)

I would love some advice on how to fix the error shown in the screenshot. I only started learning R yesterday so I'm not very familiar with it. I tried using % but this produced a different type of error (unexpected input). Are there any problems with how I've defined time_spen and species earlier on in the code?
Do you want to make a boxplot of the time_spen variable for each species? The ggplot2 package can help.
Since I don't have your data, here is an example from the penguins dataset in the palmerpenguins package.
library(palmerpenguins)
library(ggplot2)
ggplot(penguins) +
geom_boxplot(aes(x = species, y = body_mass_g))

Using the QQ Plot functionality in ggplot

I'm brand new to R, and have a data frame with 8 columns that has daily changes in interest rates. I can plot QQ plots for data each of the 8 columns using the following code:
par(mfrow = c(2,4))
for(i in 1:length(column_names)){
qqnorm(deltaIR.df[,i],main = column_names[i], pch = 16, cex = .5)
qqline(deltaIR.df[,i],cex = .5)
}
I'd like now to use the stat_qq function in the ggplot2 package to do this more elegantly, but just can't get my arms around the syntax - I keep getting it wrong. Would someone kindly help me translate the above code to use ggplot and allow me to view my 8 QQ plots on one page with an appropriate header? Trying the obvious
ggplot(deltaIR.df) + stat_qq(sample = columns[i])
gets me only an error message
Warning: Ignoring unknown parameters: sample
Error: stat_qq requires the following missing aesthetics: sample
and adding in the aesthetics
ggplot(deltaIR.df, aes(column_names)) + stat_qq()
is no better. The error message just changes to
Error: Aesthetics must be either length 1 or the same as the data (5271)
In short, nothing I have done so far (even with Google's assistance) has got me closer to a solution. May I ask for guidance?

Weird ggplot2 error: Empty raster

Why does
ggplot(data.frame(x=c(1,2),y=c(1,2),z=c(1.5,1.5)),aes(x=x,y=y,color=z)) +
geom_point()
give me the error
Error in grid.Call.graphics(L_raster, x$raster, x$x, x$y, x$width, x$height, : Empty raster
but the following two plots work
ggplot(data.frame(x=c(1,2),y=c(1,2),z=c(2.5,2.5)),aes(x=x,y=y,color=z)) +
geom_point()
ggplot(data.frame(x=c(1,2),y=c(1,2),z=c(1.5,2.5)),aes(x=x,y=y,color=z)) +
geom_point()
I'm using ggplot2 0.9.3.1
TL;DR: Check your data -- do you really want to use a continuous color scale with only one possible value for the color?
The error does not occur if you add + scale_fill_continuous(guide=FALSE) to the plot. (This turns off the legend.)
ggplot(data.frame(x=c(1,2), y=c(1,2), z=c(1.5,1.5)), aes(x=x,y=y,color=z)) +
geom_point() + scale_color_continuous(guide = FALSE)
The error seems to be triggered in cases where a continuous color scale uses only one color. The current GitHub version already includes the relevant pull request. Install it via:
devtools::install_github("hadley/ggplot2")
But more probably there is an issue with the data: why would you use a continuous color scale with only one value?
The same behaviour (i.e. the "Empty raster"error) appeared to me with another value apart from 1.5.
Try the following:
ggplot(data.frame(x=c(1,2),y=c(1,2),z=c(0.02,0.02)),aes(x=x,y=y,color=z))
+ geom_point()
And you get again the same error (tried with both 0.9.3.1 and 1.0.0.0 versions) so it looks like a nasty and weird bug.
This definitely sounds like an edge case better suited for a bug report as others have mentioned but here's some generalizable code that might be useful to somebody as a clunky workaround or for handling labels/colors. It's plotting a rescaled variable and using the real values as labels.
require(scales)
z <- c(1.5,1.5)
# rescale z to 0:1
z_rescaled <- rescale(z)
# customizable number of breaks in the legend
max_breaks_cnt <- 5
# break z and z_rescaled by quantiles determined by number of maximum breaks
# and use 'unique' to remove duplicate breaks
breaks_z <- unique(as.vector(quantile(z, seq(0,1,by=1/max_breaks_cnt))))
breaks_z_rescaled <- unique(as.vector(quantile(z_rescaled, seq(0,1,by=1/max_breaks_cnt))))
# make a color palette
Pal <- colorRampPalette(c('yellow','orange','red'))(500)
# plot z_rescaled with breaks_z used as labels
ggplot(data.frame(x=c(1,2),y=c(1,2),z_rescaled),aes(x=x,y=y,color=z_rescaled)) +
geom_point() + scale_colour_gradientn("z",colours=Pal,labels = breaks_z,breaks=breaks_z_rescaled)
This is quite off-topic but I like to use rescaling to send tons of changing variables to a function like this:
colorfunction <- gradient_n_pal(colours = colorRampPalette(c('yellow','orange','red'))(500),
values = c(0:1), space = "Lab")
colorfunction(z_rescaled)

problems with Hmisc-labelled objects in ggplot

I get the message
Error:no applicable method for 'round_any' applied to an object of
class "labelled"
when I try to plot my graphs using ggplot2 and R. I have labelled my variables in my data frame using Hmisc::label and I think this is the problem. How do I solve this issue?
My labels look like this:
label(data$results_lp)="Lumbure Puncture Results"
label(data$hiv_test)="HIV Test done"
label(data$outcome)="Outcome at Discharge"
label(data$vac_10mnth_complete)="Vaccinne 10months complete"
label(data$vac_3mnth_complete)="Vaccine 3months complete"
label(data$vac_uptodate)="Vaccine up to date"
label(data$dx1_pneum_rcd)="Pneumonia Recorded"
label(data$mal)="Malaria"
label(data$dx1_malaria)="Documented Malaria"
label(data$dehydrat)="Dehydration"
How do I solve this?
Remove the labels for plotting:
library(Hmisc)
DF <- data.frame(x=factor(rep(1:2,5)),y=1:10)
label(DF$x)="xLab"
label(DF$y)="yLab"
library(ggplot2)
ggplot(DF,aes(x=x,y=y)) + geom_boxplot()
#Don't know how to automatically pick scale for object of type labelled. Defaulting to continuous
ggplot(DF,aes(x=factor(unclass(x)),y=unclass(y))) + geom_boxplot()
#no warning
Unfortunately you don't give the details necessary to reproduce your error and give a customized solution.

Resources