Manipulate graphics within an R package (eRm) - r

I am using the eRm package to run the dichotomous Rasch model. My problem is that I can not manipulate the length of the x axis on the person-item map.
The description for the plotPImap command says to use xrange= to specify the range, but when I do so, I get the following error:
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ
Here is my code:
Model2 <- RM(EBL_MC)
plotPImap(Model2, xrange=c(-5:5), latdim = "Writing Achievement", main="Person-Item Map (EBL)")
Suggestions?

This is just a shot in the dark, since you haven't provided a reproducible example, but generally when functions in R ask for an axis range argument, they are looking for only two values, the low and high. Try xrange = c(-5,5).

Related

Getting weird error with the function as.ggplot() from ggplotify package

This uses the library ggplotify. This allows me to take a base r plot and create a ggplot plot from it with the function as.ggplot().
However, I am receiving an error that doesn't make sense to me when I run the following lines of code.
y <- 1:10
x <- 2:11
length(x)
length(y)
as.ggplot(~plot(x, y))
The error I receive is:
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ.
However, x and y have the same length and when I use plot(x,y) normally, I don't receive any errors. Can someone please explain this to me? Thanks in advance!
It works when you put the values directly in the function:
as.ggplot(~plot(1:10,2:11))
I think it is just limited to working like this.

Adding data labels to boxplot in R

Here's my code:
iFacVector <- as.factor(c(1,1,1,1,10,1,1,1,12,9,9,1,10,12,1,9,5))
iTargetVector <- c(2,1,0,1,6,9,15,1,8,0,1,2,1,1,9,12,1)
bp <- plot(iFacVector,iTargetVector)
text(bp,tapply(iTargetVector,iFacVector,median),labels=tapply(iTargetVector,iFacVector,median),cex=.8)
I am getting the following (classic R) error:
Error in xy.coords(x, y, recycle = TRUE) :
(list) object cannot be coerced to type 'double'
The vectors I am passing are numeric so I don't know what the problem is. I have tried unlist() and as.vector(). I have also tried using bp$stats[3,] as the labels.
The help for text gives the arguments as
text(x, ...)
so the first argument in your code, bp, is being treated as the x coordinate for where to place the text. You can just leave off the bp and get better behavior. You might also want to add pos=3 to get a nicer placement of the text.

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.

How to limit boundaries when plotting a shapefile in R

I hope this isn't a basic question, I've had a hard time finding online resources for using R with shapefiles. I have a shapefile of the 5 digit zip codes in Texas, specifically the one at the bottom of this page.
I'm loading the zip code data and plotting it as such:
> library(maptools)
> zipData <- readShapePoly('~/Documents/Shapefiles/zipCodesTX/tl_2009_48_zcta5.shp')
> plot(zipData)
However, this yields the full map of Texas. I'd like to pare it down to just Dallas.
I thought about using zipData#bbox to find the max values and using xlim and ylim to shrink it down from there, however, this causes the y and x axis to have different amounts.
> zipData#bbox
min max
x -106.64565 -93.50844
y 25.83723 36.99566
> plot(zipData, xlim <- c(-100, -95))
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ
Does anyone have an idea of an easy way to do this?
Further basic shapeplot question: How does plot() actually plot my shapefile? names(zipData) reveals the names of the data frame columns as:
> names(zipData)
[1] "ZCTA5CE" "CLASSFP" "MTFCC" "FUNCSTAT"
[5] "ALAND" "AWATER" "INTPTLAT" "INTPTLON"
Obviously, INTPTLAT and INTPTLON are lat and long coordinates, but plotting these as:
> plot(zipData$INTPTLAT, zipData$INTPTLON)
yields a big black box. How exactly are maps generated using plot() with shapefiles?
I apologize if these questions are very base, I just could not find a good resource or explanation of this.
You can change the limits of a plot using the xlim and ylim arguments of the plot function:
library("rgdal")
shp <- readOGR("tl_2009_48_zcta5.shp", "tl_2009_48_zcta5")
plot(shp, xlim=c(-97.13, -96.47), ylim=c(32.47, 33.08), col="orange")
or you can subset shp (an object of class SpatialPolygonsDataFrame):
zip_dallas <- c(75019, 75039, 75043, 75048, 75050, 75051, 75060, 75062, 75081,
75089, 75098, 75104, 75125, 75134, 75141, 75146, 75149, 75154,
75159, 75172, 75181, 75182, 75217, 75232, 75241, 75247, 75253,
75001, 75006, 75248, 75254, 75180, 75007, 75234, 75287, 75115,
75137, 75249, 75211, 75063, 75067, 75041, 75052, 75061, 75080,
75088, 75116, 75150, 75201, 75202, 75203, 75204, 75205, 75206,
75207, 75208, 75209, 75210, 75212, 75214, 75215, 75216, 75218,
75219, 75220, 75223, 75224, 75225, 75226, 75227, 75228, 75229,
75230, 75231, 75233, 75235, 75236, 75237, 75238, 75240, 75243,
75244, 75246, 75251, 75252, 75270, 75040, 75042, 75044, 75038,
75082, 76051)
ind <- x[["ZCTA5CE"]] %in% zip_dallas
plot(x[ind, ], col="orange")
Applied Spatial Data Analysis with R is a good reference for basic R usage and advanced spatial statistics.
Too many questions in there really.
First, read the R Spatial Task View for info on spatial data in R.
Then maybe read an introduction to spatial data in R by me: http://www.maths.lancs.ac.uk/~rowlings/Teaching/UseR2012/introductionTalk.html
Then notice that you used <- when you should have used =:
plot(zipData, xlim <- c(-100, -95))

Resources