Changing X-axis position in R barplot - r

I have two values of averages LR50<-(424.8, 425.7). I want to plot these as a barplot barplot(LR50). Now, I don't need any of the information except from ylim=c(424,426.5). When I change the x-axis axis(1,at=c(0,10), pos=424) there is still bar plot that falls below the axis.
How do I get the barplot to only plot from the new x-axis at y=424 and up?

You will need to use the xpd parameter (and set it to FALSE), this will clip the plot to the plotting region.
barplot(LR50,ylim = c(424,426.5),xpd=FALSE)
axis(1,at=c(0,10),pos=424)

Related

Swap Axes of Violin plot

I am trying to create Violin Plots using the StatsPlots.jl library.
However, I would like to have the returned Violin plot to be horizontal instead of vertical as I want to show the distribution of a variable (e.g. Temperature) for different heights, eg. at 1000m, 2000m, 3000m ...
So it would be nice if the height was at the y-Axis while the temperature distribution was on the x-Axis.
Is there a way to swap the axes of a Plots.Plot struct, or is there an argument I could pass to violin() that does the trick?

How to add frequency & percentage on the same histogram in R?

Consider the following data set.
x <- c(2,2,2,4,4,4,4,5,5,7,7,8,8,9,10,10,1,1,0,2,3,3,5,6)
hist(x, nclass=10)
I want to have a histogram where the x-axis indicates the intervals & the y-axis on the left represents the frequency. In addition to this, I need another y-axis on the right side of the histogram representing the percentage of the intervals on the same plot. Even though the following graph is for two variables, more or less it looks like what I need (taken from Histogram of two variables in R).
Thanks in advance!
You can add a vertical axis to the right side with axis(4,at = at), where at are points at which tick-marks are to be drawn. If you want the density values of your histogram as tick-marks, call axis(4, at = hist(x,nclass=10)$density).

R plot and barplot how to fix ylim not alike?

I try to use base R to plot a time series as a bar plot and as ordinary line plot. I try to write a flexible function to draw such a plot and would like to draw the plots without axes and then add universal axis manually.
Now, I hampered by strange problem: same ylim values result into different axes. Consider the following example:
data(presidents)
# shorten this series a bit
pw <- window(presidents,start=c(1965))
barplot(t(pw),ylim = c(0,80))
par(new=T)
plot(pw,ylim = c(0,80),col="blue",lwd=3)
I intentionally plot y-axes coming from both plots here to show it's not the same. I know I can achieve the intended result by plotting a bar plot first and then add lines using x and y args of lines.
But the I am looking for flexible solution that let's you add lines to barplots like you add lines to points or other line plots. So is there a way to make sure y-axes are the same?
EDIT: also adding the usr parameter to par doesn't help me here.
par(new=T,usr = par("usr"))
Add yaxs="i" to your lineplot. Like this:
plot(pw,ylim = c(0,80),col="blue",lwd=3, yaxs="i")
R start barplots at y=0, while line plots won't. This is to make sure that you see a line if it happens that your data is y=0, otherwise it aligns with the x axis line.

Y-Axis positions of barplot and base plot do not match

I was trying to plot a climate diagram and ran into the following problem:
After using barplot(...) for precipitation I superimposed another plot for the temperature. It is necessary for climate diagrams that the two y-axes (mm, °C) align at zero and that the precipitation/temperature ratio is 2:1 (e.g. 20mm precipitation corresponds to 10°C).
The problem: barplot(...) draws the axis to the plot's box while plot(...) leaves some space between the box and the axis margins.
Here is a simplified example. From the grid lines you see that the 0-values do not align:
barplot(0:10)
grid(col=1)
par(new=TRUE)
plot(0:10, xlim=c(-2,14), axes=FALSE)
axis(4,at=c(0:10), labels=c(0:10))
How can I get the right position and scaling of the two axes?
Don't use par(new = TRUE):
barplot(0:10)
grid(col=1)
lines(0:10, type = "p")
axis(4,at = c(0:10), labels = seq(0,20,2))
The function lines() is the right one here. The argument type = p is needed to plot points.
You need to adjust the y-values for the temperature, but now the second y-axis is in the right way, I think.

Histogram bars exceed chart area in R

I am creating a histogram with the following line:
hist(mydata$freq2,col="lightgreen")
This produces the image below:
I would like the bars to stay within the chart area. Why doesn't R increase the values of the X and Y axis, and how can I increase these values manually?
The bars to stay in the chart area. R calculates the axis dimensions based on your data and with default parameters even extends it a bit.
The axis with its labels is drawn for the boxplot only inside the label range.
If you draw a box around the figure, you will see that the plot uses up the space always the same disregarding of your data. So it is not the bars going outside the chart but the axis being restricted to the labels.
set.seed(12345)
par(mfrow=c(2,2))
plot_random_hist <- function() {
hist(rbeta(100,1,8)*runif(1))
# plot a box to illustrate the plot area
box(col="red")
}
replicate(4, plot_random_hist() )
Have a look at par("usr") to query the dimensions of your plot in user coordinates.
If you need to control the length of the axis and the ticks/labels you can use the axis command and suppress automatic axis in your hist call.
set.seed(12345)
hist(rbeta(100,1,8),yaxt="n")
at <- c(0,10,30,par("usr")[4])
axis(2,at=at,labels=round(at))

Resources