Axis automatically changed? - r

I have a plot, where the lines are only in the negative range and when I plot it, the axis is automatically changed, i.e. negative larger values are going up and not down, in a normal plot. Currently I have the following plot:
But I want to have the y axis the other way round, so that negative larger values are going down and not up, I hope it is understandable what I mean.
How can I achieve this in R?
My code with my specific data is just using the normal plot() function.

As Ben Bolker said, the following has to be said:
I set the ylim range wrong, I set it like
ylim=c(-0.05,-1)
but
ylim=c(-1,-0.05)
should do what I want!

Related

qcc pareto.chart: bars are squashed - how to adjust the y axis?

I've produced a pareto.chart using the QCC package in R. In the default plot, the Y axis is scaled too large & for that reason the bars are too small. Most of the plot is wasted to empty white space. I assume this is a result of the long tail (right-skew) in the data ?
How is it possible to re-scale the Y-Axis so that the bars of the chart will be taller and more prominent (and differences between the bars more visible) ?
This is my first question and I can't post images yet. Please follow the link to an illustration of the problem:
https://www.dropbox.com/s/t8bwhmoxmwl1aic/pareto-axis.png
Thanks!
Keith
If you type help(pareto.chart) you will see there is an option ylim to specify the limits if the y-axis. If you provide some sample data I could try it out.
Otherwise, try including in your function call
pareto.chart(..., ylim=c(0,10000))
and see if that rescales your y-axis.

Intelligent Y Axis Scaling BarPlot R

I want to plot some data with barplot. Rather, I want to make a bar graph and barplot seemed the logical choice. I am plotting just fine but I was wondering if there is a way to intelligently scale the y axis to round up from the highest count.
For example I set the yaxis in this case to be 30, because I knew that Strand.22 had 27 counts in it: barplot(unlist(d), ylim=c(0,30), xlab="Forward Reverse", ylab="Counts")
In the future, I want this script to run on its own, so it would be optimal for the the Y-axis to choose it's own ylim. Short of pulling the information out of my 'd' variable I can't think of a good way to do this. Is there an easy way to do this with barplot? Would some other plotter work better? I have seen things about ggplots but it seemed super complex and I wasn't sure that it would do anything better.
EDIT: If I do not choose a ylim it picks automatically and this is what it decided was best.
I disagree with it's choice.
If you don't specify ylim, R will come up with something based on the data. (Sounds like you don't like it's choice, which is fair.)
If you specify something based on the data like:
barplot(unlist(d), ylim=c(0,1.1*max(unlist(d)))
R will draw you a plot that reflects the maximum value of data. That example just takes the maximum of your values and multiplies that by 1.1 (this could be any number) to give it a little extra height. R does something similar to this when you make a scatterplot but it handles barplots slightly differently.

Plotting straight surface with lattice::wireframe()

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))

ggplot2 ecdf behaviour seems odd

Consider the following series and cumulative plot:
x=c(0,0,0,0.5,10,1500)
qplot(x,geom='step',stat='ecdf')
This produces a graph that starts left of zero. In other words, it looks as if x has negative values. At the right it continues to the right after reaching 1500 and 100%. So, it looks as if there are x points larger than 1500.
I get what I expect when doing the whole thing manually:
xs=sort(x)
qplot(xs,1:length(xs)/length(xs),x,geom='step')
But this seems to defy the whole purpose of the stat='ecdf' shortcut.
What am I missing?
By default stat_ecdf will pad the endpoints by max(0.08 * diff(rx), median(diff(xvals))). In my answer to In R ggplot2, include stat_ecdf() endpoints (0,0) and (1,1) I give a way of working around this, but it might be a little drastic, depending on your use case.

R plot axes don't meet and data extends beyond them

I have a VERY basic plot in R, and I'd like to solve two issues. Here is the code which produces the plot:
plot(o,n,bty="n",pch=21,cex=1.5,bg="gray",xlab="y",ylab="x",lwd=2)
And, here's the plot
There are two unwanted behaviors of this plot that I'm trying to fix. And I don't know how to do either one (nor do I understand why R doesn't do these things already...)
The X and Y axes do not meet. There is a gap near the origin in this plot. I want to remove that. The axes should touch, just like any other graph.
The data extends past the axis is both the X and Y direction. This clearly is unwanted. How can I fix this without having to manually make my own axis. Seems like there should be something more intuitive here.
bty="l".
You may also want to use something like:
xlim=c(0.02, 0.24), ylim=c(0.02, 0.24)
if you don't like the default limits of your two axes.
In general, check out ?par for guidance on both of these and many other options.
Try leaving out bty="n" or replacing it by bty="L" if you really do not want a box with edges above or on the right

Resources