I produced a plot with the partimat function on R.
However, the data points on the picture are ruining the plot. Is there anyway to keep the boundaries and to remove the points from the image? Also, I would like to add a legend to associate each number with it's color.
Here is the code:
library(ElemStatLearn);
data(zip.train);
load(zip.train);
zipTrain=data.frame(zip.train);
x=zipTrain[,-1];
y=zipTrain[,1];
library(MASS)
model=lda(y~.,data=x)
projected.data=as.matrix(x)%*%as.matrix(model$scaling)
projected.data=as.data.frame(projected.data)
M1=projected.data
couleurs=c("aquamarine","blue","brown","chartreuse","cyan","darkgreen","darkorange","pink","gold","gray")
library(klaR)
Matrice=data.frame(X2=M1[,2],X1=M1[,1],Y=y)
partimat(x=Matrice[,-3], grouping=as.factor(Matrice[,3]), method="lda",
col.mean="gold", image.colors =couleurs,pre=200,display.points=FALSE)
Related
I tried to extract the random effect of my LME model with ranef and plot it. But the y-axis has too many levels and thus the labels are all crammed together. Is there a way to reduce the size of the labels or plot only part of the graph?enter image description here
I used this code to obtain the figure.
x<-ranef(Model, condVar=TRUE)
plot(x)
This is how the data looks like.
enter image description here
I'm trying to make a plot in R. My x-axis is a week number converted to factor and my y-axis is an amount.
When I run plot() instead of dots I get horizontal lines.
Why does this happen?
Here is a sample dataset:
df <- data.frame(fin_week=as.factor(seq(1,20, by =1)), amount=(rnorm(20)^2)*100)
plot(df)
Looking at the documentation, it's because the first column is a factor. When R tries to find the right plot() to run, it looks into plot.dataframe, where it plots on the type of 1st column i.e a factor. Hence it plots using plot.factor(), which gives a line by default, which is used for box plots.
try using plot.default(df) to plot and you should get it the scatter plot
I am trying to simulate a minefield by plotting two Poisson distributed samples in the same plot, one with a higher intensity and smaller area than the other. This is the minefield and the other is just noise (stones, holes, metal) seen as points. I cannot get R to plot the points with the same units in the axis. Whatever I do, the points span the entire plot, even though I only want the X points to cover a quarter of the plot. My R-code is just the following:
library(spatstat)
Y = rpoispp(c(5),win=owin(c(0,10),c(0,10)))
X = rpoispp(c(10),win=owin(c(0,5),c(0,5)))
Please let me know if you can help me.
My guess is that you are doing something like:
> plot(Y)
> plot(X)
to plot the points.
The problem with this is that the default behavior of the plot function for the class ppp (which is what the rpoispp function returns) is to create a new plot with just its points. So the second plot call essentially erases the first plot, and plots its own points in a differently scaled window. You can override this behavior by setting the option add=TRUE for the second plot. So the code
> plot(Y)
> plot(X, add=TRUE, cols="red")
should get you something like:
Check out the docs (help(plot.ppp)) for more explanation and other options to prettify the plot.
I've plot this graphic to identify graphically high-leverage points in my linear model.
Given the variable "NOMBRES" of the data set which my model uses, I've tried to plot all the points of my graphic but it gets illegible. Here's the code I ran:
> plot(hatvalues(tmodel),residuals(tmodel))
> text(hatvalues(tmodel),residuals(tmodel),labels=DSET$NOMBRES)
So I would like to plot just the points with leverage(hat value) above 0.05 using the label "DSET$NOMBRES".
Identify high-leverage points according to your definition:
hlev <- which(hatvalues(tmodel)>0.05)
Add numeric labels to the graph:
text(hatvalues(tmodel)[hlev], residuals(tmodel)[hlev],
labels=DSET$NOMBRES[hlev])
I am having trouble controlling the color of data points in a line chart in R. I have the following code:
Pareto <- read.table("ParetoFront.csv",header=TRUE,sep=";")
dfP <- data.frame(Pareto$n,Pareto$z)
plot(dfP$Pareto_n,dfP$Pareto_z,xlim=c(1,max(dfP$Pareto.n)),
ylim=c(min(dfP$Pareto.z),max(dfP$Pareto.z)),xlab="n",ylab="z(n)",type="n")
lines(dfP$Pareto.n,dfPPareto.z,type="o",lwd=2,col="blue",pch=23,bg="red")
This code produces a chart with a blue line and data points filled in red. I would like to have the border color of data points in red as well but I can't figure how to set this pch parameter. I have tried to plot the chart without the type="n" parameter so that I can control the color of the points in the plot function (and not in lines) but when I run the following code
Pareto <- read.table("ParetoFront.csv",header=TRUE,sep=";")
dfP <- data.frame(Pareto$n,Pareto$z)
plot(dfP$Pareto_n,dfP$Pareto_z,xlim=c(1,max(dfP$Pareto.n)),
ylim=c(min(dfP$Pareto.z),max(dfP$Pareto.z)),xlab="n",ylab="z(n)",
pch=23,col="red",bg="red")
I obtain an empty chart: there is no data points at all. I don't understand what is wrong in my second piece of code and I would like to know if there is another way to control the bordel color of points in a line chart.
Thank you for your help.
Example using some made up data:
dfP <- data.frame(Pareto_n=1:10,Pareto_z=1:10)
Now draw a line with lines, and then add the points over the top with points:
plot(dfP$Pareto_n,dfP$Pareto_z,xlim=c(1,max(dfP$Pareto_n)),
ylim=c(min(dfP$Pareto_z),max(dfP$Pareto_z)),xlab="n",ylab="z(n)",type="n")
lines(dfP$Pareto_n,dfP$Pareto_z,lwd=2,col="blue")
points(dfP$Pareto_n,dfP$Pareto_z,lwd=2,pch=23,col="red",bg="red")