I am making an NMDS ordination plot in R with environmental vectors using envfit, and I have missing values in some of my environmental variables. In order not to exclude all rows from all variables containing a missing value in one variable (as with na.rm=TRUE), I have run envfit on each variable separately, but now would like to plot the results on the same plot. Thus I need to use the arrow.mul argument to make the scale of the arrows comparable to one another. And here I run into my problem: the location of the labels for the vectors do not seem to listen to arrow.mul, and labels stay in their original position, floating in space.
If someone could tell me how to get the labels to move to the new arrow point location, or suggest a workaround for the missing values, that would be fantastic. I hope I am not missing something obvious!
Here is a reproducible example of stubborn labels using the dune data from vegan:
data(dune)
data(dune.env)
de2<-dune.env[,c(1,2)]
ord<-metaMDS(dune,k=2)
plot(ord)
de2$Moisture<-as.numeric(de2$Moisture) #pretend this is numeric
ev<-envfit(ord,de2,choices=c(1,2))
ordiplot(ord,type="n")
points(ord,pch=16,col=c(1:4)[dune.env$Management])
plot(ev,arrow.mul=1,labels=list(vectors=c("A1","Moisture")),
col="black",cex=0.9,bg="white")#floating labels
plot(ev,labels=list(vectors=c("A1","Moisture")),
col="black",cex=0.9,bg="white")#labels are fine with no arrow.mul
plot(ev,arrow.mul=1) #default labels behave the same and float with arrow.mul
(yes I know I should learn to use ggplot, but would prefer not to just now, just for this ...)
This indeed is a bug in vegan. I'll fix it in github. Meanwhile, you can use plot(ev, arrow.mul = 1, rescale = FALSE) to have floating text. This will give you a warning, but it is harmless, and will work. Meanwhile I'll fix this in vegan.
Related
I need help in removing/converting the negative values in the x-axis of the pyramid plot.
I was able to build the pyramid through https://walker-data.com/census-r/exploring-us-census-data-with-visualization.html subheading 4.5.2. I keep getting an error saying function not found: number_format.
I think the scale_x_continuous is where I'd be able to change the - sign but returns an error everytime
So from the context of the plot, I am assuming that you don't want to convert the values themselves, but rather convert the x-axis so that the male and female sides of the pyramid are mirrored?
In which case, you can use abs. The function abs(x) computes the absolute value of x, which would remove the negative sign from your data's values.
Without seeing a reproducible example of your code (which should be included when you ask questions like this on Stack Overflow, see the package {reprex} for help with this), it's a little difficult to be sure exactly what you need to change to make the code work, but I think you should be on the right track with scale_x_continuous.
With regard to the error that you're receiving, it suggests that you haven't imported the library for that function, {scales}, as suggested by stefan in the comments (and as suggested, scales:::label_number has superseded scales:::number_format, so you should use the former).
If you're using the scale_x_continuous code from the second plot in Section 4.5.2 of the link you have shared:
utah_pyramid +
scale_x_continuous(
labels = ~ number_format(scale = .001, suffix = "k")(abs(.x)),
limits = 140000 * c(-1, 1)
)
The number_format function isn't the part of the code that is producing the absolute values, it is converting the scale of the values to thousands. It is the abs(.x) part that is removing the negative sign.
I am trying to plot a graph of certain values against time using the plot function.
I am simply trying to change the representation of the dots, using the pch= function. However R is simply ignoring me! I have also tried removing the dots so that I can place labels instead, but when I type in type="n" it ignores that too!
I am using the exact same format of code that I have used for other plots but this time it just isn't cooperating.
If I specify other features such as the title or the x/y axis labels, it will add those in but it simply ignores the pch or type commands.
This is my basic code:
plot(Differences ~ Time, data=subsetH)
But if I run
plot(Differences ~ Time, type="n", data=subsetH)
or
plot(Differences ~ Time, pch=2, data=subsetH)
it keeps plotting the same thing.
Is there something obvious I have missed?
I just came across your question because I encountered the same thing - creating an empty plot did not work, as type='n' was always ignored (as well as other type specifications).
With the help of this entry: Plotting time-series with Date labels on x-axis
I realized that my date format needed to be assigned as "date" class (as.Date()).
I know your entry dates back a little bit already, but maybe it's still useful.
I am using forest.rma in the R package metafor. I want to use the xlim argument to specify the width of the plot. But this gets overridden when one of the x-values is outside that range. Here are two examples:
library(metafor)
par(mfrow=c(2,1))
rmaObject <- rma.uni(yi=c(1.3, -16), sei=c(0.8, 0.7))
forest(rmaObject, atransf=exp, xlim=c(-5,12), alim=c(-5,12))$xlim
forest(rmaObject, atransf=exp, xlim=c(-5,12))$xlim
In both cases I think the function should follow my instruction to use xlim=c(-5,12) and draw just a left arrowhead for the second bar, to show that the whole of the bar lies outside the left of the plot. But as the console output shows the first one sets xlim to c(-16,12) and the second one sets it to c(-20,12).
In other words forest forces xlim to encompass both bars completely, but I don't think it should do that if I specify xlim, and especially not if I specify alim as well. Is there an easy way for me to fix this?
The numbers in this example are real, though I have omitted most of my studies (i.e. values of yi and sei), which are mostly in the area of 0 or 1.
You can install the development version of metafor as described here:
http://www.metafor-project.org/doku.php/installation#development_version
It deals with this better. It still forces xlim to encompass the yi values, but no longer for alim. The summary polygon at the bottom still looks like crap though (I haven't built in any code to cut off the polygon shape at the alim values -- not as trivial as it may seem), but this may not be an issue once you add the rest of the studies.
Assume I want to plot the following dataframe:
df <- data.frame(expand.grid(1:10,1:10),rep(10,100))
colnames(df) <- c("x","y","z")
with the lattice wireframe() function:
wireframe(z~x*y,df,colorkey=TRUE,drape=TRUE)
How do I get it to plot the given coordinates? I would assume it has something to do with having to scale/adjust the z-axis as the automatic scaling within wireframe is probably confused by all z-coordinates being equal.
This is from the help page scales section: "The most common use for this argument is to set arrows=FALSE, which causes tick marks and labels to be used instead of arrows being drawn (the default)." So just add that as a list value to 'scales':
wireframe(z~x*y,df,colorkey=TRUE,drape=TRUE,
scales=list(arrows=FALSE), zlim=c(0,10.1))
The failure of wireframe to display anything when the plotted plane is at one of the extremes seems to be at least "unexpected behavior" if not a bug. I suspect you would not see this in real data. Your use of drape doesn't make much sense since the entire data-plane plane gets displayed at the white midpoint. (Again this is probably not a problem if you have something other than this pathological example.)
Just add a zlim argument.
wireframe(z~x*y,df,colorkey=TRUE,drape=TRUE, zlim=c(0,20))
Is there an function in R that does the same job as Matlab's "bar" function?
R does have a "barplot" function in the library graphics, however, it is not the same.
The Matlab bar(X,Y) (verbatim excerpt from MATLAB documentation) "draws a bar for each element in Y at locations specified in X, where X is a vector defining the x-axis intervals for the vertical bars." (emphasis mine)
However, the R barplot function does not allow one to specify locations.
Perhaps there is a method in ggplot2 that supports this? I am only able to find standard bar charts in ggplot2.
No, barplot is not the same as bar, but you should read the whole help. You can do many things to position the bars. The first is simply their order in Y. You could insert spaces if you wish (additional 0s). If you have X and Y then sort Y on X (Y[order(X)]) and plot it. If you need to change positions use the "space" and "width" arguments. It's not as straightforward as specifying X values I suppose but it's definitely more useful in most situations. Generally what you want to adjust is widths of bars and spaces between bars. Their position on the X-axis should be arbitrary. If the position on the X-axis is really meaningful then you should be using line plots, not bar graphs.
In R:
barplot(rbind(1:10, 2:11), beside=T, names.arg=1:10)
In MATLAB:
>> bar(1:10, [(1:10)' (2:11)'])
Read up on par . Then observe, for example:
x<-c(1,2,4,5,6)
y<-c(3,4,3,4,2)
plot(x,y,type='h',lwd=6)
Edit: yes, I know this doesn't (yet) plot multiple data sets, but I would hope you can see simple ways to make that happen, with spacings, colors, etc. specified to your exact liking :-)
Sounds vaguely like the R stepfun. On the other hand one would need to know what "draws a bar" means before saying it is not the same as barplot(..., horiz=TRUE) One would, of course, need to examine some more detailed evidence such as data and plots before arriving at a conclusion, however. #John Colby should be congratulated for adding some specificity to the discussion. The axis function is probably what Quant Guy needs education regarding.