Error In Bar plot - incorrect number of names - r

I'm having trouble creating bar plots in R. The odd thing is that I've used the same script before with different variables without any issues, so I don't really know what is going on (I'm fairly new to R).
The code I've been using is (with transposed matrix):
barplot(StandtF, main="h2, c2, e2 by Mod FEMALES",
xlab="Social Support", ylab="% of Var", beside=TRUE, axesnames=TRUE,
names.arg=c("No","Yes"),
col=c("red","green","darkblue"))
legend("topleft",c("h2","c2","e2"),fill=c("red","green","darkblue") )
And the error message that's returned is:
Error in barplot.default(StandtM, main = "h2, c2, e2 by Mod FEMALES",
xlab = "Social Support", :
incorrect number of names
In addition: Warning message:
In plot.window(xlim, ylim, log = log, ...) :
"axesnames" is not a graphical parameter
I've looked around at different posts, but nothing so far has been helpful. And as said, the script has worked fine previously.
Any help would be greatly appreciated.

You gave to the function two names names.arg=c("No","Yes")and you have three levels according to your legend and the colors defined.
So, you need a vector with three names.

Related

Error in as.double(y) : cannot coerce type 'environment' to vector of type 'double' in ggplot command in 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.

Error message when ploting with "R"

I am attempting to plot my dataset using PCA in R in Windows 10. But while plotting I got the message
zero-length 'labels' specified
Below are the data set and code:
Taxon,L/D1 ,L/D4,L/DL,Di.Pro.,ST,H.Fo/H.Ch
1,3,2.9,2.28,200,2,1
1,1.33,2.3,2.44,225,2,1
1,2,2.52,2.4,150,2,1
1,3.5,2.75,2.32,125,2,1
1,3.18,2.81,2,125,2,1
1,2.35,3,2.82,125,2,1
1,2.72,2.28,2.09,250,2,1
2,3.6,2.82,2.65,125,4,1
2,2.5,2.7,3.02,150,4,1
2,3.2,2.6,2.7,150,4,1
2,2.4,3.3,2.7,200,4,1
2,3,2.87,3.08,175,4,1
3,2,2.83,2,100,1,0.67
3,4,2.66,2.26,100,1,0.67
3,2.33,2.78,2.28,150,1,0.67
3,2,2.83,2.3,100,1,0.67
4,2.83,2.83,2.66,200,1,0.66
4,3.66,2.57,2.27,100,1,0.66
4,3.33,2.9,2.25,150,1,0.66
4,4.33,3.09,2.15,125,1,0.66
4,1.85,2.44,2.43,225,1,0.66
4,2.85,2.57,2.65,175,1,0.66
4,2.8,2.55,2.2,200,1,0.66
4,1.85,2.57,2.04,175,1,0.66
5,2.83,2.5,3,125,3,0.66
5,2.8,4,3.14,200,3,0.66
5,2,3.5,2.4,200,3,0.66
5,3,3.42,2.9,150,3,0.66
5,2.4,2.6,2.71,175,3,0.66
5,4,3.37,2.52,125,3,0.66
5,1.75,2.69,2.9,250,3,0.66
5,2,2.54,2.76,200,3,0.66
5,2.4,3.25,2.46,175,3,0.66
6,3.5,3.14,2.48,75,5,1
6,2.25,2.16,2.1,75,5,1
6,3,3.23,2.77,125,5,1
6,2.5,2.3,1.85,100,5,1
6,2.95,2.71,2.14,100,5,1
6,2.5,3.4,3.09,125,5,1
6,2,2.57,2.3,100,5,1
6,3,2.55,2.46,125,5,1
7,3,4.76,2.94,100,1,1
7,1.66,2.77,2.2,200,1,1
7,2.16,2.8,2.4,250,1,1
8,2.25,3.09,2.83,150,3,1
8,3.5,3.5,2.88,125,3,1
8,3.33,2.87,3.04,150,3,1
library(vegan)
daz= read.csv (file.choose())
cor(daz)
daz.pca<-rda(daz)
summary(daz.pca)
PCA1,2
daz.taxon.pca <- rda(daz[,c(2:7)],scale=T)
par(pty="s")
plot(scores(daz.taxon.pca,display="sites",choices=1),scores(daz.taxon.pca,display="sites",choices=2),type="n",xlab="PCA1 (99.69%)",ylab="PCA2 (0.19%)",xlim=c (-8.00,8.00),ylim=c(-2.00,1.50))
abline(v=0,lty=3)
abline(h=0,lty=3)
arrows(0,0,scores(daz.taxon.pca,display="species",choices=1),scores(daz.taxon.pca,display="species",choices=2),length=0.10,col="dark gray")
text(scores(daz.taxon.pca,display="sites",choices=1),scores(daz.taxon.pca,display="sites",choices=2),as.character(daz$taxon),cex=0.7)
Error in text.default(scores(daz.taxon.pca, display = "sites", choices
= 1), : zero-length 'labels' specified
Any help? I am rather new to R, and failed to find any explanation of this error. The code in general seems fine, except when I add
text(scores(daz.taxon.pca,display="sites",choices=1),scores(daz.taxon.pca,display="sites",choices=2),as.character(daz$taxon),cex=0.7)
for the plot, the error comes up. Any hint would be greatly appreciated.
You use daz$taxon instead of daz$Taxon to access the data in daz variable which return back NULL.
Use the following code at the end of your analysis and it should work fine:
text(scores(daz.taxon.pca, display="sites", choices=1),
scores(daz.taxon.pca, display="sites", choices=2),
as.character(daz$Taxon),
cex=0.7)

R : Plot many boxplot in the same graph using dataframe

Hello everyone and thank you for helping me with R.
i have a 39cols * 168rows matrix which looks like this :
and i want to plot boxplot (1 for each row) in the same graph.
Two hours of intense research on how to make that and i still have no clue.
What i tried (f is the read.csv i got ) :
boxplot(x = as.list(as.data.frame(f)))
qp <- boxplot(x = as.list(as.data.frame(f)))
rn <- as.numeric(rownames(f))
plot(qp,rn)
and i've got :
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ
even if i do not know if the result of the plot was the thing i wanted.
If you want one box per row, you need to transpose the matrix before passing it to boxplot:
boxplot(t(f))
This will then do the right thing — provided you’ve read in your data correctly. In your image, this isn’t the case: the header columns are (incorrectly) part of the data. Be sure to pass header = TRUE to your data reading function to fix this.

R qqplot argument "y" is missing error

I am relatively new to R and I am struggling with a error messages related to qqplot. Some sample data are at the bottom. I am trying to do a qqplot on some azimuth data, i.e. like compass directions. I've looked around here and the ?qqplot R documentation, but I don't see a solution I can understand in either. I don't understand the syntax for the function or the format the data are supposed to be in, or probably both. I First I tried loading the data as a single column of values, i.e. just the "Azimuth" column.
azimuth <- read.csv(file.choose(), header=TRUE)
qqplot(azimuth$Azimuth)
returns the following error,
Error in sort(y) : argument "y" is missing, with no default
Then I tried including the corresponding dip angles along with the azimuth data and received the same error. I also tried,
qqnorm(azimuth)
but this returned the following error,
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ
Dataframe "azimuth":
Azimuth Altitude
23.33211466 -6.561729793
31.51267873 4.801537153
29.04577711 5.24504954
23.63450905 14.03342708
29.12535459 7.224141678
20.76972007 47.95686329
54.89253987 4.837417689
56.57958227 13.12587996
13.09845182 -7.417776178
26.45155154 31.83546988
29.15718557 25.47767069
28.09084746 14.61603384
28.93436865 -1.641785416
28.77521371 17.30536039
29.58690392 -2.202076058
0.779859221 12.92044019
27.1359178 12.20305106
23.57084707 11.97925859
28.99803063 3.931326877
dput() version:
azimuth <-
structure(list(Azimuth = c(23.33211466, 31.51267873, 29.04577711,
23.63450905, 29.12535459, 20.76972007, 54.89253987, 56.57958227,
13.09845182, 26.45155154, 29.15718557, 28.09084746, 28.93436865,
28.77521371, 29.58690392, 0.779859221, 27.1359178, 23.57084707,
28.99803063), Altitude = c(-6.561729793, 4.801537153, 5.24504954,
14.03342708, 7.224141678, 47.95686329, 4.837417689, 13.12587996,
-7.417776178, 31.83546988, 25.47767069, 14.61603384, -1.641785416,
17.30536039, -2.202076058, 12.92044019, 12.20305106, 11.97925859,
3.931326877)), .Names = c("Azimuth", "Altitude"), class = "data.frame", row.names = c(NA, -19L))
Try:
qqPlot
with a capital P.
Maybe you want to create the graph.
Have you ever tried?
qqnorm(azimuth$Azimuth);qqline(azimuth$Azimuth)
It seems that the qqplot function takes two input parameters, x and y as follows:
qqplot(x, y, plot.it = TRUE, xlab = "your x-axis label", ylab="your y-axis label", ...)
When you made your call as given above, you only gave one vector, hence R complained the y argument was missing. Check you input data set and see if you can find what x and y should be for your call to qqplot.

r - Add text to each lattice histogram with panel.text but has error "object x is missing"

In the following R code, I try to create 30 histograms for the variable allowed.clean by the factor zip_cpt(which has 30 levels).
For each of these histograms, I also want to add mean and sample size--they need to be calculated for each level of the factor zip_cpt. So I used panel.text to do this.
After I run this code, I had error message inside each histogram which reads "Error using packet 21..."x" is missing, with..." (I am not able to read the whole error message because they don't show up in whole). I guess there's something wrong with the object x. Is it because mean(x) and length(x) don't actually apply to the data at each level of the factor zip_cpt?
I appreciate any help!
histogram(~allowed.clean|zip_cpt,data=cpt.IC_CAB1,
type='density',
nint=100,
breaks=NULL,
layout=c(10,3),
scales= list(y=list(relation="free"),
x=list(relation="free")),
panel=function(x,...) {
mean.values <-mean(x)
sample.n <- length(x)
panel.text(lab=paste("Sample size = ",sample.n))
panel.text(lab=paste("Mean = ",mean.values))
panel.histogram(x,col="pink", ...)
panel.mathdensity(dmath=dnorm, col="black",args=list(mean=mean(x, na.rm = TRUE),sd=sd(x, na.rm = TRUE)), ...)})
A discussion I found online is helpful for adding customized text (e.g., basic statistics) on each of the histograms:
https://stat.ethz.ch/pipermail/r-help/2007-March/126842.html

Resources