I would like to create a very simple plot.
I am using this data:
a <- read.table("https://dl.dropbox.com/u/22681355/a.csv", sep=',', header=TRUE)
plot(a$X25, type="l",col="red", ylim=c(0,100))
lines(a$X25.1, type="l", col="blue")
lines(a$X25.2, type="l", col="green")
lines(a$X25.3, type="l", col="brown")
Now I would like to add a simple legend that indicates which color is which variable.
I understand that I can use the legend() command, but my problem is that I don't know how to put colors next to the text in the legend.
What's the simplest command that would do this?
Take a look at ?legend and try this:
legend('topright', names(a)[-1] ,
lty=1, col=c('red', 'blue', 'green',' brown'), bty='n', cex=.75)
Related
When creating a plot using gplots.plotmeans, the axis labels are not set to what I want, but to "means" and "Index".
Here is the code:
library(gplots)
plotmeans(ioe.dimension ~ ioe$profile,
col=3,
xlab="Profile", ylab="Means",
ylim=c(2.5,6.5),
bars=FALSE)
abline(h=4, lty=2, col=2)
The resulting plot is this:
This happens for some reason when bars = FALSE. I don't know why, but for me it works to first remove the labels with ann = F and then add them with a call to title().
library(gplots)
plotmeans(ioe.dimension ~ ioe$profile,
col=3,
ylim=c(2.5,6.5),
bars=FALSE,
ann=F)
title(xlab="Profile", ylab="Means")
abline(h=4, lty=2, col=2)
I have a following "beeswarm" (a single-dimensional scatterplot)
library(beeswarm)
data(breast)
beeswarm(breast$time_survival,horizontal=TRUE)
Here is the resulting plot:
How can I get rid of the axes and the box around the plot, so that I can reintroduce only the X axis and nothing else around it?
If you create an empty plot first
plot(rnorm(10), type="n", axes=FALSE, xlim=c(0, 200), ylim=c(0.4, 1.6),
xlab="", ylab="")
Then you can use the add argument to get what you want
beeswarm(breast$time_survival,horizontal=TRUE, add=TRUE)
You can use the "axes" argument (described in ?plot.default).
beeswarm(breast$time_survival, horizontal=TRUE, axes = FALSE)
Consider this example:
labs <- c("AT~frac(T,C)~G","GGAA","TTAA","AAAA")
plot(c(1:4), c(1:4), axes=F, xlab="",ylab="", par(mar=c(8,3,2,1)))
axis(1, at=c(1:4), labels=labs, las=2)
that generates this:
My intention is to have something like this:
that I hardcoded as:
plot(c(1:4), c(1:4), type="n", axes=F, xlab="",ylab="")
axis(1, at=c(1:4), labels=c(expression(AT~frac(T,C)~G), expression(GGAA), expression(TTAA), expression(AAAA)), las=2)
The closest answer I got was this.
Getting expression() to work as I desired is really very confusing for me. I intend to have these x-axis tick mark labels dynamically generated from available data using a vector of "expression strings".
You were close! frac() is a function that you need to call. it can be used with strings as an argument. This sample
labs <- expression(paste("AT"~frac("T","C")~"G",sep=""),"GGAA","TTAA","AAAA")
plot(c(1:4), c(1:4), axes=F, xlab="",ylab="")
axis(1, at=c(1:4), labels=labs, las=2)
generates this plot:
I am using R to plot line chart, with the following command
data <- read.table("input_data.txt", header=T, sep="\t")
ind=seq(1,nrow(data),by=2)
pdf(file="result.pdf")
plot_colors <- c("black","red","green","blue","purple","red")
plot(data$column_one, type="l", lty=1, col=plot_colors[1], ann=FALSE)
lines(data$column_two, type="l", lty=2, col=plot_colors[2])
lines(data$column_three, type="o", pch=1, lty=0, col=plot_colors[3], cex=1)
lines(data$column_four, type="o", pch=3, lty=0, col=plot_colors[4], cex=1)
lines(data$column_five, type="o", pch=2, lty=0, col=plot_colors[5], cex=1)
lines(data$column_six, type="o", pch=4, lty=1, col=plot_colors[6], cex=1)
box()
dev.off()
The problem is, I have 500 data points, and the symbol markers are all mashed up on the line, tightly compact on the line. I could not see the symbols on the line.
Is there a way to just show the symbol markers at fixed interval, without them cluttering together?
Use something like that:
lines(data$column_three, type="o", pch=c(1,NA,NA,NA,NA), lty=0, col=plot_colors[3], cex=1)
For the pch command: The number (here "1") defines your symbol as before. The "NA" means, that these points are plotted with no symbol. This vector will be repeatedly used until the end of your plot. Here, every 5th point is plotted with symbol 1.
I have an ultra short question about R
My aim is to assign a common title to a multi-panel plot generated using par, e.g.
par(mfrow=c(1,2))
plot(rnorm(1000))
plot(rnorm(1000))
So, something like "main" for the plot function, but extended to both plots. Is there a canonical way to do this?
Thanks for any answer :-)
Use mtext with option outer:
set.seed(42)
oldpar <- par(mfrow=c(1,2), mar=c(3,3,1,1), oma=c(0,0,3,1)) ## oma creates space
plot(cumsum(rnorm(100)), type='l', main="Plot A")
plot(cumsum(rnorm(100)), type='l', main="Plot B")
mtext("Now isn't this random", side=3, line=1, outer=TRUE, cex=2, font=2)
par(oldpar)