My aim is creating a heatmap with a continuous legend. I tried a few paths and ended up with pheatmap. Legend is great, names of columns are ok, but the names of the rows don't show. I'd also like plot as squares not rectagles. Any ideas?
Related
I have created some code to generate histograms using apply on the state.x77 dataset.
attach(as.data.frame(state.x77))
lapply(X=c("Population","Income","Illiteracy","Life Exp","Murder","HS Grad","Frost","Area"),FUN=function(s)hist(state.x77[,s],main=paste("Hist of",s)))
It works fine with generating the titles for the graphs but I do not know how to create labels for the x and y-axis.
The y-axis has the label "frequency" which is fine but I want to make it so that each x-axis is labelled according to the corresponding variable in state.x77.
How would I generate the labels?
I found the answer
lapply(X=c("Population","Income","Illiteracy","Life Exp","Murder","HS Grad","Frost","Area"),FUN=function(s)hist(state.x77[,s],main=paste("Hist of",s),xlab=s))
Adding xlab=s works for the labels.
I'm trying to make a plot in R. My x-axis is a week number converted to factor and my y-axis is an amount.
When I run plot() instead of dots I get horizontal lines.
Why does this happen?
Here is a sample dataset:
df <- data.frame(fin_week=as.factor(seq(1,20, by =1)), amount=(rnorm(20)^2)*100)
plot(df)
Looking at the documentation, it's because the first column is a factor. When R tries to find the right plot() to run, it looks into plot.dataframe, where it plots on the type of 1st column i.e a factor. Hence it plots using plot.factor(), which gives a line by default, which is used for box plots.
try using plot.default(df) to plot and you should get it the scatter plot
I have made a scatterplot matrix which is conditioned on two factors, one with different colours and one with different shapes. I want to add a legend to the right-middle of the plot showing the labels for just the factor with the shapes (Scenario), is this possible? I have looked online but cannot figure it out! Here is my code, I would be grateful for any help. Thank you.
pairs(Data[,1:3], col=Data$Physician, pch=Data$Scenario, main="Scatterplot Matrix")
I have data in a csv file which I have imported to R. The data is in a test file available at http://www.cyclismo.org/tutorial/R/_static/trees91.csv
I have imported this using:
tree<-read.csv(file="trees91.csv",header=TRUE,sep=",");
I can then extract two rows as follows
m<-tree[1,4:28]
n<-tree[2,4:28]
I would then like to plot these two sets of data as a scatter graph. I am using the command:
plot(x,y)
However, this doesn't give me a scatter graph. Instead I get a plot with 25x25 small squares each with a small circle in. The ones on the diagonal contain a number in them. The left hand y-axis and top x-axis have the same labels (0.25,0.20, 0.25, 25, 25, 0.10, 0.5, 0.4, 0.08,0.15,0.10,0.10) whilst the other two axes have the labels (0.6,0.08,1.5,0.6, 12,0.15,0.1,0.8,0.08,0.04,0.20,0.08,0.08). I have tried this with both a header row and without a header row in the csv file (setting header =FALSE in the input command) and get the same problem.
Using the same approach but extracting two columns, I am able to plot a scatter graph, so I have no idea why R won't plot a scatter graph from rows in a csv file. This seems like a fairly basic thing to want to do.
Are you after this:
plot(unlist(m),unlist(n))
tree is a dataframe, and so are m and n as subsets of it. The default for dataframes is to plot each column against each column, so you get 25x25 plots as you saw. Unlist converts the dataframe to a vector, so you see the plotting behaviour that you might be expecting.
See:
?plot.default for what you want.
?plot.data.frame for what you're getting.
I have been trying to create a legend for an R plot with the rainbow option but I am facing some difficulties.
I plot
plot(test$a,test$b, col = rainbow(length(test$s))[rank(test$s)])
with the colour assigned according to test$s. The problem is that test$s is equal for many values of the data frame test so if than I write
legend('topright',legend=test.sub$s,col=rainbow(length(test.sub$s))
[rank(test.sub$s)])
I get in the legend all duplicates of test$s but the colours are correct. Since I don't want the duplicates I wrote
legend('topright',legend=unique(test.sub$s),col=rainbow(length(test.sub$s))
[rank(test.sub$s])
but then all the colours are messed up!
Thanks in advance
You're problem is that unique(test.sub$s) is not the same length as rainbow(length(test.sub$s))[rank(test.sub$s]. My solution would be to do:
col=rainbow(length(test.sub$s))[rank(test.sub$s)[!duplicated(test.sub$s)]]