I have been unable to get any graphs to show y-axis values in RStudio. My understanding is that the y-axis should set it self automatically. I've also attached an example of the data below. Do I need to be specifying the y-axis, or am I formatting my data incorrectly in some way?
homes = read.csv("~/Downloads/homePrice1.csv")
model = lm(homes$Price ~ homes$Size,
data = homes)
plot(homes$Price ~ homes$Size,
data = homes,
xlab = "Size",
ylab = "Price")
coeff=coefficients(model)
rSquare = summary(model)$r.squared
eq = paste0("y = ", round(coeff[2],5), "*x + ", round(coeff[1],2), "\nR^2 = ", round(rSquare*100,2), "%")
print(eq)
abline(model, lwd = 3, col = "darkorange")
text(x = 1500, y = 300000, label = eq)
I used dev.off() and that fixed the problem. I found the answer here.
I am trying to prepare a graph for a poster presentation, but am getting very frustrated by how difficult things that should be simple are in plot. I want to plot a qq-plot of residuals from a mixed-effects model. All I want to do is change the font size of the axis title
. Here's a reproducible example.
library(lme4)
library(lattice)
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
qqmath(fm1,
main = list("QQ-Plot", cex = 2),
id=0.05,
cex = list(x = 2),
scales = list(x = list(cex = 2), y = list(cex = 2)))
This all works fine. But when I try to increase the font size of the axis title
qqmath(fm1,
main = list("QQ-Plot", cex = 2),
xlab = list("x-axis", cex = 2),
id=0.05,
cex = list(x = 2),
scales = list(x = list(cex = 2), y = list(cex = 2)))
I get
Error in qqmath.formula(x = ~x, ylab = "Standardized residuals", xlab = "Standard normal quantiles", :
formal argument "xlab" matched by multiple actual arguments
I gather from this post that this is due to competing arguments in the function call and some ellipsis in the original qqmath.formula object, but surely there has to be an easier way to set the fontsize of the axis titles than reprogramming the original function?!
The lattice system has functions trellis.par.get and trellis.par.set and this can be used to control the fontsize of the xlab and ylab components:
?trellis.par.get
names( trellis.par.get() )
trellis.par.set(par.ylab.text=list(cex=.5))
qqmath(fm1,
main = list("QQ-Plot", cex = 2), id=0.05,
cex=list(left=.55,bottom=.5),
scales = list(x = list(cex = 1), y = list(cex = 1)))
... reduces the size of the ylab. You can find a more complete list of the components and features that can be set from a chart onpage 127 in the "Lattice" book by Sarkar.
Can anyone tell me how to set attributes within qqmath from the lattice package. Specifically how do I increase the font size of axis titles, axis text etc? None of the native plot arguments seem to work.
library(lattice)
lattice::qqmath(~ rnorm(100), distribution = function(p) qt(p, df = 10),
id=0.05,
main = "this is a title",
ylab = "this is a y-axis",
xlab = "this is an x-axis",
cex = 2,
cex.axis = 4,
font = 2,
cex.lab = 4)
Found a solution in the labyrinth that is the help page for lattice. In order to specify attributes of axis labels in lattice graphs you need to use the x and y argument within the scales argument, all wrapped in list(). To change the font size of titles you also need to wrap everything in a list():
lattice::qqmath(~ rnorm(100), distribution = function(p) qt(p, df = 10),
id=0.05,
main = list("this is a title", cex = 2),
ylab = list("this is a y-axis", cex = 3),
xlab = list("this is an x-axis", cex = 4),
scales = list(x = list(cex = 4)))
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")
I would like to overlay 2 density plots on the same device with R. How can I do that? I searched the web but I didn't find any obvious solution.
My idea would be to read data from a text file (columns) and then use
plot(density(MyData$Column1))
plot(density(MyData$Column2), add=T)
Or something in this spirit.
use lines for the second one:
plot(density(MyData$Column1))
lines(density(MyData$Column2))
make sure the limits of the first plot are suitable, though.
ggplot2 is another graphics package that handles things like the range issue Gavin mentions in a pretty slick way. It also handles auto generating appropriate legends and just generally has a more polished feel in my opinion out of the box with less manual manipulation.
library(ggplot2)
#Sample data
dat <- data.frame(dens = c(rnorm(100), rnorm(100, 10, 5))
, lines = rep(c("a", "b"), each = 100))
#Plot.
ggplot(dat, aes(x = dens, fill = lines)) + geom_density(alpha = 0.5)
Adding base graphics version that takes care of y-axis limits, add colors and works for any number of columns:
If we have a data set:
myData <- data.frame(std.nromal=rnorm(1000, m=0, sd=1),
wide.normal=rnorm(1000, m=0, sd=2),
exponent=rexp(1000, rate=1),
uniform=runif(1000, min=-3, max=3)
)
Then to plot the densities:
dens <- apply(myData, 2, density)
plot(NA, xlim=range(sapply(dens, "[", "x")), ylim=range(sapply(dens, "[", "y")))
mapply(lines, dens, col=1:length(dens))
legend("topright", legend=names(dens), fill=1:length(dens))
Which gives:
Just to provide a complete set, here's a version of Chase's answer using lattice:
dat <- data.frame(dens = c(rnorm(100), rnorm(100, 10, 5))
, lines = rep(c("a", "b"), each = 100))
densityplot(~dens,data=dat,groups = lines,
plot.points = FALSE, ref = TRUE,
auto.key = list(space = "right"))
which produces a plot like this:
That's how I do it in base (it's actually mentionned in the first answer comments but I'll show the full code here, including legend as I can not comment yet...)
First you need to get the info on the max values for the y axis from the density plots. So you need to actually compute the densities separately first
dta_A <- density(VarA, na.rm = TRUE)
dta_B <- density(VarB, na.rm = TRUE)
Then plot them according to the first answer and define min and max values for the y axis that you just got. (I set the min value to 0)
plot(dta_A, col = "blue", main = "2 densities on one plot"),
ylim = c(0, max(dta_A$y,dta_B$y)))
lines(dta_B, col = "red")
Then add a legend to the top right corner
legend("topright", c("VarA","VarB"), lty = c(1,1), col = c("blue","red"))
I took the above lattice example and made a nifty function. There is probably a better way to do this with reshape via melt/cast. (Comment or edit if you see an improvement.)
multi.density.plot=function(data,main=paste(names(data),collapse = ' vs '),...){
##combines multiple density plots together when given a list
df=data.frame();
for(n in names(data)){
idf=data.frame(x=data[[n]],label=rep(n,length(data[[n]])))
df=rbind(df,idf)
}
densityplot(~x,data=df,groups = label,plot.points = F, ref = T, auto.key = list(space = "right"),main=main,...)
}
Example usage:
multi.density.plot(list(BN1=bn1$V1,BN2=bn2$V1),main='BN1 vs BN2')
multi.density.plot(list(BN1=bn1$V1,BN2=bn2$V1))
You can use the ggjoy package. Let's say that we have three different beta distributions such as:
set.seed(5)
b1<-data.frame(Variant= "Variant 1", Values = rbeta(1000, 101, 1001))
b2<-data.frame(Variant= "Variant 2", Values = rbeta(1000, 111, 1011))
b3<-data.frame(Variant= "Variant 3", Values = rbeta(1000, 11, 101))
df<-rbind(b1,b2,b3)
You can get the three different distributions as follows:
library(tidyverse)
library(ggjoy)
ggplot(df, aes(x=Values, y=Variant))+
geom_joy(scale = 2, alpha=0.5) +
scale_y_discrete(expand=c(0.01, 0)) +
scale_x_continuous(expand=c(0.01, 0)) +
theme_joy()
Whenever there are issues of mismatched axis limits, the right tool in base graphics is to use matplot. The key is to leverage the from and to arguments to density.default. It's a bit hackish, but fairly straightforward to roll yourself:
set.seed(102349)
x1 = rnorm(1000, mean = 5, sd = 3)
x2 = rnorm(5000, mean = 2, sd = 8)
xrng = range(x1, x2)
#force the x values at which density is
# evaluated to be the same between 'density'
# calls by specifying 'from' and 'to'
# (and possibly 'n', if you'd like)
kde1 = density(x1, from = xrng[1L], to = xrng[2L])
kde2 = density(x2, from = xrng[1L], to = xrng[2L])
matplot(kde1$x, cbind(kde1$y, kde2$y))
Add bells and whistles as desired (matplot accepts all the standard plot/par arguments, e.g. lty, type, col, lwd, ...).