My issue is the following. I use ROCR package to plot data. performance function returns an object that I pass to plot the data like this:
example <- performance(prediction1,"tpr","fpr")
plot(example,col="red")
I want to add another performance object to this plot, but lines function accepts x and y coords and not an object. In fact if I do: lines(example2, col="blue") this error appears:
Error in as.double(y) :
cannot coerce type 'S4' to vector of type 'double'**
You can add new line with add = TRUE as plot argument:
library(ROCR)
data(ROCR.simple)
prediction1 <- prediction( ROCR.simple$predictions, ROCR.simple$labels)
example1 <- performance(prediction1,"tpr","fpr")
plot(example1, col="red")
example2 <- performance(prediction1, "sens", "spec")
plot(example2, col="blue", add = TRUE)
Related
I want to set limits to the x-axis of the plot of the output of the mixfit function of the mixR package. This output is of class mixfitEM.
Reproducable example: First I simulate a mixture of log normals.
rm(list=ls())
library(mixR)
library("tidyverse")
set.seed(07062022)
N <- 1000
lbda=.5
mu1=1
Del=3
mu2=mu1+Del
components <- sample(1:2,prob=c((1-lbda), lbda),size=N,replace=TRUE)
mus <- c(mu1,mu2)
sds <- sqrt(c(1,.5))
Y <- rlnorm(n=N,meanlog=mus[components],sdlog=sds[components])
Then I try to fit two log normals into this data using mixfit() from mixR. In the first attempt I tried to use the plot() function from the base package; but plot ignores the xlim argument.
mod4 <- mixfit(Y, ncomp = 2, family = 'lnorm')
plot(mod4, title = 'Log-Normal Mixture (2 components)', xlim=c(0,200))
Then I tried to plot with ggplot, which, in theory should be possible according to the mixR manual. But ggplot does not understand the mixfitEM class.
ggplot(mod4)+
+ coord_cartesian(xlim = c(0, 200))
produces the following error:
> ggplot(data.frame(mod4))+
+ + coord_cartesian(xlim = c(0, 200))
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) :
cannot coerce class ‘"mixfitEM"’ to a data.frame
> ggplot(mod4)+
+ + coord_cartesian(xlim = c(0, 200))
Error in `fortify()`:
! `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class mixfitEM.
Run `rlang::last_error()` to see where the error occurred.
When you call plot on the mixfitEM object, you are creating a ggplot. The reason for this is that plot is a generic function, so when package authors create a new class, they are free to use whatever method they want to draw the plot. In this case, if you examine the source code of mixR:::plot.mixfitEM you will see it actually uses ggplot to draw its output. This means you can use ggplot syntax to modify the output:
plot(mod4, title = 'Log-Normal Mixture (2 components)') + xlim(c(0, 200))
I am trying to plot the GMM of my dataset using the Mclust package in R. While the plotting is a success, I do not want points to show in the final plot, just the ellipses. For a reference, here is the plot I have obtained:
GMM Plot
But, I want the resulting plot to have only the ellipses, something like this:
GMM desired plot
I have been looking at the Mclust plot page in: https://rdrr.io/cran/mclust/man/plot.Mclust.html and looking at the arguments of the function, I see there is a scope of adding other graphical parameters. Looking at the documentation of the plot function, there is a parameter called type = 'n' which might help to do what I want but when I write it, it produces the following error:
Error in plot.default(data[, 1], data[, 2], type = "n", xlab = xlab, ylab = ylab, :
formal argument "type" matched by multiple actual arguments
For reference, this is the code I used for the first plot:
library(mclust)
Data1_2 <- Mclust(Data, G=15)
summary(Data1_2, parameters = TRUE, classification = TRUE)
plot(Data1_2, what="classification")
The code I tried using for getting the graph below is:
Data1_4 <- Mclust(Data, G=8)
summary(Data1_4, parameters = TRUE, classification = TRUE)
plot(Data1_4, what="classification", type = "n")
Any help on this matter will be appreciated. Thanks!
If you look under the source code of plot.Mclust, it calls plot.Mclust.classification which in turn calls coordProj for the dot and ellipse plot. Inside this function, the size is controlled by the option CEX= and shape PCH=.
So for your purpose, do:
library(mclust)
clu = Mclust(iris[,1:4], G = 3, what="classification")
plot(clu,what="classification",CEX=0)
I'm writing functions for an R-package which will use a wrapper function for ggpairs from the package GGally to plot the output objects of the methods. I would like ggpairs to be able to use variables not part of the input object for defining aesthetics but this produces an error message with ggpairs, see below for a minimal example:
library(GGally)
library(ggplot2)
# The data object
object <- list(x = iris[, 1:2], label = "Iris data")
# The grouping
y <- iris[, 5]
# The plotting function
wrapper <- function(object, mapping = aes()){
ggpairs(object$x, mapping)
}
# This works
wrapper(object)
# This doesn't work
wrapper(object, aes(color = y))
The latter one produces the error message:
Error in .subset(col, i) : object of type 'symbol' is not subsettable
Any trick to get the second plotting command to work without modifying the input object would be greatly appreciated.
How can I change the x limits of a plot produced by varImpPlot from the randomForest package?
If I try
set.seed(4543)
data(mtcars)
mtcars.rf <- randomForest(mpg ~ ., data=mtcars, ntree=1000, keep.forest=FALSE,
importance=TRUE)
varImpPlot(mtcars.rf, scale=FALSE, type=1, xlim=c(0,15))
I get the following error:
Error in dotchart(imp[ord, i], xlab = colnames(imp)[i], ylab = "", main = if (nmeas == : formal argument "xlim" matched by multiple actual arguments".
This is because varImpPlot defines its own x limits, I think, but how could I get around this if I wanted to set the x limits myself (perhaps for consistency across plots)?
First I extracted the values using importance() (thanks to the suggestion from #dww)
impToPlot <- importance(mtcars.rf, scale=FALSE)
Then I plotted them using dotchart(), which allowed me to manually set the x limits (and any other plot features I'd like)
dotchart(sort(impToPlot[,1]), xlim=c(0,15), xlab="%IncMSE")
I want to create a contour of variable z with the x,y,z data. However, it seems like we need to provide the data in increasing order.
I tried to use some code but it gave me the error.
I tried the following code: Trial 1:
age2100 <- read.table("temp.csv",header=TRUE,sep=",")
x <- age2100$x
y <- age2100$y
z <- age2100$z
contour(x,y,z,add=TRUE,col="black")
I got the following error
Error in contour.default(x, y, z, add = TRUE, col = "black") : increasing 'x' and 'y' values expected
I then tried to use ggplot2 to create the contour. I used the following code:
library("ggplot2")
library("MASS")
library("rgdal")
library("gpclib")
library("maptools")
age2100 <- read.table("temp.csv",header=TRUE,sep=",")
v <- ggplot(age2100, aes(age2100$x, age2100$y,z=age2100$z))+geom_contour()
v
I got the following error:
Warning message:
Not possible to generate contour data
Please find the data on the following location https://www.dropbox.com/s/mg2bo4rcr6n3dks/temp.csv
Can anybody tell me how to create the contour data from the third variable (z) from the temp.csv ? I need to do these many times so I am trying to do on R instead of Arcgis.
Here is an example of how one interpolates using interp from the akimapackage:
age2100 <- read.table("temp.csv",header=TRUE,sep=",")
x <- age2100$x
y <- age2100$y
z <- age2100$z
require(akima)
fld <- interp(x,y,z)
par(mar=c(5,5,1,1))
filled.contour(fld)
Here is an alternate plot using the imagefunction (this allows some flexibility to adding lower level plotting functions (requires the image.scale function, found here):
source("image.scale.R") # http://menugget.blogspot.de/2011/08/adding-scale-to-image-plot.html
x11(width=5, height=6)
layout(matrix(c(1,2), nrow=1, ncol=2), widths=c(4,1), height=6, respect=TRUE)
layout.show(2)
par(mar=c(4,4,1,1))
image(fld)
contour(fld, add=TRUE)
points(age2100$x,age2100$y, pch=".", cex=2)
par(mar=c(4,0,1,4))
image.scale(fld$z, xlab="", ylab="", xaxt="n", yaxt="n", horiz=FALSE)
box()
axis(4)
mtext("text", side=4, line=2.5)