Plot colour in Julia - julia

I am starting to learn Juliabox. I have a question about differentiating colour in Julia plot, based on feature value.
data is:
x = [2,4,3,5,3,2,1,5,6,4,3]
m = [0,0,0,1,1,0,1,1,1,0,1]
y = [23,32,43,23,12,54,34,43,56,76,34]
I want to plot points x-y and differentiate the point color based on value m.
I tried :
using PyPlot
plot(x,y,".", color=m)
and it doesn't work. Can anyone help?
Thanks,

Perhaps you want:
PyPlot.scatter(x,y,c=m)

Related

Julia: scatter plot with different colors (using either ColorGradient or other continuous color encodings)

Very simple question but I'm not very familiar with the way the ColorGradient type works so I thought I'd ask here.
Say I have some x, y data that I want to plot in the standard way with scatter(x,y) and I want to color the dots with their unique colors based on a color map determined by a color gradient (see code below):
using Plots
C(g::ColorGradient) = RGB[g[z] for z=LinRange(0,1,30)]
g = :inferno
(source: https://github.com/JuliaPlots/ExamplePlots.jl/blob/master/notebooks/cgrad.ipynb)
How would I set up my color gradient object and then use it to map each of my (x,y) pairs to a different one of the colors in the color gradient, using the scatter() function?
My guess is something like:
x, y = (rand(10), rand(10))
using Plots
C(g::ColorGradient) = RGB[g[z] for z=LinRange(0,1,10)]
g = :blue
scatter(x,y,c=cgrad(g)|> C)
I just tried that and it works, but I don't really get why...what is with this |> C notation??
The shortest way to get the plot is:
using Plots, Colors
scatter(x,y,c=colormap("Blues",10))
Another colormap worth mentioning is to have very different colors of points:
scatter(x,y,c=distinguishable_colors(10))
The |> operator is just passing an argument to a function so you can write either f(x) or x |> f. Consider the example below:
julia> f(a,b=5) = a+b;
julia> 7 |> f
12

how to plot this circular graph that represent up and down value?

I have one list that represent gene expression value, and the data has high expression and low expression relatively. I want to plot this circular graph that highly expressed outside the circle and lowly expressed inside the circle. So I would like to ask what program can achieve this picture. It would be best if an example can be given.
Using ggplot, this can be accomplished with geom_ribbon and coord_polar, it would look something like this:
ggplot(gene_data) + coord_polar() +
geom_ribbon(aes(ymin=low_exp,
ymax=high_exp
x=seq_num))
A common example of this has x = day_of_the_year and y = something_measured_that_day

Layered ScatterD3 plots?

Is it possible to overlay multiple layers of ScatterD3 plots on top of one another? I haven't been able to find this anywhere in either the vignettes or searching StackExchange/Google.
I'm curious, since folks have been able to make PCA Vector Loading plots using ScatterD3. If one could overlay this on top of another plot with the points (akin to what's possible with ggplot2 or ggvis layers), you could have a gorgeous and interactive PCA plot. Additionally, you might be able to outline points (since point stroke currently isn't an option).
Does anyone have any insight or workarounds?
It is possible, but more difficult. I would recommend using plotly package. You'll be able to use the View tab in the RStudio and more easily examine your 3D scatter by rotating. The color scheme is also easier to add This post attempts to tackle a similar, though not identical. A good (free) tutorial for plotly can be found here through DataCamp.
Question answered here thanks to the author of ScatterD3. To generate a full PCA plot, you need to redefine the dataframe being plotted like so:
library(FactoMineR)
library (ScatterD3)
out<-PCA(iris[,1:4],scale.unit = TRUE, graph=FALSE)
cc1<-data.frame(out$ind$coord)
cc2<-data.frame(out$var$coord)
points <- data.frame(x = cc1$Dim.1,
y = cc1$Dim.2,
color = iris$Species,
lab = row.names(iris),
type = rep("point", 150))
arrows <- data.frame(x = cc2$Dim.1,
y = cc2$Dim.2,
color = "Blue",
lab = row.names(cc2),
type = rep("arrow", 4))
data1 <- rbind(points, arrows)
scatterD3(data1, x = x, y = y,
lab = lab, type_var = data1$type, col_var = color)

Coloring scatterplot in R based on fold enrichment

I'm very new to R and have tried to search around for an answer to my question, but couldn't find quite what I was looking for (or I just couldn't figure out the right keywords to include!). I think this is a fairly common task in R though, I am just very new.
I have a x vs y scatterplot and I want to color those points for which there is at least a 2-fold enrichment, ie where x/y>=2 . Since my values are expressed as log2 values, the the transformed value needs to be x/y>=4.
I currently have the scatterplot plotted with
plot(log2(counts[,40], log2(counts[,41))
where counts is a .csv imported files and 40 & 41 are my columns of interested.
I've also created a column for fold change using
counts$fold<-counts[,41]/counts[,40]
I don't know how to incorporate these two pieces of information... Ultimately I want a graph that looks something like the example here: http://s17.postimg.org/s3k1w8r7j/error_messsage_1.png
where those points that are at least two-fold enriched will colored in blue.
Any help would be greatly appreciated. Thanks!
Is this what you're looking for:
# Fake data
dat = data.frame(x=runif(100,0,50), y = rnorm(100, 10, 2))
plot(dat$x, dat$y, col=ifelse(dat$x/dat$y > 4, "blue", "red"), pch=16)
The ifelse statement creates a vector of "blue" and "red" (or whatever colors you want) based on the values of dat$x/dat$y and plot uses that to color the points.
This might be helpful if you've never worked with colors in R.
Another option is to use ggplot2 instead of base graphics. Here's an example:
library(ggplot2)
ggplot(dat, aes(x,y, colour=cut(x/y, breaks=c(-1000,4,1000),
labels=c("<=4",">4")))) +
geom_point(size=5) +
labs(colour="x/y")

Clip the contour with polygon using ggplot and R

I want to create a contour and then clip the contour by the polygon and only show the contour within the polygon.
Shapefile data can be found here
Csv file can be found here
The code I used is as follows:
library("ggplot2")
library("rgdal")
library("gpclib")
library("maptools")
require(sp)
age2100 <- read.csv("temp.csv",header=TRUE, sep=",")
shape.dir <- "C:/Users/jdbaba/Documents/R working folder/shape" # use your directory name here
lon.shape <- readOGR(shape.dir, layer = "Export_Output_4")
str(lon.shape)
lon.df <- fortify(lon.shape, region = "Id")
p <- ggplot(lon.df, aes(x = long, y = lat, group = group)) +
geom_polygon(colour = "black", fill = "grey80", size = 1) +
theme()
p <- p + geom_point(data=age2100,aes(x=age2100$x,y=age2100$y,group="z"),size=0.1)
p <- p + geom_density2d(colour="red")
p
Here, I have created the map, points and the contour. I don't know whether the code I am using created the contour for variable z or not. If it is not correct can anyone suggest me ?
The sample output that I got is as follows:
Now, I want to clip the contour within the polygon and hide the part of contour that is outside the polygon.
I want to know how to add the labels to the contour and control the contour interval.
Please let me know if my question is not clear.
Thanks
Jdbaba
I can't reproduce your map exactly. The code you provided gives me a map with two sets of contours - one that looks like yours and one that overlaps it in the southern part of the region. I suspect this is an artefact of your group setting. Also, I can see there is an island in the southern part of what I assume is the lake.
I like to clean up and partition my ggplot stuff into bits, since I often find something in an early part of a ggplot call confuses something in a later part. Here's how I would map the region, draw points, and then add a density contour:
map <- function(){
geom_polygon(data=lon.df,aes(x=long,y=lat,group=piece),colour="black",fill="grey80",size=1)
}
points <- function(){
geom_point(data=age2100,aes(x=x,y=y),size=0.1)
}
density <- function(){
geom_density2d(data=age2100,aes(x=x,y=y),colour="red")
}
ggplot()+map() +points() +density()
Which gives this:
Now that's much different to what your contour looks like, and I don't know why. Maybe your group parameter is grouping all the points with the same z?
Anyway, it seems you don't want a density plot, you want a map of your Z values over your area. This is going to need kriging or some other interpolation technique. Forget about ggplot for a while, concentrate on the numbers.
For starters, plot the points coloured by the z value. You should see this:
which at least will give you a good idea of what the correct contour will look like.
Anyway, this is getting into a full-on tutorial..

Resources