Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
My questions is about smoothing or flattening a series of numbers. I have searched for options, but have not found any. In my sample data set:
dat <- list(-21,-18,-16,-10,-8,-4,-3,-2,-1,-0.9,-0.7,-0.5,-0.3,-0.1,0,0.2,0.4,0.6,0.8,1,2,4,6,9,13,17,20,24)
I would like to logarithmically flatten or smooth the data, possibly to something like this (numbers are rounded to one decimal place):
smooth<-list(-4.6,-4.2,-4,-3.2,-2.8,-2,-1.7,-1.4,-1,-0.9,-0.8,-0.7,-0.5,-0.3,0,0.4,0.6,0.8,0.9,1,1.4,2,2.4,3,3.6,4.1,4.5,4.9)
The above is the square root of the original; however, the signs had to be changed for the negative numbers and it is also not desirable, in my case, that the absolute values of the numbers between 1 and -1 actually increased, instead of decreased. I am able to construct the above, but it is not easy or ideal, and I am wondering if there were simpler and better procedures.
Thank you.
How about something like this:
sign(dat)*log(abs(dat)+1)
plot(dat, col = "red")
points(smooth, col = "green")
points(sign(dat)*log(abs(dat)+1), col = "blue")
legend(x = 1, y = 23, legend = c("original", "smooth", "missuse"),
fill = c("red", "green", "blue"))
with log2:
data:
dat <- c(-21,-18,-16,-10,-8,-4,-3,-2,-1,-0.9,-0.7,-0.5,-0.3,-0.1,0,0.2,
0.4,0.6,0.8,1,2,4,6,9,13,17,20,24)
smooth <- c(4.6,-4.2,-4,-3.2,-2.8,-2,-1.7,-1.4,-1,-0.9,-0.8,-0.7,-0.5,-0.3,0,0.4,0.6,
0.8,0.9,1,1.4,2,2.4,3,3.6,4.1,4.5,4.9)
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I'm trying to plot columns of a matrix as vectors into one graph. Is there any plot function to achieve so?
Since you say that you want to plot columns as vectors, I assume that your matrix has two rows. You want to draw an arrow from the origin to the x-y coordinates given by the column. You can do this by making a blank plot and then using the arrows function. Here is a simple example.
## Sample data
set.seed(234)
DAT = matrix(runif(8), nrow=2)
## Plotting
plot(NULL, xlim=c(0,1), ylim=c(0,1), xlab="X", ylab="Y")
arrows(rep(0,4), rep(0,4), DAT[1,], DAT[2,])
Is that what you are looking for?
It's difficult to know what you're looking for if you don't provide some minimum example, but supposing that you have a matrix with coordinates for starting and ending points, then this can be accomplished like this:
Please notice that instead of a matrix I start my example with a data.frame. The former can be converted to the later with the as.data.frame function.
# Create some dummy data
a <- data.frame(x_start = 1:3, y_start = 2:4, x_end = 3:5, y_end = 4:6)
# load required packages
require(ggplot2)
require(grid)
# plot the data:
ggplot(a, aes(x_start, y_start, xend = x_end, yend = y_end))+geom_segment(arrow = arrow())
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to figure out how to generate a number from a range, where each item in the range has a different probability of it being generated.
For instance, I want the chances to be:
p=c(.1,.2,.3,.35, .02, .03)
For a range of 1-6.
I believe I need to use cumsum, but I'm not sure how to go about it.
You can use the sample() function for this. For a range of 1-6 and your specified chances it would look like:
sample(x = 1:6, size = 1, prob = c(.1, .2, .3, .35, .02, .03))
For more information and arguments you can check out ?sample.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am hoping to just change the diagonal plots to have simple outlines so I can view the overlap of the density functions more clearly but am not having much luck. Here is the code I have been using:
plot_rh <- ggpairs(data_rh[,1:6], mapping = ggplot2::aes(color = Condition_name),
lower = list(combo = wrap(ggally_facethist, bins = 10)),
diag = list(continuous = wrap("densityDiag"), mapping = ggplot2::aes(fill=Condition_name)))
Plot with filled density functions:
Changing aes(fill=Condition_name) to aes(color=Condition_name) should result in unfilled lines.
You could also change it to aes(fill=Condition_name), alpha = 0.4 to make the filled densities semi-transparent which may improve the view.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to plot the function y=cx depending on c. It is going to be number of lines going through the point (0,0).
What is the best way to do it?
I mean something like this:
Using abline function seems to be the way. Thanks for the help.
Think this should work. Not sure if it's the best way though
plot(x = 0,y = 0,xlab = "X",ylab = "Y", xlim = c(-10,10), ylim = c(-10,10))
c <- c(1:10) #Store the different values of your constant in this vector
for(i in 1:10){
abline(coef = c(0,c[i]))
}
If you want different limits on your X and Y axis change the values of xlim and ylim.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In this diagram, the main information (most nodes) is on the extreme left side.
I want to make the dendrogram easy to read and thus the edges should be proportionally long. Any specific arguments to be used or is it just the data's problem?
Package ape has an option for plotting a tree (or dendrogram) without edge lengths.
library(ape)
# calculate dendrogram from sample data
data(carnivora)
tr <- hclust(dist(carnivora[1:20,6:15]))
# convert dendrogram from class 'hclust' to 'phylo'
tr <- as.phylo(tr)
# plot, use par(mfrow=c(1,3)) to display side by side
plot(tr)
plot(tr, use.edge.length = FALSE)
plot(tr, use.edge.length = FALSE, node.depth = 2)
This calls the plot.phylo function and enables you to manipulate how the dendrogram looks like. To improve legibility of labels, you might need to tinker settings within plot that influence font size (cex = 0.7) or offset of the label (label.offset = 0.5).