Using same X and Y axis for all par() plots [duplicate] - r

Lets say I have 10 observations of 200 points of integers between one and ten:
mysample = sample(rep(seq(1,10),20),10);
and I want to barplot it
barplot(table(mysample));
barplot
In this example, there are no observations of 7. Is there a quick way of telling barplot to set the x-axis range to all integers between 1 and 10, or do I have to manually edit the table?

Try
barplot(table(factor(mysample, levels=1:10)));
By using a factor, R will know which levels are "missing"

Related

Is there an option to limit the presented axis values in a ggplot with significance label without cutting them off in R

I'm analyzing numeric data with values between 1 to 7. I want to plot boxplots and show the significance across categories. My problem is that adding the labels also extends the values in the y axis. This might imply that the possible data range is up to more than 7 - which is not the best. I tried using ylim() but using it cuts off the signif labels. Is there a way to make the axis values to be 1-7, without cutting the information the should apear beyond this range?
my current plot:
when using ylim()
the desired outcome is something like that:
As mentioned in the comments, the solution is setting breaks:
gboxplot(...)+ scale_y_continuous(breaks = seq(0, 7, by = 1))

How to plot for repeating values in R

I am trying to implement an array in R but plotting same y-values for all x values. If value is NA, then it shouldn't be plotted
I tried the following plot which shows the histogram for all 10 values.
plot(c(1,2,NA,3,4,5,3,NA,2,4),type='h', ylim=c(0,4))
However, for the case below, when I try to control the y-values, the repeated values are not considered in the plot.
plot(c(1,2,NA,3,4,5,3,NA,2,4), rep(1,10),type='h', ylim=c(0,4))
Is this possible with plot function? Please suggest if the same can be done with an alternative.
Please look again at the help page of ?plot.
In your second line you plot the y value 1 at the x values 1 to 5. The plot you get is exactly the plot you asked for, which is not the plot you cared for. In the first plot, your values are interpreted as the y values, not the x values. The x values in the plot are just the indices in the first example.
If you want to get the lines not plotted at the NA values, just do:
x <- c(1,2,NA,3,4,5,3,NA,2,4)
plot(!is.na(x), type = 'h')
Now you plot a TRUE (which is a value of 1) whenever there is a value, and FALSE (which translates to 0) whenever there is none.
This is the exact same as :
xx <- ifelse(is.na(x),0,1)
plot(xx, type = 'h')
On a sidenote: Please do not call this a histogram. A histogram represents counts for bins, this doesn't even come close to that.
plot(!is.na(c(1,2,NA,3,4,5,3,NA,2,4)),type='h', ylim=c(0,4))

Is it possible to make a pirateplot using frequencies instead of densities in R?

I am searching and trying the following plot in R for ages, but nothing seems to work.
What I want is a quantitative variable in the Y axis and a categorical variable in the X axis, and just an horizontal histogram (of the Y variable) for each category.
I couldn't find a package that does this. Any suggestions?

R histogram with numbers under bars

I had some problems while trying to plot a histogram to show the frequency of every value while plotting the value as well. For example, suppose I use the following code:
x <- sample(1:10,1000,replace=T)
hist(x,label=TRUE)
The result is a plot with labels over the bar, but merging the frequencies of 1 and 2 in a single bar.
Apart from separate this bar in two others for 1 and 2, I also need to put the values under each bar.
For example, with the code above I would have the number 10 under the tick at the right margin of its bar, and I needed to plot the values right under the bars.
Is there any way to do both in a single histogram with hist function?
Thanks in advance!
Calling hist silently returns information you can use to modify the plot. You can pull out the midpoints and the heights and use that information to put the labels where you want them. You can use the pos argument in text to specify where the label should be in relation to the point (thanks #rawr)
x <- sample(1:10,1000,replace=T)
## Histogram
info <- hist(x, breaks = 0:10)
with(info, text(mids, counts, labels=counts, pos=1))

switching the place of x and y axis data in r

I have a vector of data which consists of 20,000 numbers ranging between 0 and 1, i want to plot this data where x axis is the number values and y axis is their frequencies.
|
Freq|
|
|
|______________
values
but when i use plot(vector) in R, it shows frequency on x axis named as index and number values on y.
In the arguments used by plot() function i couldn't find anything helpful.
does anybody know how i could do this?
If you want a plot of frequencies, the best type of plot to make would be a barplot and the easiest way to make a barplot is just to pass a table to barplot(). For example
barplot(table(vector))
or if you just want a needle-style plot
plot(table(vector))
would also work.
If you want to trim outliers from the table, you could try
barplot( table( vector[vector<quantile(vector, .98)] ) )
here we drop samples that are above the 98% quantile.

Resources