error in density.default : x must be numeric - r

This question might have been repeated. But even after going through the previous links, I am not able to solve this. I have a file as follows:
data <- read.table("data.txt", header=TRUE)
Samp1 Samp2 Samp3
cg00000029 0.79015390399987 0.8301816 0.8966661
cg00000108 0.970260858767027 0.9655997 0.9699428
cg00000109 0.948456317952246 0.9209855 0.9325146
cg00000165 0.267769194351135 0.2370634 0.3867273
I wish to create a density plot out of the column (say Samp1). When I use the following
>plot(density(na.omit(data$Samp1)), col="black")
I get the following error:
Error in density.default(na.omit(data$Samp1)) : argument 'x' must be numeric
Can anyone help me know how to i correct this? I have created density plots for similar files, but did not get this error. It is only for this file.
Your help appreciated.
Thanks in advance..

Well, for some reason your data is non-numeric: have you tried using as.numeric() to coerce it into the right type?
Edit: Using unlist() to convert it out of a list type seems to be the answer

I was getting the same problem, I was able to solve it by doing:
a<- density(treeDF$real)
plot(a$x, a$y, col="turquoise2")
b<- density(treeDF$rpart)
lines(b$x, b$y, col="deeppink3")
c<- density(treeDF$rpart_Fourier)
lines(c$x, c$y, col="blue")
legend("topright", legend = c("real value", "rpart()", "rpart() + Fourier tem"),
col=c("turquoise2", "deeppink3", "blue"), lty=1:1, cex=0.8, box.lty = 0)

Related

Change order of plotting in Likert Scale

I am trying to change the order in which my Likert items are being plotted with the Likert package and so far I haven't been very successful. Let's consider the following minimal code to reproduce my error. I would like to see plotting with a specific custom (or at least Question 1 to Question 4, top to bottom) ordering. I have tried several things (based on some questions and answers on here), and all failed.
First the data:
question1<- c(1,5,3,4,1,1,1,3,4,5)
question2<- rev(c(1,5,3,4,1,1,1,3,4,5))
question3<- c(1,1,1,2,2,2,3,3,4,5)
question4<- c(5,5,5,4,4,4,3,3,2,1)
testData<-data.frame(question1,question2,question3,question4)
testData <- lapply(testData, factor, levels= c(1:5), ordered = TRUE)
testData <- as.data.frame(testData)
Then plotting attempt #1:
p <- (likert(testData))
plot(p)
Gives me the following:
Plotting attempt number 2 (close enough but order is reversed and this does not give me a solution for any random ordering):
p <- (likert(testData))
plot(p, ordered=FALSE)
Gives me this:
Plotting attempt #3:
p <- (likert(testData))
p$Item <- factor(p$Item, levels = rev(c("question1", "question2", "question3", "question4")))
plot(p)
Also does not work. Would anyone have any idea how to solve this?
Thanks in advance.
Ok turns out I found the answer, doing the following works:
p <- (likert(testData))
plot(p, group.order = c("question1", "question2", "question3", "question4"))

Plotting 3D Surface Chart

I'm new to R and am trying to plot a 3D surface. I thought it would be a pretty simple process as I have all my data in a nice table but can't figure out where I'm going wrong.
my_data2 (read in from Excel):
X1 X1.1 X1.21 X1.33 X1.46 X1.61 X1.77 X1.98 X2.14 X2.35
4e+05 291208737 291296846 291744988 292676157 304539662 347763047 346637087 352381080 361467196 334153676
5e+05 301234194 301322304 342042259 344633543 346394275 347763047 392216772 376048898 361467196 334153676
The first column is my row headers.
I've tried a few different R packages but am obviously missing something.
I thought it would be as simple as:
> r <- 1:nrow(my_data2)
> c <- 1:ncol(my_data2)
> z <- c(my_data2)
> contour3D(x=r, y=c, z=z, colvar=Volcano)
Error message displayed: "exactly one of the values 'x' 'y', or 'z' should be a matrix or one value"
I thought my z variable was a matrix!
Can anyone please help?
Tks
from the help page: "contour3D adds a contour in a 3-D plot." So I think you actually want something else. Maybe something like this:
library(plot3D)
z <- as.matrix(my_data2)
hist3D(z=z)

Plot two columns in R from csv file

I have just started to learn R and I have a problem with plotting some values read from a CSV file.
I have managed to load the csv file:
timeseries <- read.csv(file="R/scripts/timeseries.csv",head=FALSE,sep=",")
When checking the content of timeseries, I get the correct results (so far, so good):
1 2016-12-29T19:00:00Z 6
...
17497 2016-12-30T00:00:00Z 3
Now, I am trying to plot the values - the date should be on the x-axis and the values on the y-axis.
I found some SO questions about this topic: How to plot a multicolumn CSV file?. But I am unable to make it work following the instructions.
I tried:
matplot(timeseries[, 1], timeseries[, -1], type="1")
Also, I tried various barplot and matplot modifications but I usuassly get some exception like this one: Error in plot.window(...) : need finite 'xlim' values
Could someone suggest how to tackle this problem? Sorry for elementary question...
You need to make sure your dates have class Date.
dates <- c("2016-12-29T19:00:00Z", "2016-12-30T00:00:00Z")
values <- c(6,3)
df <- data.frame(dates, values)
df$dates <- as.Date(df$dates)
Then you could use ggplot2
library(ggplot2)
qplot(df$dates, df$values) + geom_line()
or even the default
plot(df$dates, df$values, type = "l")
or with lattice as in the question you referred to
library(lattice)
xyplot(df$values ~ df$dates, type = "l")

plotting DCC results with R

I have been running a dcc garch on R; the results is presented as matrix
I would like to extract the second column as a vector to plot, with date on the x-axis.
For the moment, if I define
DCCrho = dccresults$DCC[,2]
then head(DCCrho) yields this:
1 0.9256281
2 0.9256139
3 0.9245794
...
any help to redefine this as a simple vector of numerical values?
any other option to graph the results of dcc with date on the x-axis?
Thanks a lot!
While trying this
x <- cbind(DCCrho, com_30[,2])
head(x)
and this:
matplot(DCCrho ~ x[,2], x, xaxt = "n", type='l')
yields the following error message:
"Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x), :
invalid first argument"
Apparently it was a matter of length of the vector; the Date and the DCC results need to be vectors of same length.
One also needs to plot both date and DCCrho as shown below.
matplot(com_30$date, DCCrho, xaxt = "n", type='l')
axis(1, com_30$date, format(com_30$date, "%y"), cex.axis = .7)
I'm assuming that when you said, "I would like to extract the second row..." that you actually meant "column", because you did the following: dccresults$DCC[,2] Also, as pointed out by a previous comment, the code isn't reproducible, so it's difficult to propose and answer with certainty. However, I'll do my best.
You said you wanted DCCrho to be a "simple vector of numerical values". I'm assuming that this is largely a matter of the way that the values are displayed. Does DCCrho = as.vector(dccresults$DCC[,2]) look better?.
As for the error message, I think that it's b/c in matplot(x,y, ...), x can't be a formula. Try matplot(DCCrho, x[,2]).
If you just want to plot the DCCrho value across some index, you could try something like the following:
Y <- as.vector(dccresults$DCC[,2])
X <- seq_along(Y)
plot(X,Y)
Does that work? Aside from the arbitrary time index, what did you intend to reference as "time"? I don't see a part of the code that you supplied (e.g., a column in dccresults$DCC) that would be an obvious candidate for use as a "date".

How to name the "superscriptions" of dimensions in r?

perhaps a dumb question, yet I cannot find an answer.
If I make a mosaic plot with a vcd package so:
library(vcd)
test<-matrix(c(65,31,495,651), ncol=2,byrow=T)
colnames(test)<-c("2010", "2011")
rownames(test)<-c("yes", "now")
mosaic(test, shade=T, legend=T)
it works like a charm except that the superscriptions over the years and the outputs (yes/no) are shown "A" and "B".
I would like to name these "Years" and "Outputs" but I cannot find a parameter for this.
How could I do this? Thanks in advance.
You can specify dimnames this way :
dimnames(test) <- list(foo=colnames(test),bar=rownames(test))
mosaic(test, shade=T, legend=T)
In fact, mosaic is better suited to be applied to contingency tables, where the labels are determined by the table function :
color <- sample(c("red","blue"),10,replace=TRUE)
color2 <- sample(c("yellow","green"),10,replace=TRUE)
tab <- table(color,color2)
mosaic(tab, shade=T)

Resources