R get legend with same color as plot lines - r

Hi I am new to R and am trying to plot multiple files as lines in a scatter plot. I was able to get the plot but not when I try to add legend to the plot. I want the legend with name of the file in the same color as the color of the line made from that file. I tried using the following suggestion from a previous thread -
xlist<-list.files(pattern = NULL)
first=TRUE
cl <- rainbow(22)
for(i in xlist) {
table <- read.table((i),header=T,sep="\t")
table <- table[, 1:2]
if (first) {
plot(table,xlab='Distance from center',ylab='Coverage',ylim=c(0,70),col=1, type="n")
lines(table) #plot and add lines
legend("top", y=NULL, legend = i, col=1)
par(new=T)
first=FALSE
}
else {
lines(table,col=cl[i]) #To add another file to the plot as another line
par(new=F)
plotcol[i] <- cl[i]
legend("top", y=NULL, legend = i, col=plotcol)
}
}
The error is get is - Error in plotcol[i] <- cl[i] : object 'plotcol' not found. Please let me know what I am missing or if there is a better way to plot the lines with different colors and get legend with names of the files with the same color as the lines. Thank you.

I had to make some reproducible examples to get it to work, but the following script works to make the lengends and line colors the same:
#random data
test.df1=data.frame(runif(100)*0.2,runif(100)*0.2)
test.df2=data.frame(runif(100)*0.5,runif(100)*0.5)
test.df3=data.frame(runif(100),runif(100))
test.df4=data.frame(runif(100)*2,runif(100)*2)
test.list=list(test.df1=test.df1,test.df2=test.df2,test.df3=test.df3,test.df4=test.df4) # I used this instead of reading in files from read.table, you shouldn't need this
xlist=c('test.df1','test.df2','test.df3','test.df4') #the list of files
first=TRUE
cl <- rainbow(length(xlist)) #colors dedicated to your list
names(cl)=xlist #this names the vector elements so you can reference them
for(i in xlist) {
i.table <- test.list[[i]]
i.table <- i.table[,c(1:2)]
if (first) {
plot(i.table,xlab='Distance from center',ylab='Coverage',xlim=c(0,2),ylim=c(0,2),col=cl[i], type="n")
lines(i.table,col=cl[i]) #plot and add lines
par(new=T)
first=FALSE
}
else {
lines(i.table,col=cl[i]) #To add another file to the plot as another line
par(new=F)
plotcol <- c(plotcol,cl[i])# pulls colors from the cl vector
}
}
legend("top", y=NULL, legend =xlist, text.col=cl) #label colors should now match

Try ggplot2 package in R. You wouldn't have to code as much too!
https://www.rstudio.com/wp-content/uploads/2015/12/ggplot2-cheatsheet-2.0.pdf

Related

plot density of multiple csv files of different size in R

I have multiple csv files, each with a single column.
I want to read them and plot their density distribution in a single plot.
can anyone help me?
There are answers elsewhere about
reading multiple csv files so I will mainly concentrate on the density plotting part. Since you did not provide any data, I will use the built-in iris data to create some example files. This first step is to make a reusable example. I am assuming that you already have the data on the disk and have a list of the file names.
## Create some test data
FileNames = paste(names(iris[,1:4]), ".csv", sep="")
for(i in 1:4) {
write.csv(iris[,i], FileNames[i], row.names=FALSE)
}
So, on to the density plots. There is one small sticky point. Each of the different density plots will cover a different range of x and y values. If you want them all in one plot, you will need to leave enough room in your plot to hold them all. The code below first computes that range, then makes the plots.
## Read in all of the data from csv
DataList = list()
for(i in seq_along(FileNames)) {
DataList[[i]] = read.csv(FileNames[i], header=T)[[1]]
}
## Find the range that we will need to include all plots
XRange = range(DataList[[1]])
YRange = c(0,0)
for(i in seq_along(DataList)) {
Rx = range(DataList[[i]])
XRange[1] = min(XRange[1], Rx[1])
XRange[2] = max(XRange[2], Rx[2])
YRange[2] = max(density(DataList[[i]], na.rm=T)$y, YRange[2])
}
## Now make all of the plots
plot(density(DataList[[1]], na.rm=T), xlim=XRange, ylim=YRange,
xlab=NA, ylab=NA, main="Density Plots")
for(i in seq_along(DataList)) {
lines(density(DataList[[i]], na.rm=T), col=i)
}
legend("topright", legend=FileNames, lty=1, col=1:4, bty='n')

Prevent a plot to be overwrite in a for loop

I am trying to create three different plots in a for loop and then plotting them together in the same graph.
I know that some questions regarding this topic have already been asked. But I do not know what I am doing wrong. Why is my plot being overwritten.
Nevertheless, I tried both solutions (creating a list or using assign function) and I do not know why I get my plot overwriten at the end of the loop.
So, the first solution is to create a list:
library(gridExtra)
library(ggplot2)
out<-list()
for (i in c(1,2,4)){
print(i)
name= paste("WT.1",colnames(WT.1#meta.data[i]), sep=" ")
print(name)
out[[length(out) + 1]] <- qplot(NEW.1#meta.data[i],
geom="density",
main= name)
print(out[[i]])
}
grid.arrange(out[[1]], out[[2]], out[[3]], nrow = 2)
When I print the plot inside the loop, I get what I want...but of course they are not together.
First Plot
When I plot them all together at the end, I get the same plot for all of the three: the last Plot I did.
All together
This is the second option: assign function. I have exactly the same problem.
for ( i in c(1,2,4)) {
assign(paste("WT.1",colnames(WT.1#meta.data[i]),sep="."),
qplot(NEW.1#meta.data[i],geom="density",
main=paste0("WT.1",colnames(WT.1#meta.data[i]))))
}
You're missing to dev.off inside the loop for every iteration. Reproducible code below:
library(gridExtra)
library(ggplot2)
out<-list()
for (i in c(1,2,3)){
print(i)
out[[i]] <- qplot(1:100, rnorm(100), colour = runif(100))
print(out[[i]])
dev.off()
}
grid.arrange(out[[1]], out[[2]], out[[3]], nrow = 2)

R statistical Programing

I am trying to write R codes for the histogram plot and save each histogram separate file using the following command.
I have a data set "Dummy" and i want to plot each histogram by a column name and there will be 100 histogram plots in total...
I have the following R codes that draws the each Histogram...
library(ggplot2)
i<-1
for(i in 1:100)
{
jpeg(file="d:/R Data/hist.jpeg", sep=",")
hist(Dummy$colnames<-1, ylab= "Score",ylim=c(0,3),col=c("blue"));
dev.off()
i++
if(i>100)
break()
}
As a start, let's get your for loop into R a little better by taking out the lines trying to change i, your for loop will do that for you.
We'll also include a file= value that changes with each loop run.
for(i in 1:100)
{
jpeg(file = paste0("d:/R Data/hist", i, ".jpeg"))
hist(Dummy[[i]], ylab = "Score", ylim = c(0, 3), col = "blue")
dev.off()
}
Now we just need to decide what you want to plot. Will each plot be different? How will each plot extract the data it needs?
EDIT: I've taken a stab at what you're trying to do. Are you trying to take each of 100 columns from the Dummy dataset? If so, Dummy[[i]] should achieve that (or Dummy[,i] if Dummy is a matrix).

Put the name of the district on the plot

I am trying to make a plot of the intensity map of Ukraine's regions and intensity depends on the 'value'. Here is my code:
library(sp)
con <- url("http://biogeo.ucdavis.edu/data/gadm2/R/UKR_adm1.RData")
print(load(con))
close(con)
name<-VARNAME_1
gadm$VARNAME_1
value<-c(1:27)
gadm$VARNAME_1<-as.factor(value)
col<- colorRampPalette(c('white', 'black'))(256)
spplot(gadm, "VARNAME_1", main="Ukraine", scales = list(draw = TRUE), col.regions=col)
My question is: Is it possible to put on the plot the names of the regions (I have it as a character vector name in my code to the appropriate place on the map. Or maybe another suggestion to make a map more clear and understandable which region has corresponding value.
Thank you!
one possibility is to do it with colors :
colors=rainbow(length(gadm$NAME_1))
plot(gadm,col=colors)
legend("topleft",legend=gadm$NAME_1,fill=colors,cex=1.3,bty="n" )
or you add names with text :
colors=rainbow(length(gadm$NAME_1))
plot(gadm,col=colors)
text(coordinates(gadm), labels = gadm$NAME_1)
This solution uses plot instead of spplot so we can add labels with text. If you still want to use spplot, check
x11()
col = cm.colors(length(gadm$PID))
plot(gadm, , col=col[rev(gadm$VARNAME_1)])
text(coordinates(gadm), labels = gadm$NAME_1, cex=0.4)
Or if you still want to use spplot, be prepared to do a bit extra. Here's a modification of this answer
sp.label <- function(x, label) {
list("sp.text", coordinates(x), label)
}
NAME.sp.label <- function(x) {
sp.label(x, x$NAME_1)
}
draw.sp.label <- function(x) {
do.call("list", NAME.sp.label(x))
}
spplot(gadm, 'VARNAME_1', sp.layout = draw.sp.label(gadm))

Changing the legend in chartSeries to display values - Quantmod addTA

Is it possible to change the legend on the plot displayed in Quantmod so that values are displayed rather than the variable name? For example:
library("quantmod")
getSymbols("YHOO")
temp1 <- 6
temp2 <- "SMA"
barChart(YHOO)
addTA(ADX(YHOO, n=temp1, maType=temp2))
The legend that is displayed in the plot is ADX(YHOO, n=temp1, maType=temp2). I would like it to display the specific values instead i.e. ADX(YHOO, n=6, maType='SMA').
There isn't a way to do this automatically with addTA, because it would need to know which of the the parameters of the TA call it needs to evaluate. But you can do it manually by setting the legend= argument yourself.
One way to do it is to use paste (or paste0).
barChart(YHOO)
Legend <- paste0('ADX(YHOO, n=',temp1,', maType=',temp2,')')
addTA(ADX(YHOO, n=temp1, maType=temp2), legend=Legend)
Or you could create and manipulate the call to get what you want.
barChart(YHOO)
callTA <- call("ADX",quote(YHOO),n=temp1,maType=temp2)
eval(call("addTA", callTA, legend=deparse(callTA)))
The following is a partial solution which displays the values rather than variable names in the legend as well as the relevant output values for the TA. However, unlike the default settings of addTA, the text for each output value doesn't match the colour of the line on the addTA plot. Unfortunately I haven't worked out how to get the text of the output values to match the colour of its relevant line on the addTA plot. Any suggestions?
library("quantmod")
getSymbols("YHOO")
barChart(YHOO, subset="last 4 months")
col <- c("red", "blue", "green", "orange")
temp1 <- 8
temp2 <- "SMA"
temp <- ADX(HLC(YHOO), n=temp1, maType=temp2)
legend <- rep(NA, NCOL(temp)+1)
legend[1] <- paste("ADX(HLC(YHOO)", "n=", temp1, "maType=", temp2)
for(x in 2:(NCOL(temp)+1)){
legend[x] <- (paste(colnames(temp[,(x-1)]),": ", round(last(temp[,(x-1)]),3), sep=""))
}
addTA(temp, legend = legend, col=col)

Resources