I have 7 different categories per each value in X. I am using barplot to plot these categories. Such graph looks fine in colors printer, but what if I want it to be fine in black & white. You can check the graph below. I want to have different colors texture, so the graph looks good in color and black & white printer.
I used densities = c(10,30,40,50,100,60,80) for density parameter in barplot function. Are there any other ways to do different texture in barplot?
Note: I tried the angle value in barplot. However, it isn't a good solution in that case, since not all bars have high values (i.e height of the bar).
Along the lines of my comment, you might find the following helpful:
# data generation ---------------------------------------------------------
set.seed(1)
mat <- matrix(runif(4*7, min=0, max=10), 7, 4)
rownames(mat) <- 1:7
colnames(mat) <- LETTERS[1:4]
# plotting settings -------------------------------------------------------
ylim <- range(mat)*c(1,1.5)
angle1 <- rep(c(45,45,135), length.out=7)
angle2 <- rep(c(45,135,135), length.out=7)
density1 <- seq(5,35,length.out=7)
density2 <- seq(5,35,length.out=7)
col <- 1 # rainbow(7)
# plot --------------------------------------------------------------------
op <- par(mar=c(3,3,1,1))
barplot(mat, beside=TRUE, ylim=ylim, col=col, angle=angle1, density=density1)
barplot(mat, add=TRUE, beside=TRUE, ylim=ylim, col=col, angle=angle2, density=density2)
legend("top", legend=1:7, ncol=7, fill=TRUE, col=col, angle=angle1, density=density1)
par(bg="transparent")
legend("top", legend=1:7, ncol=7, fill=TRUE, col=col, angle=angle2, density=density2)
par(op)
Related
I would like to plot something like this (from this paper) where icons, in this case small graphs, are used as tick labels.
I get this far, where icons are more or less properly placed:
This is the code:
library(igraph)
npoints <- 15
y <- rexp(npoints)
x <- seq(npoints)
par(fig=c(0.05,1,0.3,1), new=FALSE)
plot(y, xlab=NA, xaxt='n', pch=15, cex=2, col="red")
lines(y, col='red', lwd=2)
xspan <- 0.9
xoffset <- (0.07+0.5/npoints)*xspan
for(i in 1:npoints){
x1 <- (xoffset+(i-1)/npoints)*xspan
x2 <- min(xspan*(xoffset+(i)/npoints),1)
par(fig=c(x1,x2,0,0.5), new=TRUE)
plot(graph.ring(i), vertex.label=NA)
}
However, if the number of points grows (e.g. npoints <- 15) it complains because there is no place for the icons:
Error in plot.new() : figure margins too large
I wonder wether there is a more natural way to do this so that it works for any (reasonable) number of points.
Any advice is welcome.
library(igraph)
npoints <- 15
y <- rexp(npoints)
x <- seq(npoints)
# reserve some extra space on bottom margin (outer margin)
par(oma=c(3,0,0,0))
plot(y, xlab=NA, xaxt='n', pch=15, cex=2, col="red")
lines(y, col='red', lwd=2)
# graph numbers
x = 1:npoints
# add offset to first graph for centering
x[1] = x[1] + 0.4
x1 = grconvertX(x=x-0.4, from = 'user', to = 'ndc')
x2 = grconvertX(x=x+0.4, from = 'user', to = 'ndc')
# abline(v=1:npoints, xpd=NA)
for(i in x){
print(paste(i, x1[i], x2[i], sep='; '))
# remove plot margins (mar) around igraphs, so they appear bigger and
# `figure margins too large' error is avoided
par(fig=c(x1[i],x2[i],0,0.2), new=TRUE, mar=c(0,0,0,0))
plot(graph.ring(i), vertex.label=NA)
# uncomment to draw box around plot to verify proper alignment:
# box()
}
I would like to match colorbar labels with colors in a map when plotting raster objects in R.
Assume I have the following raster:
library(raster)
r <- raster(matrix(runif(100), ncol=10))
and another raster:
r2 <- r/2
brks <- seq(0, 1, by=0.1)
nb <- length(brks)-1
cols <- rev(terrain.colors(nb))
par(mfrow=c(1,2))
plot(r, breaks=brks, col=cols, lab.breaks=brks, zlim=c(0,1), main='first')
plot(r2, breaks=brks, col=cols, lab.breaks=brks, zlim=c(0,1), main='second')
Problem:
If I plot:
plot(r, add=TRUE, legend=FALSE, axes=FALSE, lab.breaks=brks,
zlim=c(0,1), main='first',
col=colorRampPalette(c("darkred", "red3", "orange", "gold1", "yellow",
"lightskyblue","steelblue3", "royalblue3",
"darkblue","darkblue"))(255))
and then intend to plot r2 and I know:
breaks <- seq(min(minValue(r)), max(maxValue(r))))
How can I define values for r and r2 such that they match the colors defined by col above?
Please plot r and r2 with the same colorbar scale and assign values of choice to match those colors on the map. The values on the map must not necessarily be those assigned to the colorbar.
I need this kind of exercise because I am doing interpolation of station data. Kriging gives a good looking raster but underestimates the values. However, I have the max and min of the data I am mapping. Using the colors in col as above, I would like to assign values to the colorbar based on the colors on the map.
Many thanks,
Asong.
I have a sample data set for which I plot several png files divided by groups (in this case by ID) in a loop.
A question that concerns the x axis: How could I introduce a fixed range (lets say from 1940 to 2014 in every graph) into the for loop, so that the x-axis always corresponds to this range (case1) but if values in YEAR before 1940 are included, do the plot scenario with the whole group which is plotting the axis ranges automatically like in the for-loop introduced in the code above (case 2)?
Case 1 with the sample data would be for the group with ID 259 (NAME2) and case 2 would be for the group with ID 47 (NAME1)
Here is my code:
xy <- data.frame(NAME=c("NAME1", "NAME1","NAME1","NAME1","NAME2","NAME2","NAME2"),ID=c(47,47,47,47,259,259,259),YEAR=c(1932,1942,1965,1989,2007,2008,2014),VALUE=c(0,NA,-6,-16,0,-9,-28))
ind <- split(x = xy,f = xy[,'ID'])
### PLOT
for(i in 1:length(ind)){
png(names(ind[i]), width=3358, height=2329, res=300)
par(mar=c(6,8,6,5))
plot(ind[[i]][,c('YEAR','VALUE')],
type='n',
main=ind[[i]][1,'NAME'],
xlab="Time [Years]",
ylab="Length change [m]")
# plot axis
axis(1, at = seq(1000,2030,10), cex.axis=1, labels=FALSE, tcl=-0.3)
# plot points and lines
points(ind[[i]][,c('YEAR','VALUE')], type="l", lwd=2)
points(ind[[i]][,c('YEAR','VALUE')], type="p", lwd=1, cex=1, pch=21, bg='white')
# plot vertical line through 0
abline(h=0)
dev.off()
}
You've almost got it! Starting with a blank plot and then adding points/lines is perfect. Change your initial plot call to include the ranges you want, and you're good to go:
x.range <- c(1940, 2014)
if (min(ind[[i]][, 'YEAR'], na.rm = T) < 1940) {
x.range <- range(ind[[i]][, 'YEAR'], finite = T)
}
plot(x = x.range,
y = range(ind[[i]][,'VALUE'], finite = T),
type='n',
main=ind[[i]][1,'NAME'],
xlab="Time [Years]",
ylab="Length change [m]")
Note that these plots will still have different y axes.
I'm creating an heatmap through heatmap.2(). I'd like to put the xaxis labels rotate of 45 degree. Following the instructions in other posts, I tried to do this building an heatmap without x labels and then using text() to add them...
This is what I tried:
#fake matrix
cheese.matrix <- matrix(runif(100),10,10)
#build color palette
my.palette <- colorRampPalette(c("blue", "green", "yellow", "orange", "red"), space="rgb")
#build a first heatmap
hm_cheese <- heatmap.2(cheese.matrix,Rowv=NA,Colv=NA,col=my.palette,
density.info=c("none"),margins(3,5),cexRow=0.8,
cexCol=0.8,key=TRUE,keysize=1,trace="none",
lhei=c(2,8), breaks=100)
#find the coordinates on the plot where I want to pu the first and the last label
pos2 <- locator()
pos2
$x
[1] 0.08129779 0.90164993
$y
[1] -0.06905376 -0.06372554
pos2 <- structure(list(x=c(0.08129779, 0.90164993), y=c(-0.06905376, -0.06372554)), .Names=c("x","y"))
#create a vector with the labels I want to add
labs <- c("NWC1.PR", "CURD1.PR", "NWC2.PR","CURD2.PR","NWC3.PR","CURD3.PR", "NWC4.PR", "CURD4.PR", "NWC5.PR", "CURD5.PR")
#build another heatmap
hm_cheese <- heatmap.2(cheese.matrix,Rowv=NA,Colv=NA,col=my.palette,
density.info=c("none"),margins(3,5),key=TRUE,
keysize=1,trace="none", lhei=c(2,8), breaks=100,
labCol="", add.expr=text(x=seq(pos2$x[1], pos2$x[2], len=10),
y=rep(pos2$y[1],10), srt=45, xpd=TRUE, adj=0, labels=labs))
This put the labels on the heatmap, but all the names were overlying...
I also tried this:
hm_cheese2 <-heatmap.2(cheese.matrix,Rowv=NA,Colv=NA,col=my.palette,
density.info=c("none"),margins(3,5),key=TRUE,keysize=1,
trace="none", lhei=c(2,8), breaks=100, labCol="",
add.expr=text(x=seq_along(labs), y=-0.06372554, srt=45,
xpd=TRUE, adj=0, labels=labs))
The result was better, since the labels were along the axis, but still really close among them and overlying the plot...
Is there anything wrong in the way I used locator() to find the coordinates?
Can anyone help me to improve my code?
You can play with argument pos in your call to text. By using pos=1 for example:
hm_cheese2 <-heatmap.2(cheese.matrix,Rowv=NA,Colv=NA,col=my.palette,
density.info=c("none"),margins(3,5),key=TRUE,keysize=1,
trace="none", lhei=c(2,8), breaks=100, labCol="",
add.expr=text(x=seq_along(labs), y=-0.06372554, srt=45,
xpd=TRUE, adj=0, labels=labs, pos=1))
See ?text for more on pos.
If the labels fall outside the plot, you can try using xpd=NA to clip them to the device region instead of the plot or the figure regions.
hm_cheese2 <-heatmap.2(cheese.matrix,Rowv=NA,Colv=NA,col=my.palette,
density.info=c("none"),margins(3,5),key=TRUE,keysize=1,
trace="none", lhei=c(2,8), breaks=100, labCol="",
add.expr=text(x=seq_along(labs), y=-0.06372554, srt=45,
xpd=NA, adj=0, labels=labs, pos=1))
I'm trying to create a plot where color represents the combination of several values. In the example below, I am applying increasing values for red associated with the x-coordinate and increasing values for blue in associated with the y-coordinate.
#required function 'val2col' from: http://www.menugget.blogspot.de/2011/09/converting-values-to-color-levels.html
val2col<-function(z, zlim, col = heat.colors(12), breaks){
if(!missing(breaks)){
if(length(breaks) != (length(col)+1)){stop("must have one more break than colour")}
}
if(missing(breaks) & !missing(zlim)){
zlim[2] <- zlim[2]+c(zlim[2]-zlim[1])*(1E-3)#adds a bit to the range in both directions
zlim[1] <- zlim[1]-c(zlim[2]-zlim[1])*(1E-3)
breaks <- seq(zlim[1], zlim[2], length.out=(length(col)+1))
}
if(missing(breaks) & missing(zlim)){
zlim <- range(z, na.rm=TRUE)
zlim[2] <- zlim[2]+c(zlim[2]-zlim[1])*(1E-3)#adds a bit to the range in both directions
zlim[1] <- zlim[1]-c(zlim[2]-zlim[1])*(1E-3)
breaks <- seq(zlim[1], zlim[2], length.out=(length(col)+1))
}
colorlevels <- col[((as.vector(z)-breaks[1])/(range(breaks)[2]-range(breaks)[1]))*(length(breaks)-1)+1] # assign colors to heights for each point
colorlevels
}
#data
x <- seq(100)
y <- seq(100)
grd <- expand.grid(x=x,y=y)
#assign colors to grd levels
pal1 <- colorRampPalette(c("white", rgb(1,0,0)), space = "rgb")
col1 <- val2col(x, col=pal1(10))
pal2 <- colorRampPalette(c("white", rgb(0,0,1)), space = "rgb")
col2 <- val2col(y, col=pal2(10))
col3 <- NA*seq(nrow(grd))
for(i in seq(nrow(grd))){
xpos <- grd$x[i]
ypos <- grd$y[i]
coltmp <- (col2rgb(col1[xpos])/2) + (col2rgb(col2[ypos])/2)
col3[i] <- rgb(coltmp[1], coltmp[2], coltmp[3], maxColorValue = 255)
}
#plot
png("2_color_scales.png", width=6, height=4, units="in", res=200)
layout(matrix(c(1,2,3), nrow=1, ncol=3), widths=c(4,1,1), heights=4, respect=T)
par(mar=c(4,4,2,2))
plot(grd,col=col3, pch=19)
par(mar=c(4,0,2,5))
image(x=1, y=x, z=t(as.matrix(x)), col=pal1(10), xaxt="n", yaxt="n", xlab="", ylab="")
box()
axis(4)
mtext("x", side=4, line=3, cex=0.7)
par(mar=c(4,0,2,5))
image(x=1, y=y, z=t(as.matrix(y)), col=pal2(10), xaxt="n", yaxt="n", xlab="", ylab="")
box()
axis(4)
mtext("y", side=4, line=3, cex=0.7)
dev.off()
The result is technically correct in that when x = 1 and y = 10, the mixing of colors "white" and "blue", respectively, returns the lighter shade of blue. However, I would rather like this position to look as "blue" as the darkest blue of the y color bar. I imagine this would require one to use transparency for lower values rather than the color white. Does anyone have suggestions on how this might be accomplished? Adding two colors, including their transparency, is beyond me... I thought one might be able to use the transparency value as a weighting in the mixing step?
Thanks for your help.
Since I'm more familiar with ggplot, I'll show a solution using ggplot. This has the side benefit that, since the ggplot code is very simple, we can focus the discussion on the topic of colour management, rather than R code.
Start by using expand.grid to create a data frame dat containing the grid of red and blue input values.
Use the function rgb() to create the colour mix, and assign it to dat$mix
Plot.
The code:
dat <- expand.grid(blue=seq(0, 100, by=10), red=seq(0, 100, by=10))
dat <- within(dat, mix <- rgb(green=0, red=red, blue=blue, maxColorValue=100))
library(ggplot2)
ggplot(dat, aes(x=red, y=blue)) +
geom_tile(aes(fill=mix), color="white") +
scale_fill_identity()
You will notice that the colour scale is different from what you suggested, but possibly more intuitive.
When mixing light, absence of any colour yields black, and presence of all colours yields white.
This is clearly indicated by the plot, which I find rather intuitive to interpret.