how to control transparency of ppp density plot - spatstat

I am trying to use the layered methods to overlay few spatstat spatial objects. All these objects are for the same window. I have an im layer (density) from a ppp. I want to make this layer a bit transparent in order to have a better visibility of the other objects in the layered object.
How can I control the transparency of this density plot (im)? Is there something like alpha or transparency parameter for the plot.im ?
UPDATE:
library(spatstat)
pipes=simplenet
plot(pipes)
point_net = as.ppp(runifpoint(10, win = Window(pipes)))
point_surface = density(point_net)
plot(point_surface)
layers= layered(point_surface, point_net, pipes)
plot(layers)
Here , I have plotted 3 layers. As you can see the density plot has very dark blues and reds. Yes, I can plot lines and points with different colours to make them visible, but it would nice to do simple stacked line, point plots and add a little bit of transparency to the density (im) plots.
The purpose is just to avoid complex customized plot colours and to explain to colleagues.
thank you.

First the commands from the original post:
library(spatstat)
pipes=simplenet
point_net = as.ppp(runifpoint(10, win = Window(pipes)))
point_surface = density(point_net)
layers= layered(point_surface, point_net, pipes)
plot(layers)
You need to provide a different colourmap to plot.im. There are two
ways you can do this:
Plot each layer individually using add = TRUE for subsequent
layers and provide the colour map when you plot the im object.
Pass a list of plot arguments when you plot the layered object you
have created above.
I find the first option easier for illustration, so I will do that
first. The default colourmap of spatstat is the 29th Kovesi colour
sequence (?Kovesi for more details on these sequences):
def_col <- Kovesi$values[[29]]
head(def_col)
#> [1] "#000C7D" "#000D7E" "#000D80" "#000E81" "#000E83" "#000E85"
To add transparency you can use to.transparent with your choice of
fraction for more/less transparency:
def_col_trans <- to.transparent(def_col, fraction = 0.7)
head(def_col_trans)
#> [1] "#000C7DB3" "#000D7EB3" "#000D80B3" "#000E81B3" "#000E83B3" "#000E85B3"
Now you just need to use this as your colourmap:
plot(point_surface, col = def_col_trans)
plot(point_net, add = TRUE)
plot(pipes, add = TRUE)
To do it with the layered object you have to make a list of plot
argument lists (containing NULL if you don't have additional
arguments):
layer_args <- list(list(col = def_col_trans),
list(NULL),
list(NULL))
plot(layers, plotargs = layer_args)

Related

Shared axis labels with independent scale

When facet/concat-ing charts, I would like the axis labels to be shared (so only 1 label per column/row, here: Horsepower), but the scale to be independent. Is this possible?
I thought a combination of resolve_axis and resolve_scale would be the way to go, as the title is a part of Axis, but I didn't get it to work.
I'm also wondering what resolve_axis actually does different than resolve_scale, anyone has an example?
base = alt.Chart(source).mark_circle().encode(
x=alt.X('Horsepower:Q',),
y=alt.Y('Miles_per_Gallon:Q'),
color='Origin:N',
row=alt.Row('Origin:N'),
).properties(
width=200, height=100
)
base.resolve_axis(
x='shared' # doesn't do anything obvious
).resolve_scale(
x='independent'
)
Open the Chart in the Vega Editor
I found a hacky way to do this, by misusing the facet header:
base = alt.Chart(source).mark_circle(size=60).encode(
x=alt.X('Horsepower:Q',),
y=alt.Y('Miles_per_Gallon:Q',
axis=alt.Axis(title=''),),
color='Origin:N',
column=alt.Column('Origin:N', header=alt.Header(title='Miles_per_Gallon')),
).properties(
width=200, height=200
).configure_header(
labelExpr="['Origin',datum.value]",
titleOrient='left'
)
display(base.resolve_scale(y='shared'))
display(base.resolve_scale(y='independent'))
I don't know of any way to do what you're hoping for (independent scales with only a single outer axis title) via scale and guide resolution.
As to your question of the difference between resolve_scale and resolve_axis, an example may help.
Here's a chart with independent y scale:
import altair as alt
from vega_datasets import data
source = data.cars()
base = alt.Chart(source).mark_circle().encode(
x=alt.X('Horsepower:Q',),
y=alt.Y('Miles_per_Gallon:Q'),
color='Origin:N',
column=alt.Column('Origin:N'),
).properties(
width=150, height=150
)
base.resolve_scale(
y='independent'
)
And here's one with independent y axis:
base.resolve_axis(
y='independent'
)
In both cases, each chart gets its own axis (because independent scales imply independent axes), but only with an independent scale do the axes scales differ from each other.

Change color and legend of plotLearnerPrediction ggplot2 object

I've been producing a number of nice plots with the plotLearnerPrediction function in the mlr package for R. They look like this. From looking into the source code of the plotLearnerPrediction function it looks like the color surfaces are made with geom_tile.
A plot can for example be made by:
library(mlr)
data(iris)
#make a learner
lrn <- "classif.qda"
#make a task
my.task <- makeClassifTask(data = iris, target = "Species")
#make plot
plotLearnerPrediction(learner = lrn, task = my.task)
Now I wish to change the colors, using another red, blue and green tone to match those of some other plots that I've made for a project. for this I tried scale_fill_continuous and scale_fill_manual without any luck (Error: Discrete value supplied to continuous scale) I also wish to change the legend title and the labels for each legend entry (Which I tried giving appropriate parameters to the above scale_fill's). There's a lot of info out there on how to set the geom_tile colours when producing the plot, but I haven't found any info on how to do this post-production (i.e. in somebody else's plot object). Any help would be much appreciated.
When you look into the source code you see how the plot is generated and then you can see which scale has to be overwritten or set.
In this example it's fairly easy:
g = plotLearnerPrediction(learner = lrn, task = my.task)
library(ggplot2)
g + scale_fill_manual(values = c(setosa = "yellow", versicolor = "blue", virginica = "red"))

Setting multiple axvspan labels as one element in legend

I am trying to set up a series of vertical axis spans to symbolize different switching positions at different times. For example, in the figure below, switching position 1 (green) happens quite a few times, alternating between other positions.
I plot these spans running a for loop in a list of tuples, each containing the initial and final indexes of each interval to plot the axvspan.
def plotShades(timestamp, intervals, colour):
for i in range(len(intervals)):
md.plt.axvspan(timestamp[intervals[i][0]], timestamp[intervals[i][1]], alpha=0.5, color=colour, label="interval")
This function is then called upon another one, that plots the shades for each different switching position:
def plotAllOutcomes(timestamp, switches):
#switches is a list of 7 arrays indicating when the switcher is at each one of the 7 positions. If the array has a 1 value, the switcher is there. 0 otherwise.
colors = ['#d73027', '#fc8d59', '#fee08b', '#ffffbf', '#d9ef8b', '#91cf60', '#1a9850']
intervals = []
for i in range(len(switches)):
intervals.append(getIntervals(switches[i], timestamp))
plotShades(timestamp, intervals[i], colors[i])
md.plt.legend()
Doing so with the code snippets I've put here (not the best code, I know - I'm fairly new in Python!) the legend ends up having one item for each interval, and that's pretty awful. This is how it looks:
I'd like to get a legend with only 7 items, each for a single color in my plot of axvspans. How can I proceed to do so? I've searched quite extensively but haven't managed to find this situation being asked before. Thank you in advance for any help!!
A small trick you can apply using the fact that labels starting with "_" are ignored:
plt.axvspan( ... , label = "_"*i + "interval")
Thereby a label is only created for the case where i==0.

two textplots in one plot

I have been trying to work with textplot in R and am unsure if my question is possible or not, I know that par() can't be used to place two textplots in one plot. I have been using a page and this code to try and figure things out.
My question is: Is it possible to have two textplots within the same plot?
For example, in the par(mfrow=c(1,1)) scenario below, plot 1 is a texplot of species length. Say I wanted to replicate that textplot twice in that plot. Is that possible?
based on this site:
http://svitsrv25.epfl.ch/R-doc/library/gplots/html/textplot.html
textplot(version)
data(iris)
par(mfrow=c(1,1))
info <- sapply( split(iris$Sepal.Length, iris$Species),
function(x) round(c(Mean=mean(x), SD=sd(x), N=gdata::nobs(x)),2) )
textplot( info, valign="top" )
title("Sepal Length by Species")
What I want to do is put a second textplot within that plot, underneath the original. For arguments sake, replicating that textplot twice in the plot.
Is this possible?
Thanks!
Maybe you've figured it out in the last four months but I thought I'd chip in an answer anyway.
The code provided is most of the way towards doing what you require already, you just have to provide some additional inputs to title() and/or par(). Namely specify that the title is to be above both of the plots by using title("your title", outer = TRUE) and you can further adjust the position of the title with an option in par(), use par(mfrow = c(2,1), oma = c(0,0,"top",0)). Hopefully this answers your question.
require('gplots')
data(iris)
info <- sapply(split(iris$Sepal.Length, iris$Species),
function(x) round(c(Mean = mean(x), SD = sd(x), N = gdata::nobs(x)),2))
## Replace top with a numerical value to control the position of the title with respect to the
## top of the page.
par(mfrow = c(2,1), oma = c(0,0, top ,0))
textplot(info, valign = "top")
textplot(info, valign = "top")
title("Sepal Length by Species", outer = TRUE)

VTK: Put Label/Text near points in the 3d plot

I have written a code which plots multiple 3d points as spheres. I want to add some text near each sphere in 3D to mention some info about each point. But I have vtkPoints to store points positions which doesn't have GetOutputPort which I need in labelMApper (and also used glyph3d to make spheres)
vtkSmartPointer<vtkLabeledDataMapper> labelMapper = vtkSmartPointer<vtkLabeledDataMapper>::New();
labelMapper->SetInputConnection( vtkpoints->GetOutputPort() ); // No GetOutputPort()
vtkSmartPointer<vtkActor2D> labelActor = vtkSmartPointer<vtkActor2D>::New();
labelActor->SetMapper(labelMapper);
renderer->AddActor(labelActor);
You should construct a vtkPolyData from the points and set it as Input to the label mapper.
Something like this:
vtkNew<vtkPolyData> labelPolyData;
labelPolyData->SetPoints(labelPoints);
labelMapper->SetInput(labelPolyData); // Note: If you're using VTK from master (6.x), this is SetInputData(...)
renderer->AddActor2D(labelActor);

Resources