I have a plot() that I'm trying to make, but I do not want the x-values to be used as the axis labels...I want a different character vector that I want to use as labels, in the standard way: Use as many as will fit, drop the others, etc. What should I pass to plot() to make this happen?
For example, consider
d <- data.frame(x=1:5,y=10:15,x.names=c('a','b','c','d','e'))
In barplot, I would pass barplot(height=d$y,names.arg=d$x.names), but in this case the actual x-values are important. So I would like an analog such as plot(x=d$x,y=d$y,type='l',names.arg=d$x.names), but that does not work.
I think you want to first suppress the labels on the x axis with the xaxt="n" option:
plot(flow~factor(month),xlab="Month",ylab="Total Flow per Month",ylim=c(0,55000), xaxt="n")
then use the axis command to add in your own labels. This example assumes the labels are in an object called month.name
axis(1, at=1:12, labels=month.name)
I had to look up how to do this and I stole the example from here.
Related
Here is a plot generated by julia's Plots library, using the xaxis=:log attribute:
The plot has evenly spaced tick marks, with labels like 10^0.25. This might be useful sometimes, but I find it a bit confusing for this plot, because most people don't know the value of 10^0.25 without looking it up.
I would prefer the x axis to have logarithmically spaced ticks, representing uniform intervals of the quantity on the x axis. Here's a quick example to show what I mean, generated using semilogx in Python's matplotlib lirary:
In this plot the x axis has ticks at x=1, x=2, x=3 etc., which I find more useful for the figures I'm generating. Can this be achieved in julia using Plots?
As it has been told in the discourse topic, the default tick behavior (for most backends) is different than what you expected.
There is a way you can achieve the tick behaviour you want. You can manually set tick positions and tick labels through xticks (yticks or zticks) argument.
For example the following snippet will put ticks at equally spaced points as you wanted in your post.
x = 1:0.1:10;
y = rand(length(x));
plot(x, y, xscale=:log10, xticks=(1:10, 1:10))
The first element of the tuple is for the location of the ticks and the second is for the labels. Although I used a range object for labels, you can use array of Strings or LaTeXStrings etc.
You might also want to take a look at minorticks (xminorticks for your case) and minorgrid attributes in the Julia Plots documentation.
When I use plot() to plot a time serious variable, it only shows dots. I use second code, lines(), to link all the dots. Is this really necessary? Or I did something wrong...
The data is as following. I use the plot() and lines() to draw the graph to see the trend.
YYYYMM<-c("200907","200908","200909","200910","200911","200912","201001","201002","201003","201004","201005","201006","201007","201008","201009","201010","201011","201012","201101","201102","201103","201104","201105","201106")
a<-c(1158,1455,1134,1371,1352,1277,1408,1270,1000,1462,1419,0,0,0,0,0,0,0,0,0,0,0,0,0)
a_number_trend<-data.frame(YYYYMM,a)
a_number_trend
plot(a_number_trend$YYYYMM,a_number_trend$a,las=2,type="l",col="blue")
lines(a_number_trend$YYYYMM,a_number_trend$a,las=2,type="l",col="blue")
The plot is like this at beginning.
Then become this.
But I want the line only without the short bar. Or to change the short bars into points.
Convert your YYYYMM column to an actual R ?Date object. Then you can get everything lining up properly:
a_number_trend$date <- as.Date(
paste0(a_number_trend$YYYYMM,"01"),
format="%Y%m%d"
)
plot(a ~ date, data=a_number_trend, type="l", xaxt="n", ann=FALSE)
The below axis is not stricly necessary (remove xaxt="n" above if you want the default Date axis calculations instead).
axis.Date(
1,
at=seq(min(a_number_trend$date), max(a_number_trend$date), by="1 month"),
format="%Y%m",
las=2
)
I want to break the x-axis of a plot of a cumulative distribution function for which I use the function plot.stepfun, but don't seem to be able to figure out how.
Here's some example data:
set.seed(1)
x <- sample(seq(1,20,0.01),300,replace=TRUE)
Then I use the function ecdf to get the empirical cumulative distribution function of x:
x.cdf <- ecdf(x)
And I change the class of x.cdf to stepfun, because I prefer to call plot.stepfun directly over using plot.ecdf (which also uses plot.stepfun, but has fewer possibilities to customize the plot).
class(x.cdf) <- "stepfun"
Then I am able to create a plot as follows:
plot(x.cdf, do.point=FALSE)
But now I want to break up the x-axis between 12 and 20, e.g. using axis.break [plotrix-library] such as here, but since I have no ordinary x and y-argument for plotting, I don't know how to do this.
Any help would be very much appreciated!
"Breaking the axis between 12 and 20" doesn't make a lot of sense to me since 20 is the end of the x range, so I will exemplify breaking it between 12 and 15. The plotrix.axis.break function doesn't actually do very much (as can be seen if you step through that example.) All it does is put a couple of slashes at a particular location, the "breakpos". All the rest of the work needs to be done with regular plotting functions and plot.stepfun isn't really set up to do it, so I'm using regular plot.default with the type="s" argument. You need to do the offsetting of the x values, the arguments to the ecdf function and the labels in the axis arguments.
png()
plot( c(seq(1,12,0.1), seq(15,20,0.1)-3), # Supply the range, shifted
x.cdf(c(seq(1,12,0.1), seq(15,20,0.1))), # calc domain values, not shifted
type="s", xaxt="n", xlab="X", ylab="Quantile")
axis(1, at=c( 1:12, (16:20)-3), labels=c(1:12, (16:20)) ) #shift x's, labels unshifted
axis.break(breakpos=12)
dev.off()
Is it possible to add more than one x-axis to a plot in R? And to put an annotation next to each scale?
Edit > here's the result of Nick Sabbe idea. For the annotation (a little text at the left of each axis), is it possible ?
You can use the line argument of axis() to place an axis higher or lower, this way you can make multiple axes. With mtext() you can then add a label to the side. Do note that the plot itself is only on one scale so you need to rescale the points and labels of the other scale accordingly:
# Plot and first axis:
plot(1:10,1:10,bty="n",col="red",pch=16,axes=FALSE,xlab="",ylab="")
axis(2,0:11,las=1)
axis(1,0:11,line=1,col="red",col.ticks="red",col.axis="red")
mtext("Label 1",1,line=1,at=0.2,col="red")
# Secondary points and axis:
points(rnorm(10,50,20)/10, rnorm(10,5,2),pch=16, col="blue" )
axis(1,0:11,labels=0:11*10,line=3,col="blue",col.ticks="blue",col.axis="blue")
mtext("Label 2",1,line=3,at=0.2,col="blue")
You can use ?axis for that. Parameter at is in the scale of the original axis of the plot, and you can pass labels to show other values.
You have to scale the axess labels yourself, though.
A very simple/silly example:
plot(1:10,1:10)
axis(side=4, at=c(3,7), labels=c(30,70))
Finally, note that most people consider adding multiple axes to a plot bad form...
I am trying to make a simple barplot but i have a problem that I have duplicated names on x-axis. So when ever I am trying to write names on x-axis it does not show complete string. I have following data
x <- c(1.8405917,0.3265986,1.5723623,464.7370299,0.0000000,3.2235716,
3.1223534, 7.0999787, 1.7122258,3.2005524,3.7531266,469.4436828)
and I am using barplot
barplot(x,xlab=c("AA/AA","AA/CC","AA/AC","AA/NC","CC/AA","CC/CC","CC/AC",
"CC/NC","AC/AA","AC/CC","AC/AC","AC/NC"))
But it does not work. I also used
axis()
But it does not work as well.
Thanks in advance.
No, xlab is for providing a label for the entire x-axis of the plot, not for labelling the individual bars.
barplot() takes the labels for the bars from the names of the vector plotted (or something that can be derived into a set of names).
> names(x) <- c("AA/AA", "AA/CC", "AA/AC", "AA/NC", "CC/AA", "CC/CC", "CC/AC",
+ "CC/NC", "AC/AA", "AC/CC", "AC/AC", "AC/NC")
> barplot(x)
> ## or with labels rotated, see ?par
> barplot(x, las = 2)
Edit: As #Aaron mentions, barplot() also has a names.arg to supply the labels for the bars. This is what ?barplot has to say:
names.arg: a vector of names to be plotted below each bar or group of
bars. If this argument is omitted, then the names are taken
from the names attribute of height if this is a vector,
or the column names if it is a matrix.
Which explains the default behaviour if names.arg is not supplied - which is to take the names from the object plotted. Which usage is most useful for you will mainly be a matter of taste. Not having the row/column/names might speed code up slightly, but many of R's functions will take the names attribute (or similar, e.g. row names) directly from objects so you don't have to keep providing labels for plotting/labelling of results etc.
xlab should be names.arg. See ?barplot for details.
The way to use axis() is to capture the midpoints, which is what the barplot function returns. See ?barplot:
mids <- barplot(x, xlab="")
axis(1, at=mids, labels=c("AA/AA","AA/CC","AA/AC","AA/NC","CC/AA","CC/CC",
"CC/AC","CC/NC","AC/AA","AC/CC","AC/AC","AC/NC"),
las=3)
Try this:
barplot(x, cex.names=0.7,
names.arg=c("AA/AA","AA/CC","AA/AC","AA/NC","CC/AA","CC/CC","CC/AC",
"CC/NC","AC/AA","AC/CC","AC/AC","AC/NC"))