Changing size of axis labels in hexbin plot - r

Is there a way to change the font size of axis labels when plotting hexbin data?
library(hexbin)
myData <- hexbin(rnorm(100), rnorm(100))
myPlot <- plot(myData, xlab = "Variable 1", ylab = "Variable 2")

You can suppress the labels and add them separately with grid commands. It may take some trial and error to position them exactly where you want them:
library(grid)
myPlot <- plot(myData, xlab="", ylab="", lcex=.75)
grid.text("Variable 1", .45, .1, gp=gpar(fontsize=12))
grid.text("Variable 2", .05, .5, rot=90, gp=gpar(fontsize=12))

Related

how to plot matlab style log-log plot using R plot

ENV
R version 3.3.1
MAC OSX 10.9.4
I would like to plot a style like figure below, which is plotted by matlab.
There is full grid on the plot with customized axis range (e.g. $10^0~10^{-4}$) and axis label (e.g. 10^0 10^1 10^-2 10^-3 10^-4 10^-5). There are ten ticks between 10^0 and 10^1 and also other labels. Similar for y axis.
Expected:
I tried:
initial.dir<-getwd()
setwd("/Rworks/bin")
sink("r.o")
pk <- read.table("2017.file)
rownames(pk)<-c("k","pk")
d.f <- data.frame(t(pk))
png(file="m.png")
plot(
d.f$k,
d.f$pk,
type = "n",
log = "xy",
xlim = c( 10^0, 10^2),
ylim = c( 0.00001, 1),
)
lines( d.f$k, d.f$pk, col = "green4", lty = "dotted")
points( d.f$k, d.f$pk, bg = "limegreen", pch = 21 )
box()
dev.off
sink()
setwd(initial.dir)
I got:
The axis and axis label and the ticks and grid is not what I want. Can anyone can give an advices? Thanks.
Worst case scenario, you can just draw the axes and background lines yourself.
plot(
x=c(1,2), y=c(0.6,0.2),
pch=21, bg="red",
log = "xy",
xlim = c( 10^0, 10^2),
ylim = c( 0.00001, 1),
xaxt="n", yaxt="n",
xlab="", ylab="",
yaxs="i"
)
lines(x=c(1,2), y=c(0.6,0.2))
axis(1, at=10^(0:2),
labels=expression(10^0, 10^1, 10^2))
axis(2, at=10^(-5:0), las=1,
labels=expression(10^-5, 10^-4, 10^-3, 10^-2, 10^-1, 10^0))
abline(h=outer((1:10),(10^(-5:-1))), col="#00000033", lty=2)
abline(v=outer((1:10),(10^(0:1))), col="#00000033", lty=2)
Here's an example - it's not exactly what you want (e.g. you could play around with theme options such as panel.grid.minor to get dotted grid lines), but it's most of the way there.
Exponential-format axis tick labels, from here:
fancy_scientific <- function(l) {
# turn in to character string in scientific notation
l <- format(l, scientific = TRUE)
# quote the part before the exponent to keep all the digits
l <- gsub("^(.*)e", "'\\1'e", l)
# turn the 'e+' into plotmath format
l <- gsub("e", "%*%10^", l)
# return this as an expression
parse(text=l)
}
Manual ticks from #G5W's answer: might be possible to write a function to do this automatically, or one might exist somewhere.
yticks = outer((1:10),(10^(-5:-1)))
xticks = outer((1:10),(10^(0:1)))
Draw the plot (with #G5W's sample mini-data)
library(ggplot2)
ggplot(data.frame(x=1:2,y=c(0.6,0.2)),
aes(x,y))+
geom_point(colour="red")+
scale_x_log10(limits=c(1,100),labels=fancy_scientific,
minor_breaks=xticks)+
scale_y_log10(limits=c(1e-5,1),labels=fancy_scientific,
minor_breaks=yticks)+
theme_bw()

Plot multiple frames of different size on the same window

Consider this example:
par(mfrow=c(2,3))
frame()
image(matrix(1:100, nrow=100), main="my wide plot", axes=FALSE)
frame()
plot(rnorm(120), rnorm(120), main="plot 1")
plot(dpois(0:20, lambda=6), type="b", main="plot 2")
x = rnorm(100)
y = x+runif(100, 10, 12)
plot(x=x, y=y, , main="plot 3")
How can I do to make my first graph (image(...) titled my wide plot) to occupy the 3 frames on top of the window?
A simple way would be to use layout():
layout(mat=matrix(c(1,1,1,2,3,4), ncol=3, byrow=TRUE))
image(matrix(1:100, nrow=100), main="my wide plot", axes=FALSE)
plot(rnorm(120), rnorm(120), main="plot 1")
plot(dpois(0:20, lambda=6), type="b", , main="plot 2")
x = rnorm(100)
y = x+runif(100, 10, 12)
plot(x=x, y=y, main="plot 3")
(For a nice example of a much more sophisticated layout, see here.)

Plot area too short for Y axis (base graphics)

I have created this data frame:
seq(1,70)
Group <-paste("a", 1:70, sep="")
Counts <- c(1:18, 5:14, 1:20, 5:20, 10:15)
When plotted, it returns a large plot where "Group" does not fit:
barplot(Counts, names.arg=Group,
horiz=TRUE, las=1, cex.names=0.6, border=NA,
ylim=c(0,30), xlim=c(0,20), width = 1.5)
EDIT: this is the plot after deleting ylim=c(0,30)
I would like to be able to see each term on the "Group" axis.
The ylim argument is constraining how many groups are shown. Remove it and you can see all of them.
I tried:
barplot(stage1_90$Percentage.stage1,
names.arg=stage1_90$miR.stage1,
horiz=TRUE, las=1, cex.names=0.5, border=NA,
ylim=c(0,125), xlim=c(0,14), main = "Stage 1. Top 90%",
xlab = "% total expression", space = 1, yaxs = "i")
And then I printed out in A4 format

Pie-chart and legend are overlapping

I'm trying to create a pie-chart, like here, but the legend and the actual pie are overlapping.
The code I used is here:
library(ggplot2)
library(grid)
par(mai = c(1,1,1,1))
numb <- c(41, 30, 21, 8)
colors <- c("black", "grey70", "grey30","white")
numb_labels <- round(numb/sum(numb) * 100, 1)
numb_labels <- paste(numb_labels, "%", sep="")
xx <- c("the process of familiarizing with the codebase",
"the patch review process",
"the impact on the users of the project",
"high degree visibility of the contributions")
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
legend("top", legend = xx, fill=colors, bty="n")
Can someone help me?
I don't know why you attach ggplot2.
You can use layout:
par(mai = c(0,0,0,0))
layout(c(1,2),heights=c(0.3,1))
plot.new()
legend("bottom", legend = xx, fill=colors, bty="n")
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
One solution is to use argument xpd=TRUE with function par() and then set legend outside the plot region with coordinates of legend.
par(xpd=TRUE)
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
legend(-1.4,1.6, legend = xx, fill=colors, bty="n")
par(mai = c(1,1,4,1), xpd=TRUE)
pie(numb, col=colors, labels=numb_labels, clockwise=TRUE)
legend("top", inset=c(0,-0.5),cex=0.9,legend = xx, fill=colors, bty="n")
Note: First element of inset shift your legend horizontally and the second one vertically.
cex parameter can resize your legend. So may adjust your legend by changing those parameter values.

How to incorporate updated line colours into legend of a plot in R using lattice?

Context and Question:
I want to add a legend to a lattice plot in R that shows the density of two groups.
I've changed the default colours to black and gray. However, the legend has not updated the colours.
How can I get the lattice plot to use my updated colours in the legend?
How can I control the position of the legend (I'd like to be able to place it inside the plot area)?
Working example:
set.seed(4444)
x1 <- rep("Group A", 50)
x2 <- rep("Group B", 50)
y1 <- rnorm(50, 0, 2)
y2 <- rnorm(50, 1, 2)
dtf <- data.frame(x=c(x1, x2), y =c(y1, y2))
print(densityplot(~y, groups=x, data=dtf,
pch=".",
cex=2,
col=c("black", "gray"),
auto.key=TRUE,
xlab="Y"))
The legend color is a well-known annoyance in lattice. It looks like it is difficult to correct, because Deepayan recommends simpleTheme as a solution. For positioning, see Josh's answer.
print(densityplot(~y, groups=x, data=dtf,
pch=".",
cex=2,
par.settings=simpleTheme(col=c("gray","black")),
auto.key=TRUE,
xlab="Y"))
There might be a more elegant solution, but this works well enough. Notice that the corner= argument can be used to place the legend anywhere inside the plot.
densityplot(~y, groups = x, data = dtf,
pch = ".",
cex = 2,
col = c("black", "gray"),
par.settings = list(superpose.line = list(col=c("black", "grey"))),
auto.key = list(corner = c(0.95, 0.95)),
xlab = "Y")

Resources