How to get equally scaled axes with Plots in Julia - plot

I have created a heatmap plot using in Julia by using the Plotly with the Plots package. I generate the heatmap with the following command heatmap(10^9 .* (height + deformation)). Then, I get a plot that looks like this
The length of the x-axis and y-axis both range from 0 to 256, but nonetheless they do not have the same scale as seen from the rectangular shape of the heatmap. How can I make the scale of the x-axis and y-axis equal?

You can use aspect_ratio attribute with :equal option.
heatmap(10^9 .* (height + deformation), aspect_ratio=:equal)
should give you equally scaled x and y axes. If you give a number instead of :equal, plot area is resized so that 1 y-unit is the same size as aspect_ratio x-units.
You can see other attributes to use with Plots.jl in the relevant section of Plots.jl documentation.

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?

R: Matching x-axis scales on upper and lower plot using layout with base graphics

I am trying to arrange 3 plots together. All 3 plots have the same y axis scale, but the third plot has a longer x axis than the other two. I would like to arrange the first two plots side by side in the first row and then place the third plot on the second row aligned to the right. Ideally I would like the third plot's x values to align with plot 2 for the full extent of plot 2 and then continue on below plot one. I have seen some other postings about using the layout function to reach this general configuration (Arrange plots in a layout which cannot be achieved by 'par(mfrow ='), but I haven't found anything on fine tuning the plots so that the scales match. Below is a crappy picture that should be able to get the general idea across.
I thought you could do this by using par("plt"), which returns the coordinates of the plot region as a fraction of the total figure region, to programmatically calculate how much horizontal space to allocate to the bottom plot. But even when using this method, manual adjustments are necessary. Here's what I've got for now.
First, set the plot margins to be a bit thinner than the default. Also, las=1 rotates the y-axis labels to be horizontal, and xaxs="i" (default is "r") sets automatic x-axis padding to zero. Instead, we'll set the amount of padding we want when we create the plots.
par(mar=c(3,3,0.5,0.5), las=1, xaxs="i")
Some fake data:
dat1=data.frame(x=seq(-5000,-2500,length=100), y=seq(-0.2,0.6,length=100))
dat2=data.frame(x=seq(-6000,-2500,length=100), y=seq(-0.2,0.6,length=100))
Create a layout matrix:
# Coordinates of plot region as a fraction of the total figure region
# Order c(x1, x2, y1, y2)
pdim = par("plt")
# Constant padding value for left and right ends of x-axis
pad = 0.04*diff(range(dat1$x))
# If total width of the two top plots is 2 units, then the width of the
# bottom right plot is:
p3w = diff(pdim[1:2]) * (diff(range(dat2$x)) + 2*pad)/(diff(range(dat1$x)) + 2*pad) +
2*(1-pdim[2]) + pdim[1]
# Create a layout matrix with 200 "slots"
n=200
# Adjustable parameter for fine tuning to get top and bottom plot lined up
nudge=2
# Number of slots needed for the bottom right plot
l = round(p3w/2 * n) - nudge
# Create layout matrix
layout(matrix(c(rep(1:2, each=0.5*n), rep(4:3,c(n - l, l))), nrow=2, byrow=TRUE))
Now create the graphs: The two calls to abline are just to show us whether the graphs' x-axes line up. If not, we'll change the nudge parameter and run the code again. Once we've got the layout we want, we can run all the code one final time without the calls to abline.
# Plot first two graphs
with(dat1, plot(x,y, xlim=range(dat1$x) + c(-pad,pad)))
with(dat1, plot(x,y, xlim=range(dat1$x) + c(-pad,pad)))
abline(v=-5000, xpd=TRUE, col="red")
# Lower right plot
plot(dat2, xaxt="n", xlim=range(dat2$x) + c(-pad,pad))
abline(v=-5000, xpd=TRUE, col="blue")
axis(1, at=seq(-6000,-2500,500))
Here's what we get with nudge=2. Note the plots are lined up, but this is also affected by the pixel size of the saved plot (for png files), and I adjusted the size to get the upper and lower plots exactly lined up.
I would have thought that casting all the quantities in ratios that are relative to the plot area (by using par("plt")) would have both ensured that the upper and lower plots lined up and that they would stay lined up regardless of the number of pixels in the final image. But I must be missing something about how base graphics work or perhaps I've messed up a calculation (or both). In any case, I hope this helps you get the plot layout you wanted.

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

How to expand X axis to include arrows in stick plot (oce)

I'm creating a stick plot using the "plotSticks" function in the oce library. The arrows that are created in the stick plot often go beyond the limits of the x axis. In my case, the x axis is usually a date range that I can't adjust to fit the data.
As you can see in this plot:
library(oce)
x<-1:10
u<-c(0,1,2,3,4,5,6,7,8,9)
v<-c(0,1,2,1,2,3,2,1,2,3)
plotSticks(x,0,u,v)
The ends of the arrows go beyond the limits of my x axis. Is there a way to include some space on either side of the x axis so that the tips of the arrows can be seen?
The plotSticks has a yscale function that can reduce the size of the arrows, but then the arrows do not match the y axis scale so I'd like to avoid using the yscale fix.
Will this get you the result you want? It extends the x axis and uses the yscale argument.
library(oce)
x<-1:10
u<-c(0,1,2,3,4,5,6,7,8,9)
v<-c(0,1,2,1,2,3,2,1,2,3)
plotSticks(x,0,u,v, xlim=c(0, 40), ylim=c(-.5, 4), yscale=2)

How to create side-by-side bar charts (for multiple series) with ggplot?

I have two sets of data (3 columns: x=categorical, y = numerical, l = location) and I would like to create a bar chart with the categories on the x axis and, for each value of the category, two vertical bars, coloured differently, with the y values for each location. By the default, Excel/OpenOffice produce this kind of chart.
I tried
qplot (x,y,data=mydata,col=location, geom="histogram")
but it produces stacked bars, not side by side. I then looked in the ggplot2 documentation and didn't find any other geom I could use (see below for full list).
Is this not possible with ggplot2?
Thanks in advance.
Name Description
abline - Line, specified by slope and intercept
area - Area plots
bar - Bars, rectangles with bases on y-axis
blank - Blank, draws nothing
boxplot - Box-and-whisker plot
contour - Display contours of a 3d surface in 2d
crossbar - Hollow bar with middle indicated by horizontal line
density - Display a smooth density estimate
density_2d - Contours from a 2d density estimate
errorbar - Error bars
histogram - Histogram
hline - Line, horizontal
interval - Base for all interval (range) geoms
jitter - Points, jittered to reduce overplotting
line - Connect observations, in order of x value
linerange - An interval represented by a vertical line
path - Connect observations, in original order
point - Points, as for a scatterplot
pointrange - An interval represented by a vertical line, with a point
in the middle
polygon - Polygon, a filled path
quantile - Add quantile lines from a quantile regression
ribbon - Ribbons, y range with continuous x values
rug - Marginal rug plots
segment - Single line segments
smooth - Add a smoothed condition mean
step - Connect observations by stairs
text - Textual annotations
tile - Tile plot as densely as possible, assuming that every tile is the same size
vline - Line, vertical
There is a position argument that defaults to stack here. Use:
qplot (x,y,data=mydata,col=location, geom="bar", position="dodge")
It is in the manual, just search for "dodge". Also, you probably want a "bar" geom if the y values give the height of the bar.

Resources