I have a lattice xyplot with smoothed lines (6 different lines). I would like to change the line types- color and type of line (dashed, etc), so that they are understandable in B&W, rather than in color (which is the default). Can anyone provide advice on this? Below is my current code:
xyplot(y~x,
data=df,
group=categorical,
type = "smooth",
ylim=c(-2,0.5),
xlab="x",
ylab="y",
auto.key=list(space="top",
columns=3,
title="",
cex.title=0.1,
lines=FALSE, points=TRUE)
)
Thank you
There are two options. Either you just set the line type with lty = 1:x or you use the built in black-and-white theme -- the latter will set up a bunch of other settings as well.
library(lattice)
y <- c(rnorm(10), rnorm(10, 2, 0.2), rnorm(10, 1.5, 0.4))
x <- rep(1:10, times = 3)
z <- rep(letters[1:3], each = 10)
# Option 1
xyplot(y ~ x, groups = z, type = "l",
par.settings = standard.theme(color = FALSE))
# Option 2
xyplot(y ~ x, groups = z, type = "l", lty = 1:3, col = "black")
Related
I am planning to reproduce the attached figure, but I have no clue how to do so:
Let´s say I would be using the CO2 example dataset, and I would like to plot the relative change of the Uptake according to the Treatment. Instead of having the three variables in the example figure, I would like to show the different Plants grouped for each day/Type.
So far, I managed only to get this bit of code, but this is far away from what it should look like.
aov1 <- aov(CO2$uptake~CO2$Type+CO2$Treatment+CO2$Plant)
plot(TukeyHSD(aov1, conf.level=.95))
Axes should be switched, and I would like to add statistical significant changes indicated with letters or stars.
You can do this by building it in base R - this should get you started. See comments in code for each step, and I suggest running it line by line to see what's being done to customize for your specifications:
Set up data
# Run model
aov1 <- aov(CO2$uptake ~ CO2$Type + CO2$Treatment + CO2$Plant)
# Organize plot data
aov_plotdata <- data.frame(coef(aov1), confint(aov1))[-1,] # remove intercept
aov_plotdata$coef_label <- LETTERS[1:nrow(aov_plotdata)] # Example labels
Build plot
#set up plot elements
xvals <- 1:nrow(aov_plotdata)
yvals <- range(aov_plotdata[,2:3])
# Build plot
plot(x = range(xvals), y = yvals, type = 'n', axes = FALSE, xlab = '', ylab = '') # set up blank plot
points(x = xvals, y = aov_plotdata[,1], pch = 19, col = xvals) # add in point estimate
segments(x0 = xvals, y0 = aov_plotdata[,2], y1 = aov_plotdata[,3], lty = 1, col = xvals) # add in 95% CI lines
axis(1, at = xvals, label = aov_plotdata$coef_label) # add in x axis
axis(2, at = seq(floor(min(yvals)), ceiling(max(yvals)), 10)) # add in y axis
segments(x0=min(xvals), x1 = max(xvals), y0=0, lty = 2) #add in midline
legend(x = max(xvals)-2, y = max(yvals), aov_plotdata$coef_label, bty = "n", # add in legend
pch = 19,col = xvals, ncol = 2)
I'm hoping to keep in the image below the ticks on the vertical z axis, but remove ticks and numbers from the x and y axes. I would like to be able to label my x and y axes with a label for each condition in my matrix, but have not figured out how to do this with text3D. For some reason (because I'm on a mac?) I can't download axes3D, which is one potential solution I've seen in other responses.
Here is my code:
x = c(0,1)
y = c(0,1)
zval = c(104.1861, 108.529, 110.3675, 110.4112)
z = matrix (zval, nrow=2, ncol=2, byrow=TRUE)
hist3D(x,y,z, zlim=c(101,111), colvar = NULL, d=2, col = "lightblue", NAcol = "white", breaks = NULL, colkey = NULL, theta=-60, phi=20, nticks=10, axes=TRUE, ticktype="detailed", space=0.5, lighting=TRUE, light="diffuse", shade=.5, ltheta = 50, bty = "g")
My output

Ultimately, I'd like something more along the lines of this:
I'm very new to R.
stackoverflow.com/questions/26794236/ggplot2-3d-bar-plot
^ this seems like it might be what I need, but I couldn't replicate the code without an error. When I tried to run this piece I got an error because my x and z (in this case) axes aren't numerical:
cloud(y~x+z, d, panel.3d.cloud=panel.3dbars, col.facet='grey', xbase=0.4, ybase=0.4, scales=list(arrows=FALSE, col=1), par.settings = list(axis.line = list(col = "transparent")))
Maybe this might be helpful (with the caveat that 3D plots can sometimes make interpretation more challenging).
First, I recreated a data frame d based on something similar to what you started with:
x = c(0, 0, 1, 1)
y = c(0, 1, 0, 1)
z = c(104.1861, 108.529, 110.3675, 110.4112)
d <- data.frame(
x = factor(as.logical(x)),
y = factor(as.logical(y)),
z = z
)
Note that for x and y I converted the 0 and 1 to FALSE and TRUE with as.logical, then made them factors.
Then for the plot:
library(latticeExtra)
cloud(z ~ x + y, data = d, panel.3d.cloud=panel.3dbars, col.facet='grey',
xbase=0.4, ybase=0.4, scales=list(arrows=FALSE, col=1),
par.settings = list(axis.line = list(col = "transparent")))
You will want the formula as z ~ x + y where z is a numeric response.
Edit: If you wish to customize the axis labels, you can set the factor labels as follows (for example):
d <- data.frame(
x = factor(as.logical(x), labels = c("Hi", "Lo")),
y = factor(as.logical(y), labels = c("Label1", "Label2")),
z = z
)
Plot
I am plotting in xyplot() as per below. I put symbols on the plot with print(panel.points()) and it works. But I need to save the plot with the points to a variable (a in the example) so I can use grid arrange to combine it with other plots in the same picture. Ideas?
dev.off()
x <- c(1:10)
y <- c(1:10)
a <- xyplot(y ~ x, type = "l")
trellis.focus("panel", 1, 1, highlight = FALSE)
print(panel.points(x[c(5,10)],
y[c(5,10)],
pch = 19,
cex = 0.75,
col = c("red", "black")))
Use panel.points within a panel function that calls panel.xyplot to do the main plot:
b = xyplot(
y~x,type="l",
panel=function(...){
panel.xyplot(...)
panel.points(
x[c(5,10)],y[c(5,10)],
cex=0.75, col=c("red","black"),pch=19
)
}
)
I'm using base R plot(), and I want a legend (a color block and key) to show up above (outside) the top right of my plot next to my title (generated using title()).
What's the best way to do this?
Maybe something like this is what you're looking for:
x <- c(1,2,3,4)
y <- c(4,1,3,2)
z <- c(1,2,3,4)
dat <- data.frame(x,y,z)
windows(width = 5, height = 9) #quartz() on Mac
layout(matrix(c(1,2), 2, 1, byrow = TRUE), heights=c(0.5,1))
par(oma = c(4,3,0,0) + 0.1, mar = c(0,0,1,1) + 0.1)
plot(dat$x, y=rep(1,4), type = "n", axes = F, ylab = "", xlab = "")
legend(x = "bottomright", legend = c("y", "z"), fill = c("blue", "red"))
plot(dat$x, dat$y, type = "n", main = "PLOT")
lines(z, col = "red")
lines(y, col = "blue")
Basically this makes two plots, one is just invisible and shortened so all that's displayed is the legend.
You may be able to addtionally tweak the margins around the legend and other graphical parameters (?par) to get the layout better.
I am using xyplot from the lattice package, and I want to change the color of hte header. Currently, it is an ugly light-orange color.
library(lattice)
x <- c(1:10, 1:10)
y <- c(10:1, 10:1)
z <- c(1:10, seq(1,20, by=2))
a = c(rep("one",10),rep("two",10))
DF <- data.frame(x, y, z, a)
xyplot(y ~ x | a, groups = z < 5, data = DF, col = c("black", "red"),
pch=20, cex=0.3)
You need to reset the contents of trellis.par.get()$strip.background$col.
To do this for a single plot, use the par.settings= argument:
xyplot(y ~ x | a, groups = z < 5, data = DF, col = c("black", "red"),
pch = 20, cex = 0.3,
par.settings = list(strip.background=list(col="lightgrey")))
To more persistently reset the strip background color, use trellis.par.set():
trellis.par.set(strip.background=list(col="lightgrey"))
To see how you might have found this out yourself, try the following:
names(trellis.par.get())
trellis.par.get("strip.background")
Finally, for an example of more complicated (and aesthetically appalling) strip-background manipulations, see here.