I am trying to plot an image and then overlay a legend on its top. The legend is covering the plot and I am not able to adjust it using different parameters like cex, lty, etc.
plot(cov16_2ms04h$unqC_Sp, cov16_2ms04h$unqC_My, log="xy",
col=(cov16_2ms04h$binom_q<0.001)+1,
ylab="Haplotype B Count", xlab="Haplotype A Count")
abline(0,1,col="grey")
legend("topleft",c("No significant imbalance","Significant imbalance"),pch=c(10,10),col=c(1,2), cex = 0.5)
is giving me plot as:
But, I want something like:
Thanks,
have you tried enlarge the size of the plot before overlying a legend? I tried you legend code, the legend was perfect on the plot.
The problem is with R-studio because the size of the output plot is determined by the costumized size of the pane.
To plot the results directly to a pdf or a png file the following can be done.
# plotting directly on pdf or png - select the required one
pdf("my_plot.pdf", height=6, width=6)
png("my_plot.png", width = 4, height = 4,
units = 'in', res = 300)
plot(cov16_2ms04h$unqC_Sp, cov16_2ms04h$unqC_My, log="xy",
col=(cov16_2ms04h$binom_q<0.001)+1,
ylab="Haplotype B Count", xlab="Haplotype A Count")
abline(0,1,col="grey") # draw abline
legend("topleft",c("No significant imbalance","Significant imbalance"),
pch=c(1,1),col=c(1,2), cex = 0.75) # add legend
dev.off() # close the plot
The output then comes as:
Thanks,
Related
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 want to have the plot legend for a pie chart as a single plot (6 graphics at a page - 5 plots, 1 legend). Now I have difficulties adjusting the space between the 2 columns.
I used the following code (by try and error):
#Colors
colors=c("blue","green","yellow","orange","red","purple","pink","grey")
legtext <- c("G","G*E","E","Source","Source*E",
"Table*E","Table*Block*E","Residual Error")
#Code for chart
pie3D(#data for pie
rooting1,
#specify labels vector
#labels=labels,
#specify labels size
labelcex=0.9,
#how much different pies go from each other
explode=0.1,
#height of chart
height=0.1,
#Main title
theta=pi/3,
#Colors
col=colors
)
#Code for legend
xcoords <- c(0.9,1,1.1,1.2)
secondvector <- (1:length(legtext))-1
textwidths <- xcoords/secondvector # this works for all but the first element
textwidths[1] <- 0
legend(-1, 0.9,ncol=2,
c("G","G*E","E","Source","Source*E","Table*E","Table*Block*E","Residual Error"),
cex = 0.8,
fill = colors,
text.width=textwidths)
The plot I get is this: I want to remove the vertical lines and if possible, remove the rest of the chart as I only want to display the legend.
Legend I get:
Can anybody help me?
Add bty="n" to your legend:
legend(-1, 0.9,ncol=2,
c("G","G*E","E","Source","Source*E","Table*E","Table*Block*E","Residual Error"),
cex = 0.8,
fill = colors,
text.width=textwidths,
bty="n")
As for the other question - how to get rid of the chart itself, this will take some fiddling. Basically, what you can do is to make an empty chart, but adjusting the xlim and ylim, as well as margins so that there is enough room for the legend:
par(mar=c(0.1,0.1,0.1,0.1)) # you don't need large margins
# but maybe you need more than 0.1
plot(NA, xlim=c(-1,1), ylim=c(-1,1), axes=FALSE, xlab="", ylab="")
# this makes an empty plot
# you may need to change xlim and ylim (or the x and y of your legend)
# ... so that the legend would start from the left/upper corner
Is there any possibility to control for the font size of axis labels when plotting a network generated with igraph in R?
Contrary to the package "network", cex.lab doesn't change anything when passed to 'plot'.
An example:
library(igraph)
testnet <- graph_from_adjacency_matrix(diag(10))
plot(testnet,xlab="This is xlab Text")
par(mfrow=c(2,3))
replicate(6,plot(testnet,xlab="This is xlab Text"))
replicate(6,plot(testnet,xlab="This is xlab Text",cex.lab=10))
With multiple plots on one page, the default font size is too small.
Note that I am not talking about setting the size of vertex labels (vertex.label.cex).
This is a workaround to your problem (even if it is not the solution to it). One possibility to get what you want is to omit the x-label and add a customizable text below your plot.
par(mfrow=c(1,2))
# plot with x-axis label
plot(graph_from_adjacency_matrix(diag(10)), xlab = "mylab")
# plot w/o x-axis label + text
plot(graph_from_adjacency_matrix(diag(10)))
text(0, -1.8, labels = "myxlab", cex = 2.5)
This could be your result.
Another option: add the label size to par.
One size for all plots
rr <- 2; cc <- 3
par(mfrow=c(rr,cc), cex.lab=1.5)
replicate(6,plot(testnet,xlab="This is xlab Text"))
Different sizes for the plots:
par(mfrow = c(rr,cc))
plot.new()
cex.labs <- matrix(runif(2*3, 1, 3), ncol=cc, nrow=rr)
for (x in seq_len(rr))
for (y in seq_len(cc)) {
par(mfg=c(x,y), cex.lab = cex.labs[x,y])
plot(testnet,xlab="This is xlab Text")
}
I want the bars to be close to each other, but not adjacent (beside=FALSE), and I want also the bars to be slightly thin. I have played around with space and width but I am not getting my desired results.
I prefer to stay with barplot function instead of using ggplots, but if there is no way thought it, that is ok.
This is the expected barplot (edited manually from the obtained):
And this is the obtained:
Code Used:
jpeg("03.Annotation.Distribution.DMRs.All.jpeg")
bp1 = barplot(ann[,1:2], ylim=c(0,1.1), col=ann[,3], beside=FALSE, space=10,
names.arg=c("Genome","DMRs"), las=3, xlab="Group", ylab="Frequency",
main="Annotation distribution on DMRs")
legend("top",legend=rownames(ann)[7:1], fill=ann[7:1,3], bty="n", horiz=FALSE)
dev.off()
Thankyou!
The width of the bars has to do with the size of the device you're using. You can hack the spacing by setting up a new plot window based on your data and add to it, ie, make the xlim larger than you need.
jpeg('./tmp.jpg', width = 250, height = 480, bg = 'grey90')
plot.new()
plot.window(c(0,6), c(0,1))
with(mtcars,
barplot(prop.table(table(gear, vs), 2), col = 1:3, add = TRUE,
legend.text = unique(gear), xlab = 'vs'))
dev.off()
I am still learning Sweave and R. I have below a sample code that reads in a data file and plots it. I am choosing the postscript option as I like ending up with an EPS file. There are a number of things that I would like to improve with the plot. Here is my code with my own comments for my own learning:
\documentclass[a4paper,12pt]{article}
\usepackage{Sweave} %%%%%%
\SweaveOpts{eps=TRUE}
\begin{document}
<<echo=FALSE, results=hide>>=
test.frame<-data.frame(ratio= c(0.0, 144.321, 159.407, 178.413, 202.557), value= c(0, 0.84, 0.8925, 0.945, 0.9975))
#
<<echo=FALSE,results=hide,eval=TRUE>>=
postscript('doudou.eps',
width=7, height=6,
colormodel="cmyk",
family = "ComputerModern",
horizontal = FALSE,
onefile=FALSE,
paper = "special",
encoding = "TeXtext.enc",
pagecentre=FALSE)
with(test.frame,plot(ratio, value, ylab= "Hello",
xlab="Wonderful",
type="o", # line and markers
bty="o", # box around graph
lty="solid", # solid line or put 1
lwd=3, # line width
pch=1, # or enclose symbol in quotes
cex=3, # size of markers
cex.lab=2, # label size
cex.axis=1.5, # axis annot size problem if big
cex.main=2, # main title size
xaxp=c(0, 200, 4), #c(x1, x2, n)
col=2, # plotting color
xlim=c(0,200),
yaxt = "n", #suppresses axis
main=" My curve"))
axis(2,seq(0,1, by=0.5), las=2,cex=3,cex.lab=2,cex.axis=1.5,cex.main=2)
dev.off()
#
\begin{figure}[htbp]
\begin{center}
\includegraphics[width=0.8\textwidth]{doudou.eps}
\end{center}
\end{figure}
\end{document}
Some things that I would like to know more about an improve:
I have a boxed frame around the plot. How to control its line width?
I am using cex.axis=1.5for the axis annotation size. if I change it to say cex.axis=3 then the values on the x-axis get big and they overlap with the tickmarks. Is there a way to then place the x-axis values a bit lower further away from the plot?
The y-label Hello is truncated at the top of the H letter in the plot. How to fix this?
How to move either the x-label Wonderful or y-label Hello further away from the plot?
If we look at the plotted curve, the axes do not start at (0,0) though the initial values are (0,0) for the data set. How to control the axes so that they start at (0,0)?
Thanks a lot...
"I have a boxed frame around the plot. How to control its line width?"
box(lwd=3)
"I am using cex.axis=1.5 for the axis annotation size. If I change it to say cex.axis=3 , then the values on the x-axis get big and they overlap with the tickmarks. Is there a way to then place the x-axis values a bit lower further away from the plot?"
par(mgp=c(3,1.5,0) ) # second element is number of lines below the box for the labels
"The y-label Hello is truncated at the top of the H letter in the plot. How to fix this?"
# use par() to increase left margins
"How to move either the x-label Wonderful or y-label Hello further away from the plot?"
par( mgp=c(4,1.5,0) ) # First element in mgp vector
"If we look at the plotted curve, the axes do not start at (0,0) though the initial values are (0,0) for the data set. How to control the axes so that they start at (0,0)?"
..., xaxs="i", yaxs="i", ... # can be done in `par` or in the plot call
So the R code for the figure is below:
postscript('doudou.eps',
width=7, height=6,
colormodel="cmyk",
family = "ComputerModern",
horizontal = FALSE,
onefile=FALSE,
paper = "special",
encoding = "TeXtext.enc",
pagecentre=FALSE)
par( mgp=c(4,1.5,0), mai=c(1.5, 1.5, 1.5, .75) ) # using inches as the spacing unit
with(test.frame, plot(ratio, value, ylab= "Hello",
xaxs="i", yaxs="i",
xlab="Wonderful",
type="o", # line and markers
bty="o", # box around graph
lty="solid", # solid line or put 1
lwd=3, # line width
pch=1, # or enclose symbol in quotes
cex=3, # size of markers
cex.lab=2, # label size
cex.axis=3, # axis annot size problem if big
cex.main=2, # main title size
xaxp=c(0, 200, 4), #c(x1, x2, n)
col=2, # plotting color
xlim=c(0,200),
yaxt = "n", #suppresses axis
main=" My curve"))
axis(2,seq(0,1, by=0.5), las=2,cex=3,cex.lab=2,cex.axis=1.4, cex.main=2)
box(lwd=3)
dev.off()
Not pretty, but it does illustrate the control functions. Basically you need to spend more time on the help(par) page.