I have made my own data set for a student project I'm working on and I am attempting to plot a graph to show employment outcomes, the number employed, in different regions. I am new to Rstudio/data in general and a technophobe! I am struggling to get my Y axis to show the numbers as normal numbers, not exponential notation (if that's what it's called 6e+04 etc?)
Further to this, the X axis is only choosing certain regions to display, more if I expand it. How can I fix this to show all the regions?
Thanks so much in advance for any help!
I have tried using ylim=c(ymin=0, ymax=100000) in the plot. (Info, max value for employed is just below 100000). But this made no change to the plot.
employment.region$Region<- factor(employment.region$Employed, labels=c("North East","North West","Yorkshire","East Midlands","West Midlands","East of England","London","South East","South West","Wales","Scotland"))
plot(employment.region$Region,employment.region$Employed, ylim=c(ymin=0, ymax=100000), frame= FALSE)
AND
plot(employment.region$Employed~employment.region$Region, ylim=c(ymin=0, ymax=100000))
Plot without a y axis, then set the tick marks and the axis labels by hand. This is why yticks and yaxis_labs are defined.
Then plot, with las = 2 to have the axis' annotations perpendicular to the axis.
yticks <- seq(0, 100000, by = 2e4)
yaxis_labs <- formatC(yticks, digits = 6)
old_par <- par(mar = c(7, 4, 4, 2) + 0.1)
plot(employment.region$Region, employment.region$Employed,
ylim=c(ymin=0, ymax=100000), frame= FALSE, las = 2, yaxt = "n")
axis(2, at = yticks, labels = yaxis_labs, las = 2)
par(old_par)
Related
I would like to make my y axis labels horizontal, while keeping my y axis titles as parallel.
When I try inputting las=1 into the twoor.plot()argument, nothing happens. I have also tried ylas=1, y_las=1, lylas=1, rylas=1, and nothing happens. The only way I've been able to make my yaxis labels horizontal, is by using par(las=1), but then this makes my y-axis titles horizontal too, which I don't want...
This is my code so far:
par(las=1)
yFrequency <- c(0,20,40,60,80,100,120,140,160)
GS_class_labels <- c("<2", "2-4", "4-8", "8-16", "16-32", "32-64", "64-128", "128<")
twoord.plot(data=distribution,lx="Var1",ly="Freq", ry="cum_percentile",
main="B1 Surface Grain Size Distribution",
xlim=NULL,lylim=c(0,160),rylim=NULL,lwd=1.5,
lcol=1,rcol=2,xlab="Grain Size (mm)",lytickpos=yFrequency,
ylab="Frequency",ylab.at=NA,
rytickpos=NA,rylab="Percent Finer Than (%)",rylab.at=NA,
lpch=1,rpch=2,
type="b",xtickpos=NULL,xticklab=GS_class_labels,
halfwidth=0.4,axislab.cex=1.1,
do.first=NULL,xaxt="s", yticklab=yFrequency, cex.lab=1)
An alternative way to set the y axis labels parallel is as follows.
(1) Set both of the ylab and rylab from twoord.plot to empty.
(2) Use mtext and set the parameters accordingly.
Here is the code to do that. Because you don't provide the distribution data, I use iris data just to make it possible to generate the plot.
# Emptying both of ylab and rylab
twoord.plot(data = iris,lx="Sepal.Length",ly="Petal.Width", ry="Sepal.Width",
main="B1 Surface Grain Size Distribution",
xlim=NULL,lylim=c(0,160),rylim=NULL,lwd=1.5,
lcol=1,rcol=2,xlab="Grain Size (mm)",lytickpos=yFrequency,
ylab="",ylab.at=NA,
rytickpos=NA,rylab="",rylab.at=NA,
lpch=1,rpch=2,
type="b",xtickpos=NULL,xticklab=GS_class_labels,
halfwidth=0.4,axislab.cex=1.1,
do.first=NULL,xaxt="n",yaxt="n", #yticklab=yFrequency,
cex.lab=1)
# Assign the previous labels of ylab and rylab to the *text* parameter of *mtext*.
# side = 2 means the left side. side = 4 means the right side.
# las = 0 is the parallel style of the text.
# line shows the distance of the text from the y axis.
mtext(text = "Frequency", side = 2, las = 0, line = 2.5)
mtext(text = "Percent Finer Than (%)", side = 4, las = 0, line = 0.5)
The resulted plot:
I was wondering if it's possible to get a two sided barplot (e.g. Two sided bar plot ordered by date) that shows above Data A and below Data B of each X-Value.
Data A would be for example the age of a person and Data B the size of the same person. The problem with this and the main difference to the examples above: A and B have obviously totally different units/ylims.
Example:
X = c("Anna","Manuel","Laura","Jeanne") # Name of the Person
A = c(12,18,22,10) # Age in years
B = c(112,186,165,120) # Size in cm
Any ideas how to solve this? I don't mind a horizontal or a vertical solution.
Thank you very much!
Here's code that gets you a solid draft of what I think you want using barplot from base R. I'm just making one series negative for the plotting, then manually setting the labels in axis to reference the original (positive) values. You have to make a choice about how to scale the two series so the comparison is still informative. I did that here by dividing height in cm by 10, which produces a range similar to the range for years.
# plot the first series, but manually set the range of the y-axis to set up the
# plotting of the other series. Set axes = FALSE so you can get the y-axis
# with labels you want in a later step.
barplot(A, ylim = c(-25, 25), axes = FALSE)
# plot the second series, making whatever transformations you need as you go. Use
# add = TRUE to add it to the first plot; use names.arg to get X as labels; and
# repeat axes = FALSE so you don't get an axis here, either.
barplot(-B/10, add = TRUE, names.arg = X, axes = FALSE)
# add a line for the x-axis if you want one
abline(h = 0)
# now add a y-axis with labels that makes sense. I set lwd = 0 so you just
# get the labels, no line.
axis(2, lwd = 0, tick = FALSE, at = seq(-20,20,5),
labels = c(rev(seq(0,200,50)), seq(5,20,5)), las = 2)
# now add y-axis labels
mtext("age (years)", 2, line = 3, at = 12.5)
mtext("height (cm)", 2, line = 3, at = -12.5)
Result with par(mai = c(0.5, 1, 0.25, 0.25)):
Hi I guess that I have quite a rudimentary question here.
I have a plot like this
but as you could easily notice, some of the label could not be displayed (some are overlapped with the symbols, some are just out of the figure frame)
I noticed that there are some way to adjust the position of labels
text(tsne_out$Y[,1], tsne_out$Y[,2], labels=samplegrouptry, pos=1)
for example, I could specify the the value of "pos" (from 1 to 4). I guess they are good enough in most cases .But I wonder whether there are some better ways to do that.
Any suggestion, thanks!
Following the suggestion from
vas_u Through change the axis ranges as well as "pos", I could get better plot:
One way around the problem would be to enlarge the axes of the plot.
Your example approximately reproduced with dummy data:
x <- rnorm(16, mean = 0)
y <- rnorm(16, mean = 1)
# Initial scatterplot with text labels out of plot area:
plot(x, y, pch = 16)
text(x, y, labels = paste("Name", 1:16), pos = 1) # Some labels outside plot area
# Second plot with the X and Y axes gently expanded:
plot(x, y, pch = 16,
xlim = 1.1*range(x),
ylim = 1.1*range(y))
text(x, y, labels = paste("Name", 1:16), pos = 1) # Labels now fit inside!
I hope this helps.
I need some help with axis labels in base R plotting, thanks in advance for any guidance!
What I need:
In R base plot() I would like to rotate my axis(3, ...) label to -90 degrees to get the following output:
(note that I have rotated the pic outside R)
Why I need it (big picture):
I am using labcurve for curve annotation and strangely enough for my data the annotation results are visually waay better if applied to the -90 degree rotated graph. After running labcurve I can rotate the resulting R-generated PDF back 90 degrees in LaTeX.
What I have tried:
#1
I know that this is governed by the las option in par with the following options:
0: always parallel to the axis [default],
1: always horizontal,
2: always perpendicular to the axis,
3: always vertical.
However, these four options available only cover the two angles 0 and 90 degrees as either of the following:
plot(x=c(0,10), y=c(0,1), type='n', xlab='',ylab='', axes=FALSE)
lines(x=c(0,7,7,10), y=c(0,0.33,0.67,1))
axis(2, at=c(0,1), labels=c('',''), las=2)
xlabels <- c('0','10')
axis(3, at=c(0,10), labels=xlabels, las=0)
or
axis(3, at=c(0,10), labels=xlabels, las=1)
axis(3, at=c(0,10), labels=xlabels, las=2)
or
axis(3, at=c(0,10), labels=xlabels, las=3)
#2:
One could think of str but according to the doc:
Note that string/character rotation via argument srt to par does not
affect the axis labels.
Thanks again!
The general procedure for creating rotated axis labels is described in R FAQ 7.27. Here's a modified example which hopefully suits your needs.
# some toy data
x <- c(0, 2, 6, 10)
y <- sample(1:4)
# Increase top margin to make room for rotated labels
par(mar = c(5, 4, 7, 2) + 0.1)
# Create plot without axis or labels
plot(x, y, type = "l", axes = FALSE, xlab = "", ylab = "")
# positions for tick marks
atx <- range(x)
aty <- range(y)
# x axis without labels
axis(side = 3, at = atx, labels = FALSE)
# y axis without labels
axis(side = 2, at = aty, labels = FALSE)
# add -90 rotated x axis labels
text(x = atx, y = par("usr")[4] + 0.25, srt = -90, adj = 1,
labels = atx, xpd = TRUE)
I want to create a graph in R using the image()-function. My x-axis has non-numerical subdivisions. The axis is divided like this: "Arctic Ocean" - "North Atlantic Ocean" - etc.
How can I add vertical lines to this axis in order to separate different groups on my axis using the abline(v = [value]) function?
This is the code I used to create the image (which works fine):
dev.new()
par(mar = c(9,9,1,1), bg = "grey90")
n.bins <- 24
image(log10(data.stand), col = colorRampPalette(blues9)(n.bins), xaxt = "n", yaxt = "n", useRaster = F)
axis(side = 2, at = 0:(n.taxa.data - 1) / (n.taxa.data - 1), labels = colnames(data.by.tax), las = 1)
axis(side = 1, at = 0:(n.iho.obis - 1) / (n.iho.obis - 1), labels = rownames(data.by.tax), las = 2, cex.axis = 0.5)
I tried implementing the vertical lines using the abline() function, but it doesn't appear on the figure.
Now my question is: how do I implement it correctly in this code? And how can I also make it appear in the figure?
Cheers!!
Since you didn't provide x and y arguments to image but only a z matrix, it took by default seq(0,1,nrow(z)) and seq(0,1,ncol(z)) as x and y values. So your vertical lines will have to be expressed in the [0,1] range. Let's say your 10 first columns out of 100 are one group then abline(v=.1) should do the trick. Of course it may be more convenient for you to declare a x and a y directly so you'll have better control on it.