R - plotted data points are indistinguishable - r

W = c(20000, 5000, 3000, 8, 2, 0.5)
BMR = c(19000, 12000, 960, 86, 30, 10)
BMRPlot <- plot(W, BMR, main='Graph 2', cex=1.25, pch=21, bg='blue', lwd=1)
The above is the data I am trying to plot, however as you can probably tell the final data points once plotted appear to be indistinguishable as they are so close together. What could I add to my line of code that would change the view of this so that all points could be visible?

In a situation like this, you more or less have to use a transformation to make all the points visible. Otherwise, the points would have to be incredibly small not to overlap, and then you wouldn't be able to see them.
A log transformation of x and y seems to work here.
logW = log(c(20000, 5000, 3000, 8, 2, 0.5))
logBMR = log(c(19000, 12000, 960, 86, 30, 10))
BMRPlot <- plot(logW, logBMR, main='Graph 2', cex=1.25, pch=21, bg='blue', lwd=1)
As noted by commenter below, you can do the log-transform within the plot statement if you want your tick values to be untransformed:
W = c(20000, 5000, 3000, 8, 2, 0.5)
BMR = c(19000, 12000, 960, 86, 30, 10)
BMRPlot <- plot(W, BMR, main='Graph 2', cex=1.25, pch=21, bg='blue', lwd=1, log="xy")

Related

Even display of unevenly spaced numbers on x/y coordinates

Would you advise on how I could make an even display of unevenly spaced number on a graph. For example, considering the code below :
BREAKS = c(0, 0.1, 1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500)
a <- seq(0,100,0.1)
b <- seq(0,1000,0.1)
plot(ecdf(a), col="red", xlim=c(0,100), main=NA, breaks=BREAKS)
plot(ecdf(b), col="green", xlim=c(0,100), add=T, breaks=BREAKS)
I would like to show on X-axis (0, 0.1, 1 and 10) spaced in an equal/even manner.

R raster plot even legend

I tried to use NetCDF file to draw a raster map in R using raster and ncdf4 packages.
The range of my data is too large, but almost my data distributes within 0-2000. You can look at the histogram of my data:
So I want to draw a plot with the interval of 200, like seq(0, 2000, 200).
But when I used these intervals, the values which are larger than 2000 were recognized by R as NA when drawing the plot. These large values are printed as transparent when drawing.
The plot using even intervals:
I tried to give large intervals to the plot, like breaks = c(seq(0, 1500, 100), 40000), but the legend of the plot looks ugly.
In a word, I want a plot with even interval and legend. Something like an open range, not a closed range.
The following plot is my desirable plot realized by other software. How can I resolve my problems using R?
My code is here:
library(rgdal)
library(raster)
library(ncdf4)
library(rasterVis)
library(sp)
ncname <- "output.nc"
ncdata <- nc_open(ncname)
bb <- raster(ncname)
hist(bb)
hist(bb, xlim = c(0, 2000), breaks = seq(0, 40000, 500))
plot(bb)
plot(bb, xlim = c(25, 53), asp = 1.5, breaks = c(seq(0, 1500, 100), 40000), col = topo.colors(20))
plot(bb, legend.only = FALSE, zlim = c(0, 2000), col = topo.colors(20), asp = 1.5,
legend.width = 1, legend.shrink = 1.0,
axis.args = list(at = seq(0, 2000, 100),
labels = seq(0, 2000, 100)))

barplot labels in R: add horizontal lines below the plot region

My code:
x <- c(10, 50, 20, 40)
barplot(x, names.arg=LETTERS[1:4])
What I want is:
I made this figure with the help of R and Adobe Acrobat. I am wondering can I obtain this figure using pure R code?
You can add text with mtext
mtext("E", side = 1, line = 3, adj = 0.375)
mtext("F", side = 1, line = 3, adj = 0.875)
and then draw line with lines but indicating xpd=T
lines(c(0,3.5),c(-10,-10),xpd=TRUE)
lines(c(3.8,4.8),c(-10,-10),xpd=TRUE)
However, you need manually adjust it.
Thanks to Pascal. I got another answer.
x <- c(10, 50, 20, 40)
barplot(x, names.arg=LETTERS[1:4])
mtext("E", side = 1, line = 3, adj = 0.375)
mtext("F", side = 1, line = 3, adj = 0.875)
axis(1, at=c(0.5,1,2,3,3.3), line=2.5, tick=T, labels=rep("",5), lwd=2, lwd.ticks=0)
axis(1, at=4+c(0.1,0.2,0.3,0.4,0.5),line=2.5,tick=T,labels=rep("",5), lwd=2, lwd.ticks=0)

create a heatmap with regions in R

I have the following kind of data: on a rectangular piece of land (120x50 yards), there are 6 (also rectabgular) smaller areas each with a different kind of plant. The idea is to study the attractiveness of the various kinds of plant to birds. Each time a bird sits down somewhere on the land, I have the exact coordinates of where the bird sits down.
I don't care exactly where the bird sits down, but only care which of the six areas it is. To show the relative preference of birds for the various plants, I want to make a heatmap that makes the areas that are frequented most the darkest.
So, I need to convert the coordinates to code which area the bird visits, and then create a heatmap that shows the differential preference for each land area.
(the research is a bit more involved than this, but this is the general idea.)
How would I do this in R? Is there a R function that takes a vector of coordinates and turns that in such a heatmap? If not, do you have some hints for more on how to do this?
Not meant to be the answer you are looking for, but might give you some inspiration.
# Simulate some data
birdieLandingSimulator <- data.frame(t(sapply(1:100, function(x) c(runif(1, -10,10), runif(1, -10,10)))))
# Assign some coordinates, which ended up not really being used much at all, except for the point colors
assignCoord <- function(x)
{
# Assign the four coordinates clockwise: 1, 2, 3, 4
ifelse(all(x>0), 1, ifelse(!sum(x>0), 3, ifelse(x[1]>0, 2, 4)))
}
birdieLandingSimulator <- cbind(birdieLandingSimulator, Q = apply(birdieLandingSimulator, 1, assignCoord))
# Plot
require(ggplot2)
ggplot(birdieLandingSimulator, aes(x = X1, y = X2)) +
stat_density2d(geom="tile", aes(fill = 1/..density..), contour = FALSE) +
geom_point(aes(color = factor(Q))) + theme_classic() +
theme(axis.title = element_blank(),
axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank()) +
scale_color_discrete(guide = FALSE, h=c(180, 270)) +
scale_fill_continuous(name = "Birdie Landing Location")
Use ggplot2. Take a look at the examples for geom_bin2d. It's pretty simple to get 2d bins. Notice that you pass in binwidth for both x and y:
> df = data.frame(x=c(1,2,4,6,3,2,4,2,1,7,4,4),y=c(2,1,4,2,4,4,1,4,2,3,1,1))
> ggplot(df,aes(x=x, y=y,alpha=0.5)) + geom_bin2d(binwidth=c(2,2))
If you don't want to use ggplot, you can use the cut function to separate your data into bins.
# Test data.
x <- sample(1:120, 100, replace=T)
y <- sample(1:50, 100, replace=T)
# Separate the data into bins.
x <- cut(x, c(0, 40, 80, 120))
y <- cut(y, c(0, 25, 50))
# Now plot it, suppressing reordering.
heatmap(table(y, x), Colv=NA, Rowv=NA)
Alternatively, to actually plot the regions in their true geographic location, you could draw the boxes yourself with rect. You would have to count the number of points in each region.
# Test data.
x <- sample(1:120, 100, replace=T)
y <- sample(1:50, 100, replace=T)
regions <- data.frame(xleft=c(0, 40, 40, 80, 0, 80),
ybottom=c(0, 0, 15, 15, 30, 40),
xright=c(40, 120, 80, 120, 80, 120),
ytop=c(30, 15, 30, 40, 50, 50))
# Color gradient.
col <- colorRampPalette(c("white", "red"))(30)
# Make the plot.
plot(NULL, xlim=c(0, 120), ylim=c(0, 50), xlab="x", ylab="y")
apply(regions, 1, function (r) {
count <- sum(x >= r["xleft"] & x < r["xright"] & y >= r["ybottom"] & y < r["ytop"])
rect(r["xleft"], r["ybottom"], r["xright"], r["ytop"], col=col[count])
text( (r["xright"]+r["xleft"])/2, (r["ytop"]+r["ybottom"])/2, count)
})

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")

Resources