I have a data table and I want to do the following:
1) use xyplot to plot the data
2) use rasterimage to 'mark' certain regions in this plot as 'good' (green) or 'bad' (red)
This is what I got so far:
library(lattice)
dataFrame = data.frame(
Z1 = c(0, 1, 2, 3, 4),
Z2 = c(0, 1, 2, 3, 4))
dataFrameResult = data.frame(
install=c(TRUE, TRUE, FALSE))
imageMatrix = matrix(
c(rgb(255, 0, 0, alpha=100, maxColorValue = 255 ),rgb(0, 255, 0, alpha=100, maxColorValue = 255 ),
rgb(255, 0, 0, alpha=100, maxColorValue = 255 ),rgb(0, 0, 255, alpha=100, maxColorValue = 255 )),
nrow = 2, ncol = 2, byrow = TRUE,)
image <- as.raster(imageMatrix)
fig = xyplot(Z1 ~ Z2, group = dataFrameResult$install, data=dataFrame)
plot.new()
print(fig, pos=c(0,0,1,1), more = TRUE)
par(new=TRUE)
plot(c(0, 3), c(0, 3), type = "n", xlab = "", ylab = "")
rasterImage(image, 0, 0, 1, 1, interpolate = FALSE)
This produces the following:
In principal it looks fine but the positioning of the rasterImage function and the positioning of the xyplot do not match up... So, instead of guessing and pushing them around (does this procedure depend on the scales, etc.?) I thought that it can't be that hard to draw an image into a plot... right?
So anybody knows how to achieve the image below with (0,0) being (0,0) in and (1,1) being (1,1) in both scales? Or, even better, is there a way to draw an xyplot and tell R to paint the background in a user specified function like so...
getColor = function(x,y) {
return(rgb(x, y, 0, 0, ...))
}
plot (backgroundColorFunction=getColor)
Cheers,
FW
an easier way is to use the normal plot function, and use the functions like rect() after your plot to mark the regions and points() to plot the data,
for example :
> plot(c(1, 5), c(0, 4), type= "n", xlab = "", ylab ="")
> rect( 2 ,3 , 3 , 4 , col ="green" , border="red" )
> points(c(1:5),c(0:4),col="blue")
> rect(1.8,2.8,2.3,3.4,col="white",border = "white")
that results :
you can customize your plot by changing the parameters and using your data inside this function. other functions to use after plot are :
plot.default, plot.window, points, lines, abline, axis, title, text, mtext, segments, symbols, arrows, polygon, rect, box, contour, filled.contour and image.
try search them in R help, easy to use :)
I've to mention that you can use this function for many times for one plot for example if you want to plot 2 rects just write two rect() function
Related
Using r, I would like to change the fill color of the data points in my multi-group radar chart, to indicate which effect sizes are significant versus nonsignificant.
a) Is there a way to change the fill color of individual data points? (ideal solution)
b) Alternatively, is there a way to fill all data points with white, while retaining the original outline colors and line colors? This would allow me to edit the image on a point by point basis outside of r. In the code below, I successfully changed the point type to empty circles using pty = 1, but I don't want the lines to show through underneath (practical solution).
Reproducible example:
set.seed(1)
df2 <- data.frame(rbind(rep(10, 8), rep(0, 8),
matrix(sample(0:10, 24,
replace = TRUE),
nrow = 3)))
colnames(df2) <- paste("Var", 1:8)
library(fmsb)
# Fill colors
areas <- c(rgb(1, 0, 0, 0.25),
rgb(0, 1, 0, 0.25),
rgb(0, 0, 1, 0.25))
radarchart(df2,
cglty = 1, # Grid line type
cglcol = "gray", # Grid line color
pcol = 2:4, # Color for each line
pty = 1, # Data point type
plwd = 2, # Width for each line
plty = 1, # Line type for each line
pfcol = areas) # Color of the areas
How can I fill the circles with white or keep the lines from showing through?
What are the alternatives for drawing a simple curve for a function like
eq = function(x){x*x}
in R?
It sounds such an obvious question, but I could only find these related questions on stackoverflow, but they are all more specific
Plot line function in R
Plotting functions on top of datapoints in R
How can I plot a function in R with complex numbers?
How to plot a simple piecewise linear function?
Draw more than one function curves in the same plot
I hope I didn't write a duplicate question.
I did some searching on the web, and this are some ways that I found:
The easiest way is using curve without predefined function
curve(x^2, from=1, to=50, , xlab="x", ylab="y")
You can also use curve when you have a predfined function
eq = function(x){x*x}
curve(eq, from=1, to=50, xlab="x", ylab="y")
If you want to use ggplot,
library("ggplot2")
eq = function(x){x*x}
ggplot(data.frame(x=c(1, 50)), aes(x=x)) +
stat_function(fun=eq)
You mean like this?
> eq = function(x){x*x}
> plot(eq(1:1000), type='l')
(Or whatever range of values is relevant to your function)
plot has a plot.function method
plot(eq, 1, 1000)
Or
curve(eq, 1, 1000)
Here is a lattice version:
library(lattice)
eq<-function(x) {x*x}
X<-1:1000
xyplot(eq(X)~X,type="l")
Lattice solution with additional settings which I needed:
library(lattice)
distribution<-function(x) {2^(-x*2)}
X<-seq(0,10,0.00001)
xyplot(distribution(X)~X,type="l", col = rgb(red = 255, green = 90, blue = 0, maxColorValue = 255), cex.lab = 3.5, cex.axis = 3.5, lwd=2 )
If you need your range of values for x plotted in increments different from 1, e.g. 0.00001 you can use:
X<-seq(0,10,0.00001)
You can change the colour of your line by defining a rgb value:
col = rgb(red = 255, green = 90, blue = 0, maxColorValue = 255)
You can change the width of the plotted line by setting:
lwd = 2
You can change the size of the labels by scaling them:
cex.lab = 3.5, cex.axis = 3.5
As sjdh also mentioned, ggplot2 comes to the rescue. A more intuitive way without making a dummy data set is to use xlim:
library(ggplot2)
eq <- function(x){sin(x)}
base <- ggplot() + xlim(0, 30)
base + geom_function(fun=eq)
Additionally, for a smoother graph we can set the number of points over which the graph is interpolated using n:
base + geom_function(fun=eq, n=10000)
Function containing parameters
I had a function (emax()) involving 3 parameters (a, b & h) whose line I wanted to plot:
emax = function(x, a, b, h){
(a * x^h)/(b + x^h)
}
curve(emax, from = 1, to = 40, n=40 a = 1, b = 2, h = 3)
which errored with Error in emax(x) : argument "a" is missing, with no default error.
This is fixed by putting the named arguments within the function using this syntax:
curve(emax(x, a = 1, b = 2, h = 3), from = 1, to = 40, n = 40)
which is contrary to the documentation which writes curve(expr, from, to, n, ...) rather than curve(expr(x,...), from, to, n).
Heads up I am an R noob so bare with please!
I am trying to identify areas where the most data points are situated using a density heatmap.
Here is what the plot looks like
What will I have to add to this code I've done so far (I listed less data points obviously) to apply this 'heatmap'?
par(mfrow=c(1, 2), mar=rep(0.3, 4))
TernaryPlot(atip = "Red", btip = "Green", ctip = "Blue", alab="Redder\u2192", blab="Greener \u2192", clab="Bluer \u2190",
point='Up', lab.cex=0.8, grid.minor.lines = 0,
grid.lty='solid', col=rgb(0.9,0.9,0.9), grid.col='White',
axis.col=rgb(0, 0, 0), ticks.col=rgb(0, 0, 0),
padding=0.08)
data_points <- list(
c(0.89,0.88,0.78),
c(0.98,0.96,0.92),
c(0.6,0.52,0.28),
c(0.88,0.9,0.85),
c(0.96,0.87,0.6),
c(0.63,0.53,0.29),
c(0.92,0.85,0.09),
c(0.84,0.87,0.87),
c(0.93,0.88,0.88),
c(0.98,0.76,0.71)
)
AddToTernary(points, data_points, bg=vapply(data_points, function (x) rgb(x[1], x[2], x[3], 1, maxColorValue=1), character(1)), pch=25, cex=0.8)
AddToTernary(text, data_points, names(data_points), cex=0.8, font=2)
Sorry for being so silly, very new to all this, learning lots thanks to this website!
The TernaryPlot function from the Ternary package creates and styles a blank Ternary plot. If you would like to show your data points, you need to use the TernaryPoints function:
TernaryPlot(atip = "Red", btip = "Green", ctip = "Blue", alab="Redder\u2192", blab="Greener \u2192", clab="Bluer \u2190",
point='Up', lab.cex=0.8, grid.minor.lines = 0,
grid.lty='solid', col=rgb(0.9,0.9,0.9), grid.col='White',
axis.col=rgb(0, 0, 0), ticks.col=rgb(0, 0, 0),
padding=0.08)
TernaryPoints(data_points)
You will get the following plot:
Another option is to use the vcd library:
library(vcd)
# convert data points into a matrix
data_points = matrix(unlist(data_points), ncol=3, byrow=T)
ternaryplot(x=data_points)
Using vcd, you should get the following plot:
What are the alternatives for drawing a simple curve for a function like
eq = function(x){x*x}
in R?
It sounds such an obvious question, but I could only find these related questions on stackoverflow, but they are all more specific
Plot line function in R
Plotting functions on top of datapoints in R
How can I plot a function in R with complex numbers?
How to plot a simple piecewise linear function?
Draw more than one function curves in the same plot
I hope I didn't write a duplicate question.
I did some searching on the web, and this are some ways that I found:
The easiest way is using curve without predefined function
curve(x^2, from=1, to=50, , xlab="x", ylab="y")
You can also use curve when you have a predfined function
eq = function(x){x*x}
curve(eq, from=1, to=50, xlab="x", ylab="y")
If you want to use ggplot,
library("ggplot2")
eq = function(x){x*x}
ggplot(data.frame(x=c(1, 50)), aes(x=x)) +
stat_function(fun=eq)
You mean like this?
> eq = function(x){x*x}
> plot(eq(1:1000), type='l')
(Or whatever range of values is relevant to your function)
plot has a plot.function method
plot(eq, 1, 1000)
Or
curve(eq, 1, 1000)
Here is a lattice version:
library(lattice)
eq<-function(x) {x*x}
X<-1:1000
xyplot(eq(X)~X,type="l")
Lattice solution with additional settings which I needed:
library(lattice)
distribution<-function(x) {2^(-x*2)}
X<-seq(0,10,0.00001)
xyplot(distribution(X)~X,type="l", col = rgb(red = 255, green = 90, blue = 0, maxColorValue = 255), cex.lab = 3.5, cex.axis = 3.5, lwd=2 )
If you need your range of values for x plotted in increments different from 1, e.g. 0.00001 you can use:
X<-seq(0,10,0.00001)
You can change the colour of your line by defining a rgb value:
col = rgb(red = 255, green = 90, blue = 0, maxColorValue = 255)
You can change the width of the plotted line by setting:
lwd = 2
You can change the size of the labels by scaling them:
cex.lab = 3.5, cex.axis = 3.5
As sjdh also mentioned, ggplot2 comes to the rescue. A more intuitive way without making a dummy data set is to use xlim:
library(ggplot2)
eq <- function(x){sin(x)}
base <- ggplot() + xlim(0, 30)
base + geom_function(fun=eq)
Additionally, for a smoother graph we can set the number of points over which the graph is interpolated using n:
base + geom_function(fun=eq, n=10000)
Function containing parameters
I had a function (emax()) involving 3 parameters (a, b & h) whose line I wanted to plot:
emax = function(x, a, b, h){
(a * x^h)/(b + x^h)
}
curve(emax, from = 1, to = 40, n=40 a = 1, b = 2, h = 3)
which errored with Error in emax(x) : argument "a" is missing, with no default error.
This is fixed by putting the named arguments within the function using this syntax:
curve(emax(x, a = 1, b = 2, h = 3), from = 1, to = 40, n = 40)
which is contrary to the documentation which writes curve(expr, from, to, n, ...) rather than curve(expr(x,...), from, to, n).
When I plot several 3D images using plot3d from the rgl package, the images are displayed separately. I want to show them in one plot, as when using, e.g., par(mfrow=c(2, 2)) to display four 2D images in a single plot window.
Is this possible?
The command layout3d might be useful. Maybe this code can help:
shapes <- list(Tetrahedron = tetrahedron3d(), Cube = cube3d(), Octahedron = octahedron3d(),
Icosahedron = icosahedron3d(), Dodecahedron = dodecahedron3d(),
Cuboctahedron = cuboctahedron3d())
col <- rainbow(6)
open3d()
mat <- matrix(1:4, 2, 2)
mat <- rbind(mat, mat + 4, mat + 8)
layout3d(mat, height = rep(c(3, 1), 3), sharedMouse = TRUE)
for (i in 1:6) {
next3d()
plot3d(shapes[[i]], col = col[i])
next3d()
text3d(0, 0, 0, names(shapes)[i])
}
To deactivate the rotation of all the solids together, it is enough to put sharedMouse = FALSE.