Control contour line transparency plot.kde - r

I'm using plot.kde in library(ks) to extract contour levels of kernel density plots. I'd like to overlay multiple plots so I'm making the contour fills semi-transparent. However, there is a border/contour line whose color I just can't seem to control.
I have tried changing all of the different col,cont.col,color (etc) options in the plot.kde function and just can't seem to hone in on the color of the contour itself. I could probably use some work around by extracting the coordinates of the contour from the kde object and then plotting this using the polygon() function, but I'd really like to control this from within plot.kde. It's something I'll be running many times.
This is likely super simple but I'm just missing it! In the figure below, it's the thicker red line I'm trying to control.
Thanks!!!
library(ks)
data(unicef)
H.scv <- Hscv(x=unicef)
fhat <- kde(x=unicef, H=H.scv, compute.cont=TRUE)
plot(fhat, display="filled.contour2", cont=c(10),col=c(NA,rgb(1,0,0,0.5)))

Related

Cicular contour map using Plotly

I am trying to create contour plots of film thicknesses on a wafer using plotly, but would like the outputted plot to be a circle since it is a wafer instead of the default square. Do I have to somehow overlay a circle on to the plot and then exclude anything outside of it after the plot is generated? I prefer using plotly if possible since it looks nice. I've tried using ggplot as well but for some reason my data doesn't work with it since the x and y coordinates are apparently irregularly spaced. I've searched around but have not seen any results at least using R.
Thanks!

How to move the legend to outside the plotting area in Plots.jl (GR)?

I have the following plot where part of the data is being obscured by the legend:
using Plots; gr()
using StatPlots
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)))
I can see that using the "legend" attribute, the legend can be moved to various locations within the plotting area, for example:
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
Is there any way of moving the plot legend completely outside the plotting area, for example to the right of the plot or below it? For these kinds of stacked bar plots there's really no good place for the legend inside the plot area. The only solution I've been able to come up with so far is to make some "fake" empty rows in the input data matrix to make space with some zeros, but that seems kind of hacky and will require some fiddling to get the right number of extra rows each time the plot is made:
groupedbar(vcat(rand(1:100,(10,10)),zeros(3,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
I can see that at there was some kind of a solution proposed for pyplot, does anyone know of a similar solution for the GR backend? Another solution I could imagine - is there a way to save the legend itself to a different file so I can then put them back together in Inkscape?
This is now easily enabled with Plots.jl:
Example:
plot(rand(10), legend = :outertopleft)
Using layouts I can create a workaround making a fake plot with legend only.
using Plots
gr()
l = #layout [a{0.001h}; b c{0.13w}]
values = rand(1:100,(10,10))
p1 = groupedbar(values,bar_position=:stack, legend=:none)
p2 = groupedbar(values,bar_position=:stack, label="item".*map(string,collect(1:10)), grid=false, xlims=(20,3), showaxis=false)
p0=plot(title="Title",grid=false, showaxis=false)
plot(p0,p1,p2,layout=l)

cluster: :clusplot axis in wrong direction

I'm trying to plot the cluster obtained from fuzzy c-means clustering.
The plot should look like this.
code for the plot
plot(data$Longitude, data$Latitude, main="Fuzzy C-Means",col=data$Revised, pch=16, cex=.6,
xlab="Longitude",ylab="Latitude")
library(maps)
map("state", add=T)
However, when I tried to use clusplot the plot is displaying in opposite direction(both top and bottom and left and right) as below.
I wanna know if there's a way to reverse the plot to show in the order as the above picture.
Also, for the very dense area, it's hard to find the ellipse label. I wanna know if there's a way to show the label inside the ellipse instead of outside.
code for 2nd pic
library(cluster)
clusplot(cbind(Geocode$Longitude, Geocode$Latitude), cluster, color=TRUE,shade=TRUE,
labels=4, lines=0,col.p=cluster,
xlab="Longitude",ylab="Latitude",cex=1)
clusplot is a function that performs a lot of magic for you. In particular it projects the data set - which happens in a way you don't like, unfortunately. (Also note the scales - it centered and scaled the data, too)
clusplot.default: Creates a bivariate plot visualizing a partition (clustering) of the data. All observation are represented by points in the plot, using principal components or multidimensional scaling.
As far as I can tell, clusplot doesn't have map support, but you will want such a map I guess...
While maybe you can use the s.x.2d parameter to specify the exact projection (and this way disable automatic scaling), it probably is still difficult to add the map. Maybe look at the source of clusplot instead, and take only the parts you want?

R - Scatter plots, how to plot points in differnt lines to overlapping?

I want to plot several lists of points, each list has distance (decimal) and error_no (1-8). So far I am using the following:
plot(b1$dist1, b1$e1, col="blue",type="p", pch=20, cex=.5)
points(b1$dist2, b1$e2, col="blue", pch=22)
to add them both to the same plot. (I will add legends, etc later on).
The problem I have is that points overlap, and even when changing the character using for plotting, it covers up previous points. Since I am planning on plotting a lot more than just 2 this will be a big problem.
I found some ways in:
http://www.rensenieuwenhuis.nl/r-sessions-13-overlapping-data-points/
But I would rather do something that would space the points along the y axis, one way would be to add .1, then .2, and so on, but I was wondering if there was any package to do that for me.
Cheers
M
ps: if I missed something, please let me know.
As noted in the very first point in the link you posted, jitter will slightly move all your points. If you just want to move the points on the y-axis:
plot(b1$dist1, b1$e1, col="blue",type="p", pch=20, cex=.5)
points(b1$dist2, jitter(b1$e2), col="blue", pch=22)
Depends a lot on what information you wish to impart to the reader of your chart. A common solution is to use the transparency quality of R's color specification. Instead of calling a color "blue" for example, set the color to #0000FF44 (Apologies if I just set it to red or green) The final two bytes define the transparency, from 00 to FF, so overlapping data points will appear darker than standalone points.
Look at the spread.labs function in the TeachingDemos package, particularly the example. It may be that you can use that function to create your plot (the examples deal with labels, but could just as easily be applied to the points themselves). The key is that you will need to find the new locations based on the combined data, then plot. If the function as is does not do what you want, you could still look at the code and use the ideas to spread out your points.
Another approach would be to restructure your data and use the ggplot2 package with "dodging". Other approaches rather than using points several times would be the matplot function, using the col argument to plot with a vector, or lattice or ggplot2 plots. You will probably need to restructure the data for any of these.

Plotting a box within filled.contour plots in R?

I'm trying to plot a box within a filled.contour plot, but unfortunately, when I plot the lines() after the filled.contour plot is created, the figure is shifted to the right because the scale forces the image to the left, but the box stays at the same coordinates. Here's what my code looks like:
dev.new(width=6,height=7)
mypredict<-matrix(data=mypredict,nrow=20,ncol=25)
filled.contour(x=seq(from=-1.5,to=1.5,length=20),
y=seq(from=1,to=3.75,length=25),
z=mypredict,
col=hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1)
)
top <- 3.42
bot <- 1.56
lines(c(-1,-1),c(bot,top))
lines(c(1,1),c(bot,top))
lines(c(-1,1),c(top,top))
lines(c(-1,1),c(bot,bot))
Does anyone know how I can plot those lines within the filled.contour function? Otherwise, the lines do not plot correctly onto the main image, since the scale/legend of the graph is placed on the right.
Thanks!
The manual page for filled.contour explains the problem (and gives a solution)
This function currently uses the ‘layout’ function and so is restricted
to a full page display. As an alternative consider the ‘levelplot’
and ‘contourplot’ functions from the ‘lattice’ package which work in
multipanel displays.
The output produced by ‘filled.contour’ is actually a combination
of two plots; one is the filled contour and one is the legend.
Two separate coordinate systems are set up for these two plots,
but they are only used internally - once the function has returned
these coordinate systems are lost. If you want to annotate the
main contour plot, for example to add points, you can specify
graphics commands in the ‘plot.axes’ argument. An example is
given below.
So essentially you pass some instructions as the plot.axes parameters to override standard behaviour.
In your example:
filled.contour(x = seq(from=-1.5,to=1.5,length=20),
y = seq(from=1,to=3.75,length=25), z = mypredict,
col = hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1),
plot.axes = {axis(1); axis(2); rect(left, bottom, right, top);})
Note that you have to recreate the two axes otherwise they will not be drawn. Also, no need to use the lines statement, when there is a rect function! :)
Hope this helps

Resources