3D plotting in R with categorical data - r

So I am fairly new to the whole 3D plotting in R thing, and have spent the last day reading up various articles and posts in here and other websites/books. And I have to confess, I am still lost. So any help/advice people can give me will be hugely appreciated!
Ok, so here is my issue (with example data). I have 3 Locations (A-C) sampled for 12 consecutive years (2001-2012) for some measure, with many zeros. So I have this data in a table/matrix/dataframe:
LocationA<-c(0,0,0.83,0.167,0.167,0.083,0.25,0.212,0,0.083,0,0)
LocationB<-c(0,0,0,0,0,0,0,0,0,0.212,0,0)
LocationC<-c(0.633,0.462,0.167,0.467,0,0.167,0.451,0.333,0.462,0.595,0,0)
TestMat<-cbind(LocationA,LocationB,LocationC)
rownames(TestMat)<-c(2001:2012)
What I would like would be similar to a topographical map, with the x-axis being the Location, z-axis the Year, and y-axis the value of the thing I am measuring. I would be potentially open to using bar-plots, but ideally something like wireframe() would be best. I have been messing around with various functions and packages, but I cant quite figure out why what I am doing is not working.
Thank you very much for your help! I am fully aware that I may just be a bit of an idiot and not getting it, so thank you for bearing with me!
Cheers!
EDIT:
So I changed it to a "long-read". The x-values couldnt be non-numeric, so I changed them to x2 (as shown below).
x<-c(rep("LocationA",12),rep("LocationB",12),rep("LocationC",12))
y<-rep(c(2001:2012),3)
z<-c(LocationA,LocationB,LocationC)
x2<-c(rep(1,12),rep(2,12),rep(3,12))
I then used the wireframe() function:
wireframe(z~x2*y,xlab="Location",ylab="Year",zlab="Value",distance=0.4,zoom=0.6,
scales=list(arrows=FALSE,
x=list(at=c(1:3),label=c("A","B","C")),
y=list(at=c(2001:2012),label=c(2001:2012)),
z=list(at=c(0,0.2,0.4,0.6,0.8,1),label=c(0,0.2,0.4,0.6,0.8,1)),
distance=c(2,2,2)),
drape=TRUE)
Now, the thing I cannot figure out, and have been fiddling with, is the colouring. I know that the drape() sub-function allows me to give the lattice a gradient of colour changes, with the higher values showing a different colour to the lower ones. However, I cannot seem to get it to work. It does give me a colour gradation, but the highest value (in this is 0.83) which should be a turquoise colour (the default colour gradient is pink-turquoise - will attempt to change this later), is in fact the colour corresponding to ~0.3 value. There is no error message or warning message attached, and I have no idea why this is happening... Any ideas?
Thanks again! :)

Related

My bar graph is not producing the correct results, any ideas what went wrong?

To keep this rather simple, I am using this function to see which countries make the highest rated chocolate bars, according to the course I am taking there should only have been a handful of bars that should have been created, however I ended up with a great many of them, I have a picture of the code and the output included in this. Any help would be greatly appreciated!
Sorry about the labels all scrunching together, I don't understand what went wrong I followed everything as prompted.
I entered the code as seen in the picture above, now the output should only have given a few countries, however it is listing all of them, I am fairly certain that the course itself was flawed and needs to be corrected, I am just curious as to what the correct code would be for this.

heatmap.2 color legend custom bins

Hi there stackoverflow community!
I am a graduate student inquiring for some consultation on an aethetics R problem I am encountering.
The data I am working with is in the form of a VERY large matrix (49x51).
My problem is that my data ranges from very small to very large, with the bulk of my data falling within the "very large" end of the spectrum, so unless I convert my data to log10, the heatmap is rather boring and almost entirely the same color.
The spectrum of my data is totally within the range I am expecting, but I am hoping to display it in a more aesthetic way.
Proposed solution: I think I need to bin my data in a non-uniform way. If you look at the attached image, you will see that their heatmap looks nice and the color key shows the heat spectrum in a non-fixed bin format. I would like to do something like that, however, I am not sure how to declare cutoffs for each bin. I would ideally like to declare the cutoffs.
For example, bin 1 (0-1), bin 2 (2-50), bin 3 (51-5000). As you can see, my bins would not be fixed in equal increments.
I have been using heatmap.2 for this. Thanks so much in advance!
heatmap with color legend in non-uniform bins:
Hey #Punintended and #S Rivero,
I think I have reached the point that my heatmap will only improve marginally. Both of you contributed deeply to this success, so thanks! First, to condense the matrix values as much as possible, I normalized by column. I was then able to assign gradients. This turned out much better than I had hoped. As you can see, most of my data is clustered (check out the density in the key) at very low values, this is okay though, for I am interested in the higher values. I had to use custom color gradients to account for possible instances of colorblind attendees that might look at my poster. Anyways, if you guys have comments or recommendations, they will be much appreciated :). Again, thanks a bunch!
enter image description here

Rarefaction curves in RStudio. How do I add labels to the end of the lines?

I am using R to do a simple rarecurve, it makes a nice graph and i can colour and add lables to the axis or title. But I can not get it to show the plot labels.
Someone help?
Below is what I started with.
library(vegan)
rarecurve(orders, step=1, xlab="Individuals", ylab="Orders")
When I change
rarecurve(orders, step=1, xlab="Individuals", ylab="Orders", label=TRUE)
Nothing changes on the graph. I just want a label at the end of each plot saying what row it is from?
I've seen others who have used Vegan and gotten lables on their data, I have tried copying exactly what they have done and I still get no labels. Is there something with the data I need to change?
Thank you to those who viewed my question. For those of you who are also new here is the answer! Hopefully it saves you 12 hours of your life..
I made sure the excel file I used had row names in the first column. Then when loading the data into R I used:
dataname<-read.csv(file="nameofexcel.csv", header = TRUE, row.names=1)
This last bit was not explained and took a lot of playing to figure out. Sorry it is a amateur error but that is what we all are when we start out. Well many of us.

Creating a grid on a map in R using grid points

I'm clearly struggling with this problem for a day now and can't seem to find a nice solution to it. I would really appreciate some help and I'm really a novice in R (since last week).
Problem 1:
I have a set CSV representing grid points which I can parse into a data frame (pointname, latitude, longitude).
Eg:
name,latitude,longitude
x0y0,35.9767,-122.605
x1y0,35.9767,-122.594
x2y0,35.9767,-122.583
x0y1,35.9857,-122.605
x1y1,35.9857,-122.594
x2y1,35.9857,-122.583
x0y2,35.9947,-122.605
x1y2,35.9947,-122.594
x2y2,35.9947,-122.583
The points in this file represent the lower left corner and are arranged in row major format, meaning lowest horizontal grid points first. Each point is a certain great circle distance away from its neighbors (1km). I want to create a grid overlay on a map which I've plotted using ggmap.
What I've tried or considered:
map.grid() - this is really not useful to me as I'm not looking for any kind of projection.
geom_vline() and geom_hline(). These look good but I don't have constant x and y intercepts on a plane. Moreover, once I create a grid, I'd like to use the grid to color against a density.
geom_rect() and geom_tile(). These look really promising and may be what I want. But I'm not able to find a good way of working with these.
I'd like to fill these grid boxes later with another parameter. Any suggestions on how I can create such a grid? This may be a trivial question but I don't know a lot of R yet.
Problem 2:
How can I store or hold such a grid so that I given a point (lat,lon), I can quickly get to that grid. In fact my whole back end is in C++ and can directly output the grid name x<n>y<n> directly against a given search point. I somehow am finding it difficult to count such points against grid points so that I can fill grid with a representative color.
I'm not sure if everything of what I'm saying is clear. Please tell me if I've to clarify something.
Also note that I've Googled quite a lot and not found relevant answers although some looked close.
Eg: This, ThisToo
Thanks for the help!

Building a heatmap in R with more information

I made a heatmap on R and most of it is one colour. I have two columns of data which showed up as various colours, but the rest of it is red.
Does anyone know how to increase the "resolution" of this? I don't mean anything about how to make the image more clear (which is why I think I'm having trouble searching for info on it). I mean, how do I make my heatmap more meaningful and not all mostly one colour.
Thanks and sorry if this has been answered somewhere else. I think I don't know the key term I need to search properly.
Edit:
Here is the code I used so far (heatdata is my matrix):
heatmap <- heatmap(heatdata,Rowv=NA,Colv=NA,col=cm.colors(256),scale="row")

Resources