how to use xticks in Gadfly spy function - plot

I'm using spy function from Gadfly package to plot a heatmap or a matrix of values.
and i want to show for each column a specific string but i cannot find the correct syntax
plt = Gadfly.spy(mtx, x= ["a","b","c","d"]) #suppose mtx has 4 columns
thanks!

Per the original request
using Gadfly
parameters = ["ξ₁", "η₁", "ξ₂", "η₂"]
spy(rand(4,4), Scale.y_discrete(labels = i->parameters[i]), Scale.x_discrete,
Guide.ylabel("Parameters"), Guide.xlabel("Mode"))

Related

Represent a colored polygon in ggplot2

I am using the statspat package because I am working on spatial patterns.
I would like to do in ggplot and with colors instead of numbers (because it is not too readable),
the following graph, produced with the plot.quadratest function: Polygone
The numbers that interest me for the intensity of the colors are those at the bottom of each box.
The test object contains the following data:
Test object
I have looked at the help of the function, as well as the code of the function but I still cannot manage it.
Ideally I would like my final figure to look like this (maybe not with the same colors haha):
Final object
Thanks in advance for your help.
Please provide a reproducible example in the future.
The package reprex may be very helpful.
To use ggplot2 for this my best bet would be to convert
spatstat objects to sf and do the plotting that way,
but it may take some time. If you are willing to use base
graphics and spatstat you could do something like:
library(spatstat)
# Data (using a built-in dataset):
X <- unmark(chorley)
plot(X, main = "")
# Test:
test <- quadrat.test(X, nx = 4)
# Default plot:
plot(test, main = "")
# Extract the the `quadratcount` object (regions with observed counts):
counts <- attr(test, "quadratcount")
# Convert to `tess` (raw regions with no numbers)
regions <- as.tess(counts)
# Add residuals as marks to the tessellation:
marks(regions) <- test$residuals
# Plot regions with marks as colors:
plot(regions, do.col = TRUE, main = "")

Error in axis(side = side, at = at, labels = labels, ...) : invalid value specified for graphical parameter "pch"

I have applied DBSCAN algorithm on built-in dataset iris in R. But I am getting error when tried to visualise the output using the plot( ).
Following is my code.
library(fpc)
library(dbscan)
data("iris")
head(iris,2)
data1 <- iris[,1:4]
head(data1,2)
set.seed(220)
db <- dbscan(data1,eps = 0.45,minPts = 5)
table(db$cluster,iris$Species)
plot(db,data1,main = 'DBSCAN')
Error: Error in axis(side = side, at = at, labels = labels, ...) :
invalid value specified for graphical parameter "pch"
How to rectify this error?
I have a suggestion below, but first I see two issues:
You're loading two packages, fpc and dbscan, both of which have different functions named dbscan(). This could create tricky bugs later (e.g. if you change the order in which you load the packages, different functions will be run).
It's not clear what you're trying to plot, either what the x- or y-axes should be or the type of plot. The function plot() generally takes a vector of values for the x-axis and another for the y-axis (although not always, consult ?plot), but here you're passing it a data.frame and a dbscan object, and it doesn't know how to handle it.
Here's one way of approaching it, using ggplot() to make a scatterplot, and dplyr for some convenience functions:
# load our packages
# note: only loading dbscacn, not loading fpc since we're not using it
library(dbscan)
library(ggplot2)
library(dplyr)
# run dbscan::dbscan() on the first four columns of iris
db <- dbscan::dbscan(iris[,1:4],eps = 0.45,minPts = 5)
# create a new data frame by binding the derived clusters to the original data
# this keeps our input and output in the same dataframe for ease of reference
data2 <- bind_cols(iris, cluster = factor(db$cluster))
# make a table to confirm it gives the same results as the original code
table(data2$cluster, data2$Species)
# using ggplot, make a point plot with "jitter" so each point is visible
# x-axis is species, y-axis is cluster, also coloured according to cluster
ggplot(data2) +
geom_point(mapping = aes(x=Species, y = cluster, colour = cluster),
position = "jitter") +
labs(title = "DBSCAN")
Here's the image it generates:
If you're looking for something else, please be more specific about what the final plot should look like.

R, ggplot2 qqplot using 2 vectors + straight line?

I have 2 data.frame objects:
df1
df2
Both have one column = amount.
For example:
df1 <- data.frame(amount = c(119.00,191.41,69.00,396.80,245.00,24.50,300.00,149.77,599.01,397.65))
df2 <- data.frame(amount = c(60.00,336.38,115.37,220.01,60.00,611.88,189.78,129.98,34.90,45.00))
I want to make a qqplot using both of them and add a y = x straight line to see if they have same distribution.
I am using qqplot(df1$amount, df2$amount) + abline() but it doesn't work: Error: ggplot2 doesn't know how to deal with data of class uneval
Please advise.
Also please explain me if I have an almost straight line in qqplot but I have a "level" there - what does it mean?
As has been pointed out, qqplot() and abline() are base R functions from the packages 'stats' and 'graphics'. There is no need to use + from the 'ggplot2' package.
It is more convenient to gather the data in a single data.frame.
df <- data.frame(
"Amount_X" = c(119.00,191.41,69.00,396.80,245.00,24.50,300.00,149.77,599.01,397.65),
"Amount_Y" = c(60.00,336.38,115.37,220.01,60.00,611.88,189.78,129.98,34.90,45.00)
)
A base R solution for the plot then would be as follows:
qqplot(df$Amount_X, df$Amount_Y)
abline(0,1)

Julia: Plot matrix with Gadfly.jl

I'm trying to plot a matrix with Gadfly, like I can do with PyPlot's matshow:
using PyPlot
p = eye(5)
p[5,5] = -1
matshow(p)
But I took a look at the docs, and found nothing. How can I do it with Gadfly?
Gadfly has a spy() function which does the same thing.
using Gadfly
p = eye(5)
p[end, end] = -1
spy(p)
You can check out the source for more information.

Partially superposing data in lattice's xyplot()

Please, reproduce this code:
install.packages('lattice')
install.packages('zoo')
require(lattice)
require(zoo)
X <- matrix(runif(25 * 8), ncol = 8)
(Its purpose is just to load packages and to create a matrix with 8 columns).
Using zoo it is very easy to create such a plot:
plot.zoo(X, screen = c(1,1,2,2,3,3,4,4), col = c(1,2))
How can I make the same with lattice's xyplot() function?
This can be done via zoo:::xyplot.zoo: as reported in zoo package documentation, xyplot.zoo has xyplot methods for time series objects.
Then, for what concerns the above question, it is possible to use:
xyplot(as.zoo(X), screen = c(1,1,2,2,3,3,4,4), col = c(1,2))
to produce a trellis object like in lattice selecting the desired layout with the screen argument.

Resources