r geom polygon coordinates - r

What is the correct way to enter coordinates with geom_polygon?
In this plot I would like to draw 2 rectangles.
One going from .5 to 1.5 on the x axis and 148 to 161 on the y axis.
The other going from 1.5 to 2.5 on the x axis and 339 to 352 on the y axis.
The coordinates in the polygon() below work but I'd like to confirm how the coordinates must be entered. Below the coordinates are entered with the bottom line of each rectangle first 148 148 339 339 are entered and then the top line of each rectangle are entered: 161 161 352 352. Is that how the coordinates must be entered - bottom line first then top line?
plot(1, type="n", main="test",
xlim=c(0, 5), xlab="y",
ylim=c(0, max( 0,400 ) ), ylab="")
polygon(
x=c(0.5 ,1.5, 1.5, 2.5, 2.5, 1.5, 1.5, 0.5),
y= c(148, 148, 339, 339, 352, 352, 161, 161),
col = "blue", border = NA)
When I enter all 4 coordinates for each rectangle for the first rectangle first and then all 4 coordinates for the second rectangle the plot is wrong:
plot(1, type="n", main="test",
xlim=c(0, 5), xlab="y",
ylim=c(0, max( 0,400 ) ), ylab="")
polygon( x=c(.5,1.5,.5,1.5,1.5,2.5,1.5,2.5 ), y=c(148,148,161,161,339,339,352,352 ),
col = "red", border = NA)
Thank you.

This is a base plot question rather than ggplot2
polygon is trying to to draw a single polygon rather than the two you want. It is also assuming that the points are in order, and that the last point is connected to the first point
So your second example might work better if you separated the rectangles and reordered the points, perhaps trying
plot(1, type="n", main="test",
xlim=c(0, 5), xlab="y",
ylim=c(0, max(0, 400)), ylab="")
polygon(x=c(0.5, 1.5, 1.5, 0.5), y=c(148, 148, 161, 161),
col = "red", border = NA)
polygon(x=c(1.5, 2.5, 2.5, 1.5), y=c(339, 339, 352, 352),
col = "red", border = NA)
so rather than
you would get
which is what I assume you want

Related

Matching an arrow's ends with the x-axis R plot

I'm wondering how to to get the left-most end and the right-most end of my arrows() in my plot to EXACTLY match the left-most end and the right-most end of my X-AXIS lying beneath it (if possible, such that one can read the values (within rounding error) for two ends of the arrow off the X-Axis right below it)?
Here is my R code with no success:
ci <- c(0.09253967, 0.48434172)
plot(1, 1, ty="n" ,ann=F, yaxt="n", bty="n", xlim=c(ci[1], ci[2]), ylim=c(0, 1), xaxs="i")
arrows(ci[1], .01, ci[2], .01, code=3, lwd=2, angle = 90, length = .08 )
One method: control where axis places the ticks:
ci <- c(0.09253967, 0.48434172)
plot(1, 1, ty="n" ,ann=F, yaxt="n", bty="n", xlim=c(ci[1], ci[2]), ylim=c(0, .3), xaxt="n")
axis(side = 1, at = ci)
arrows(ci[1], .01, ci[2], .01, code=3, lwd=2, angle = 90, length = .08 )
... but the ticks are at not-pretty locations (though you can easily control this).
Another method: find what axTicks thinks would be the correct axis ticks for the current plot:
newx <- range(axTicks(side = 1))
newx
# [1] 0.1 0.4
# ... your plotting code ...
arrows(newx[1], 0.02, newx[2], 0.02, code=3, lwd=2, angle=90, length=.08)

How to adjust the position of circles when plotting venn diagram

I wanted to plot the venn diagram with two sets in which one set falls completely within another. I could draw a diagram with R package Venndiagram like this
library(VennDiagram)
grid.newpage();
venn.plot <- draw.pairwise.venn(area1 =467 ,area2 =273 ,cross.area = 273,
category = c("Set1", "Set2"),fill = c("darkorange", "dodgerblue1"),
lty = rep("solid", 2),lwd = c(2,2),col = c("black","black"),cex = 2,cat.cex = 2,cat.pos = c(310, 135),
cat.dist = 0.09,cat.just = list(c(-1, -1), c(1, 1)),
ext.pos = 30,ext.dist = -0.05,
ext.length = 0.85,ext.line.lwd = 2,ext.line.lty = "dashed");
grid.draw(venn.plot);
This may sound like esoteric tricks, but how to adjust the position of the circles, say, instead of two concentric circles, let the inner circle touch the the outer one?
Something like this one here. I added one non overlapping element.
I could not find an argument in the Venndiagram package allowing me to adjust the position of the circles.
You can try this with plotrix:
library(plotrix)
area1 = 467
area2 = 273
r1 = round(sqrt(area1/pi))
r2 = round(sqrt(area2/pi))
xc = 8
yc = 8
plot(0:40,0:40,type="n",xlab="",ylab="",main="Venn Diagram", xaxt='n', yaxt='n')
draw.circle(xc+r1,yc+r1,r1,border="black", col="orange",lty=1,lwd=1)
draw.circle(xc+2*r1-r2,yc+r1,r2,border="black", col="steelblue",lty=1,lwd=1)
text(xc+2*r1-r2,yc+r1, '272', cex=3)
text(xc+(r1-r2)/2+1,yc+r1, '195', cex=3)
text(xc+r1,yc+2*r1+7, 'Set1', cex=3)
text(xc+r1+r2,1, 'Set2', cex=3)

R plot: Print text on margin in top left corner

I have the following basic code to plot a grid.
grid <- expand.grid( pi=seq(50, 95, 5) / 100, mu2=seq(5, .5, -.5) )
pi <- seq(44, 100, .5) / 100
par( mai=c(.05, .05, .05, .05), oma=c(2.9, 2.9, 0, 0) ) # Make room for label
plot( grid, cex=.5, xlab="", ylab="", cex.axis=.7 )
How can I plot a text label such as "(A)" in the top left corner as indicated by the red circle?
Edit: The "(A)" should be printed in regular, i.e., horizontal, reading direction; not vertically along with the y-axis.
You can use mtext to place text outside of the margins:
##Look at the help page for further details
mtext("A", 2, adj=1, line=2)
##To rotate "A", try
mtext("A", 2, adj=5, las=1, padj=-22)
to get:
You may try this:
text(x = 0.44, y = 5, labels = "(A)", xpd = NA)
See also ?par how you can adjust plot margins with mar, if you need more space for the text.

r grouped barplot from Excel CSV file

I'm trying to make a grouped barplot in r, but there are some things I cannot figure out. This is what I have so far:
I would like:
to create a matrix from the data.frame (.csv file, see below)
the ablines to appear, but not in front of the bars
labels for the grouped bars (November, December, January, ... ->see data below)
for the plot layout to be as shown below. (I basically want the plot border)
I used the following code:
x<-matrix(nrow=3,ncol=7, data=c(200,227,196,210,279,319,220,126,111,230,196,123,240,106,94,250,154,233,260,226,218))
tiff("p_month_all.tiff", width=600, height=300)
par(mar=c(5,4,0.5,0.5))
a=c("November","December","January","February","March","April","May")
barplot(x, beside=TRUE, ylim=c(0,350),xlab="Month", axes=TRUE,axis.lty=1, ylab="Monthly Precipitation [mm]", col=c("darkblue","dodgerblue3","deepskyblue1"),panel.first= abline(h = c(50,100,150,200,250,300), col = "grey", lty = 2), xaxt="n", yaxt="n")
par(ps=12, cex =1, cex.main=2)
axis(2, c(0,350, c(50, 100, 150, 200, 250, 300)), las=1)
dev.off()
The data set (.csv file) looks like this:
Month Hornberg Strick Huetten
November 120 278 234
December 279 156 145
January 328 300 299
February 267 259 234
March 190 201 187
April 150 199 177
May 147 156 160
I've rewritten your code for clarity so you can see more easily what the problem is.
You were suppressing the axes with xaxt = "n" and yaxt = "n". I removed those lines.
Adding a call to box draws the box around the plot.
Adding a call to grid draws gridlines in the plot.
I've added row and column names to your data matrix so the plot know what to use in the axes.
I've updated the plot margins.
I also tidied a few bits like replacing month names with month.name and using seq.int rather than a hard-coded sequence.
x <- matrix(
c(
200, 227, 196,
210, 279, 319,
220, 126, 111,
230, 196, 123,
240, 106, 94,
250, 154, 233,
260, 226, 218
),
nrow = 3,
ncol = 7
)
colnames(x) <- month.name[c(11:12, 1:5)]
rownames(x) <- c("Hornberg", "Strick", "Huetten")
par(mar = c(5, 4, 1.5, 0.5), ps = 12, cex = 1, cex.main = 2, las = 1)
barplot(
x,
beside = TRUE,
ylim = c(0,350),
xlab = "Month",
axes = TRUE,
axis.lty = 1,
ylab = "Monthly Precipitation [mm]",
col = c("darkblue", "dodgerblue3", "deepskyblue1"),
panel.first = abline(
h = seq.int(50, 300, 50),
col = "grey",
lty = 2
)
)
box()
grid()
So, first of all, look through ggplot2 documentation, it's pretty good http://docs.ggplot2.org/0.9.3.1/index.html
If you haven't found an answer for your question, never give up googling :)
Ok, about your question:
Create data
help(read.csv) -> import your data to data.frame named x
Prepare data for the plot:
Melt your data to use it for the plot
x<-melt(x)
Use Month variable as a factor and order by month:
x$Month=factor(x$Month,level=month.name)
x<-x[order(x$Month),]
Plot the graph using ggplot2 (as you tagged it here and it's straitforward in use)
ggplot(x,aes(x=Month,y=value,fill=variable))+geom_bar(stat="bin",position="dodge")+theme_bw()+ylab("Monthly Precipitation [mm]")+xlab("Month")
For the colours, can use scale_fill_brewer() (great tutorials here:http://www.cookbook-r.com/Graphs/Colors_%28ggplot2%29/)
ggplot(x,aes(x=Month,y=value,fill=variable))+geom_bar(stat="bin",position="dodge")+theme_bw()+ylab("Monthly Precipitation [mm]")+xlab("Month")+scale_fill_brewer(palette="Blues")

How to set the second x-aixs in the graph?

x <- seq(0.5, 0.9, length = 400)
y <- dnorm(x,0.7,0.0458)
plot(x, y, type="l", yaxt="n",ann=FALSE,bty="n", xaxt="n")
axis(1, at=seq(0.5,0.9,by=0.1), labels=c("","",0.7, 0.8, 0.9) )
mtext("Proportions", 1, at=0.9, line=2)
xx=c(0.8,seq(0.8,0.9,length=100),0.9)
yy= c(0,dnorm(seq(0.8,0.9,length=100),0.7,0.0458),0)
polygon(xx, yy, col = "gray", border = NA)
I get a good graph(graph1.jpg) with the code,how can i create the second line on the graph1.jpg,to change graph1.jpg into graph2.jpg?
This is a graph1.jpg.
This is a graph2.jpg.
For the function axis() there is an argument line= that determine in which line under plot axis is drawn. Setting this argument, for example, to line=4 you can add another axis. But before plot() you should change margin setting to get more space under x axis with par(mar=...)).
par(mar=c(8,3,3,3))
x <- seq(0.5, 0.9, length = 400)
y <- dnorm(x,0.7,0.0458)
plot(x, y, type="l", yaxt="n",ann=FALSE,bty="n", xaxt="n")
#First x axis
axis(1, at=seq(0.5,0.9,by=0.1), labels=c("","",0.7, 0.8, 0.9) )
mtext("Proportions", 1, at=0.9, line=2)
#Second x axis
axis(1, line=4,at=seq(0.5,0.9,by=0.1), labels=c("","",0, 2.18, 3) )
mtext("z score", 1, at=0.9, line=6)

Resources