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.
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:
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)
I want to get rid the small margin close to zero on X and Y value (red line on pic), and plot ONLY what is showed in red square.
I tried setting par(mar = rep(0, 4) and xlim=c(0, ...), ylim=c(0, ...) but R still keeps adding this tiny margin. How to get rid of it?
EDIT:
another point of view on my problem:
after running:
require(plotrix)
axisRange <- c(0,500)
plot(NULL, xlim = axisRange, ylim=axisRange)
draw.circle(0, 0, 200, col = "white", border = "red")
I end up with a circle positioned not in "true" 0,0 point:
EDIT2:
Actually what I want to do, is to plot circles of different radius, and save it as an image. That is why I care about the margins.
I end up with something like this (spots on the corners are for the reference):
And should be more like this:
You can set the xaxs and yaxs arguments to "i" as opposed to the default of "r". From the par help page:
Style "r" (regular) first extends the data range by 4 percent at each
end and then finds an axis with pretty labels that fits within the
extended range.
Style "i" (internal) just finds an axis with pretty labels that fits
within the original data range.
library(plotrix)
axisRange <- c(0,500)
par(mar = rep(0,4))
plot(NULL, xlim = axisRange, ylim=axisRange, xaxs = "i", yaxs = "i")
draw.circle(0, 0, 200, col = "white", border = "red")
Gives:
At first I thought this would be trivial, but I could not figure out how to combine rectangles with lines in the legend of a lattice plot. Consider the following example:
library(latticeExtra)
xyplot(rnorm(10) ~ 1:10,
key=list(rectangles=list(size=2, border=F),
text=list(c("Zero", "One", "Two"), col="black"),
col=c("black", "lightgrey", "darkgrey"), divide=1, columns=1,
x=0.01, y=0.95, corner=c(0,1) ),
panel=function(x,...){
panel.abline(v=3, lty="dashed")
panel.xblocks(x,x>5, col="lightgrey")
panel.xblocks(x,x>7, col="darkgrey")
panel.xyplot(x, ...) } )
Instead of 3 rectangles, I would like to have 2 rectangles filled with the appropriate colours, and one dashed line above or below these two rectangles. If I provide a lines argument, then both lines and rectangles will be drawn for all elements (i.e. there will be 2 rectangles and 2 lines simultaneously next to each other).
How can I set up the legend key so that I get this mix of "symbols"? That is, how can I get one dashed line and two rectangles with the appropriate text and colours?
Any help is greatly appreciated! My apologies if this is trival. Please help me see the obvious! :)
This is a rather late answer but it is something I still do. One approach is to use auto.key in the function and then modify the lattice object with update(obj, key = newKey). A more general approach, as suggested by #josh-obrien, is to use the grid functions that under lattice. However, this typically requires empirical tweaking of the coordinates as can be seen in the need to use 3 decimal places of precision to place the dashed lines.
# relative position may be sensitive to absolute sizes
library(latticeExtra)
dev.new(width = 5, height = 5)
set.seed(1234)
# same code as in question, re-written a little bit
# using "transparent" for the 1st of the three rectangles
# using a grid call in the panel function to place the dashed line
xyplot(rnorm(10) ~ 1:10,
key = list(rectangles = list(size = 2, border = FALSE,
col = c("transparent", "lightgrey", "darkgrey")),
text = list(c("Zero", "One", "Two"), col = "black"),
columns = 1, corner = c(0.01, 0.95)),
panel = function(x,...) {
panel.abline(v = 3, lty = "dashed")
panel.xblocks(x, x > 5, col = "lightgrey")
panel.xblocks(x, x > 7, col = "darkgrey")
panel.xyplot(x, ...)
grid::grid.lines(c(0.04, 0.07), c(0.935, 0.935),
gp = gpar(lty = "dashed", col = "black"))
}
)
[![plot with combined elements in legend][1]][1]
[1]: https://i.stack.imgur.com/K7AJN.png
I am trying to increase the size of the coloured squares in the legend on a histogram in R - when I output the PDFs they are too small so it is hard to distinguish the colours. I've searched Google, the R-help Nabble forum and this place, all to no avail. I've also tried several of the commands in the legend documentation.
What do I need to use in the legend() function to increase them? and is it possible to remove the black border around each coloured square to ease viewing?
Here my example:
a<-c(1,1,2,3,3,3,3,4,54,56,2,23,1,3,23)
hist(a)
graphics::legend(x=-1,y=10,c(">0%",">20%",">40%",">60%",">80%"),
x.intersp=1,y.intersp=2,cex=1, bty="n",
fill=c("black","gray50","gray70","gray85","white"))
I wish to change the size of box in the legend?
SOLUTION: from #Ben Bolker
add to the script above the legend function
> source("http://www.math.mcmaster.ca/bolker/R/misc/legendx.R")
then add
> box.cex=c(2,2)
within the legend function
I hacked the source of the legend function to allow a box.cex argument that specifies the relative x and y dimensions of the box. This isn't perfect -- if the expansion is big enough then you have to adjust y.intersp to prevent the fill boxes from overlapping.
source("http://www.math.mcmaster.ca/bolker/R/misc/legendx.R")
a<-c(1,1,2,3,3,3,3,4,54,56,2,23,1,3,23)
cex <- 1
hist(a)
legend("topright",c(">0%",">20%",">40%",">60%",">80%"),
bty="n",
fill=c("black","gray50","gray70","gray85","white"),
box.cex=c(3,3),
y.intersp=2.8)
You can use very thick lines, with rectangular ends.
plot( 1, type = "n", axes = FALSE, xlab = "", ylab = "" ) # Empty plot
par( lend = 1 ) # Rectangular line endings
legend(
"topleft",
c( "Red", "Black" ),
col = c("red", "black"),
lty = 1, lwd = 10
)
It is difficult to mix the option fill and change the size of the box.
But we can have something with options pt.cex and pch without fill options.
a<-c(1,1,2,3,3,3,3,4,54,56,2,23,1,3,23)
par(mfrow=c(1,2))
hist(a)
graphics::legend(x=10,y=10,c(">0%",">20%",">40%",">60%",">80%"),
x.intersp=1,y.intersp=1,cex=c(1),bty="n",
fill=c("black","gray50","gray70","gray85","white"),
# pch=c(24,22,21,23,25),
pt.cex = c(2,2,2,2,5),
lwd=1.5,title='Histo fill option')
hist(a)
graphics::legend(x=10,y=10,c(">0%",">20%",">40%",">60%",">80%"),
x.intersp=1,y.intersp=1,cex=c(1),bty="n",
pch=c(24,22,21,23,25),
pt.cex = c(1,2,3,3,4),
lwd=1.5, title = 'Histo whithout fill ')