Sorry if the question is quite naive but I am stuck and could not find a specific answer elsewhere. I am a beginner. I cannot make the vertical axis show in the plot explained below.
barplot(-(seq(0,10,2)), width=0.6, space=1, xlim=c(-12,12), ylim=c(2,10), horiz=T, axisnames=FALSE,col="green")
barplot(seq(0,10,2),add=T, width=0.6, space=1, axes=F,horiz=T,axisnames=FALSE,col="darkred")
When I try to "include the option axis.lty=1 to draw it" (quoted from statmethods.net) I get warning saying it is "no graphical parameter". Please could anyone enlighten me in using boxplot() to solve this? Thanks in advance.
see the manual of barplot:
axisnames logical. If TRUE, and if there are names.arg (see above),
the other axis is drawn (with lty = 0) and labeled.
axis.lty the graphics parameter lty applied to the axis and tick
marks of the categorical (default horizontal) axis. Note that by
default the axis is suppressed.
That axis will be drawn only when the height parameter has names attributes or you supplied names.arg. When this criteria was satisfied, a blank axis line will be drawn, and you can make it visible with axis.lty = 1. See the following example (the horizontal axis overlapped the first bar because you set ylim to c(2, 10)):
barplot(
-(seq(0,10,2)), width=0.6, space=1, names.arg = paste('h=', -(seq(0,10,2))),
xlim=c(-12,12), ylim=c(2,10),
horiz=T, axisnames=T,col="green", axis.lty = 1, las = 1)
barplot(seq(0,10,2),add=T, width=0.6, space=1,
axes=F,horiz=T,axisnames=FALSE,col="darkred", axis.lty = 1)
Related
I am trying to change the y-axis to being horizontal so that it can be read easier for my talk. I am new to R and don't have a lot of experience with it.
This is my code:
first.value = pt(500, 1.127344, 1.561328)
space = (1 - first.value)/648
edges = seq(first.value + space, 1 - space, space)
value.edges = 1.561328*qt(edges, 1.127344)
data = read.table(file.choose())
observed = log(data$V1)
expected = log(value.edges)
plot(observed,expected, xlim =c(4,15), ylim = c(0,14), las =1)
abline(0,1)
I would like the word expected the same orientation as observed. Thank you!
Try this,
par(mar = c(6,6,6,3))
plot(1, las=1, ylab="")
mtext(text="Test", side=2, line=4, las=1)
You can change the values in par to your liking, put your data instead of the 1 in plot, add your x labels etc... Then use mtext to write the yaxis label
I'm tinkering with cex options in base R plots to resize my plot for a presentation slide. I seem to be able to tweak most size aspects, but I notice the outline color of my pch point is not getting any bolder/thicker when the plot point becomes larger. So the larger the plot point, the less noticeable the outline color.
Found many websites (& SO posts) on various cex options (cex, cex.main, cex.sub, cex.axis, cex.lab), but none seem to be adjusting the pch plot point.
I'm aware that only certain pch symbols (21 to 25) can be used with fill color and outline. My sample code uses 21 (circle).
data("mtcars") # test data
summary(mtcars[c("hp","mpg")]) # to find on min & max values to set plot limits
# set general features used for multiple plots
par(bg="blue", fg="red", col="yellow", col.axis="white", col.lab="white", bty="n", cex=1.5)
# test plot to illustrate, cex used here to further adjust plot points from par setting
plot(mtcars$hp, mtcars$mpg, ylim=c(10, 35), xlim=c(50, 340), pch=21, bg="red", cex=2)
Am I missing something about cex or is there a different solution without turning to ggplot? I'm not adverse to ggplot if there is an option, but I'd like to see if it can be done in base R.
You need to specify lwd to change the border thickness of pch symbol
plot(1:10, 1:10, pch = 21, cex = 3, lwd = 1:10)
I am trying to Plot the K-S Test result. Basically b\w the Significant and Insignificant variables. I need to Plot the legend, and the axis values in visible manner. If you can see the below plot, the Y- Axis values are not visible. Also the graph's Legend displays a dotted line. I need a solid squares or circle for Legend.
barplot(matrix, main="KS-Significance Test",
xlab="Proportion b/w InSig vs Sig",
ylab = 'Combinations',
yaxt="n",ylim=c(0,5),
cex.axis=0.8,cex.lab = 0.8,font = 2, horiz=TRUE,
col=c("darkgreen","darkred"))
legend('topright',legend= c("Insignificant","Significant"),
col= c("darkgreen","darkred"), lty = c(11,11),cex=0.8))
Please find the matrix data below by clicking the hyperlink.
input data matrix image
Also attaching the image of my barplot.
Use fill instead of col and do not specify a line type lty if you do not want lines but box shapes -- i.e. wider lines that match your barplots :)
legend('topright',legend= c("Insignificant","Significant"),
fill = c("darkgreen","darkred"), cex=0.8)
Otherwise, specify a line width lwd, here I use a solid line lty=1 and lwd=10 to get darker and wider lines.
legend('topright',legend= c("Insignificant","Significant"),
col = c("darkgreen","darkred"), lty = 1, lwd = 10, cex = 0.8)
Thanks Everyone. I have found the answer. Legend issues are also resolved.
here is the code.
barplot(matrix, main="KS-Significance Test",
xlab="Proportion b/w InSig vs Sig",
ylab = 'Combinations',
ylim=c(0,270),
cex.axis=0.8,cex.lab = 0.8,font = 4,
names.arg = c("Cohort 2014-2015", "Cohort 2014-2016", "Cohort 2015-2016","Cohort 2015-2017","Cohort 2016-2017"),
#horiz=TRUE,
col=c("darkgreen","darkred"),
legend = c("Insignificant","Significant"))
This completely satisfies my plot.
I want to omit the x-axis labels for my plot of means, but I just fail (I didn't use packages such as ggplot2):
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="")) # normal code
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="",frame=F)) # which gives no response
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="",axes=F))
This gives the following error:
Error in plot.default(c(1, n.levs), yrange, type = "n", xlab = xlab, ylab = ylab, :
formal argument "axes" matched by multiple actual arguments
The below code temporarily sets the color of axis tick labels (i.e., for both the x and y-axis) to "transparent" using par, thus preventing the automatically drawn tick labels from being visible. The y-axis tick labels can easily be inserted afterwards by reverting the par-related modifications.
## sample data
library(Rcmdr)
data(Moore)
## create plot with transparent (i.e., non-visible) tick labels
par(col.axis = "transparent")
with(Moore, plotMeans(conformity, fcategory, partner.status,
ylim = c(0, 25)))
## add y-axis tick labels
par(col.axis = "black")
axis(side = 2, at = seq(0, 25, 5), tick = FALSE)
Remember that this is a rather undesired behavior of plotMeans since the help pages explicitly say that ... are arguments to be passed on to plot. Therefore, if your target is to entirely disable the drawing of the frame and axes when using plotMeans, e.g. similar to the visual output of plot(1:10, 1:10, axes = FALSE), you should probably file a bug report.
Update
In case I understood it wrongly and you actually wanted to remove both the tick labels and the ticks, you could simply use
par(xaxt = "n")
with(Moore, plotMeans(conformity, fcategory, partner.status,
ylim = c(0, 25)))
See also ?par for further possible modifications of R base plots.
Is it possible to put tick labels of only x-axis inside plotting area?
I am trying:
axis(1,at=c(0:71),c(rep(0:23,3)),cex.axis=.7, font=1,tck=.01)
It seems that:
par(mgp=c(0,-1.4, 0))
puts both x and y tick labels inside plotting area.
Why don't you just draw the ticks where you want them using the pos argument to axis():
plot(0:72, xaxt="n")
text(0:71, -1, rep(0:23, 3), cex = 0.5)
axis(1, at=c(0:71), NA, cex.axis=.7, font=1, tck=.01)
I think the best and easy solution is the parameter tcl in axis or par.
Positive values put the marks inside, negative outside, the value is the length.
Your example:
axis(1,at=c(0:71),c(rep(0:23,3)),cex.axis=.7, font=1,tcl=0.3)