Y axis values missing in R scatter plot and graphs - r

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.

Related

Problems with 'legend' in latticeExtra

i'm using the package 'latticeExtra' to plot with double Y axis as i show in my code:
library(latticeExtra)
MS <- xyplot(Tabla.Rosetta.dt[,14] ~ Tabla.Rosetta.dt[,1], Tabla.Rosetta.dt, type='l', col="#9933CC", ylab = 'Meteoscore',
key=list(space="topright",
lines=list(col=c("#9933CC","#FF9933"), lty=c(1,1), lwd=6),
text=list(c("Meteoscore"," RSP"))
))
RSP <- xyplot(Tabla.Rosetta.dt[,2] ~ Tabla.Rosetta.dt[,1], Tabla.Rosetta.dt, type='l', col="#FF9933", ylab = 'RSP frequency', xlab = 'Dates')
doubleYScale(MS, RSP, add.ylab2 = TRUE, use.style=FALSE)
but the input 'key' doesn't work. I would like add legend in topright but i don't know how do ir.
ideas?
Thanks for your time
Please read the info at the top of the r tag and note that all questions should be reproducible. Tabla.Rosetta.dt is missing so in the absence of that we will use the SeatacWeather data frame from latticeExtra and adjust the example to that adding panel.key in a new layer specifying packets=3 to the layer.
library(latticeExtra)
data(SeatacWeather)
temp <- xyplot(max.temp + min.temp ~ day | month,
data = SeatacWeather, type = "l", layout = c(3, 1))
rain <- xyplot(precip ~ day | month, data = SeatacWeather, type = "h")
doubleYScale(temp, rain, style1 = 0, style2 = 3, add.ylab2 = TRUE) +
layer(panel.key(text = c("max temp", "min temp", "rain"),
lines = TRUE, points = FALSE),
packets = 3)

Manually set fontsize of axis titles in native-R plot and lattice graphing functions

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.

plotFit - data plotted as bars instead of points?

I am using the plotFit function in the investr package in R to display my data as follows:
Figure 1
The code I am using to generate this is simply:
plotFit(nls model, interval = "confidence", level = 0.95, pch = 19, shade = TRUE,
col.conf = "seagreen2", col.fit = "green", lwd.fit = 2,
ylim = c(y1,y2), xlim = c(x1,x2),
xaxp = c(0,200,10), n = 100,
ylab = "", xlab = "",
main = "")
Is there a simple way that I could adapt the code to plot the data as bars, rather than points?
Yes, use type = "h". For example,
fit <- lm(dist ~ speed, data = cars)
library(investr)
plotFit(fit)
plotFit(fit, type = "h", lwd = 3)

R: How to add normal distributions to overlapping grouped histograms with lattice

I've been searching for ways to make overlapping grouped histograms with the function 'histogram' in lattice, which I've found an answer to here.
histogram( ~Sepal.Length,
data = iris,
type = "p",
breaks = seq(4,8,by=0.2),
ylim = c(0,30),
groups = Species,
panel = function(...)panel.superpose(...,panel.groups=panel.histogram,
col=c("cyan","magenta","yellow"),alpha=0.4),
auto.key=list(columns=3,rectangles=FALSE,
col=c("cyan","magenta","yellow3"))
)
Now my question is if you could still add normal distributions for every group to this plot.
Possibly using this?
panel.mathdensity(dmath = dnorm, col = "black",
args = list(mean=mean(x),sd=sd(x)))
end result should end up looking similar to this:
image
This is the closest I was able to get. The hint I used was here. My problem is that the density plot gets hidden behind the next histogram plot.
plot1 <- histogram( ~Sepal.Length,
data = iris,
type = "p",
ylim = c(0,30),
breaks = seq(4,8,by=0.2),
groups = Species,
col=c("cyan","magenta","yellow"),
panel = panel.superpose,
panel.groups = function(x,y, group.number,...){
specie <- levels(iris$Species)[group.number]
if(specie %in% "setosa"){
panel.histogram(x,...)
panel.mathdensity(dmath=dnorm,args = list(mean=mean(x), sd=sd(x)), col="black")
}
if(specie %in% "versicolor"){
panel.histogram(x,...)
panel.mathdensity(dmath=dnorm,args = list(mean=mean(x), sd=sd(x)), col="black")
}
if(specie %in% "virginica"){
panel.histogram(x,...)
panel.mathdensity(dmath=dnorm,args = list(mean=mean(x), sd=sd(x)), col="black")
}
}
)

Bold boxplot labels in R

Does anybody know how to embolden the x and y axis labels ("Single sample" and "Value axis") in an R boxplot?
This is what my boxplot looks like:
Using this example data:
dat <- data.frame(values = c(rnorm(100, mean = 1), rnorm(100, mean = 3),
rnorm(100, mean = 4, sd = 3)),
groups = factor(rep(c("aaa","bbb","ccc"), each = 100)))
There are several ways. One is to use ?plotmath functionality and the plotmath "function" bold() in an expression:
boxplot(values ~ groups, data = dat,
ylab = expression(bold(Value~axis)),
xlab = expression(bold(Single~sample)))
or, similarly
boxplot(values ~ groups, data = dat,
ylab = expression(bold("Value axis")),
xlab = expression(bold("Single sample")))
Another way is to leave the titles off the plot and then add them with the title() function using the bold font:
boxplot(values ~ groups, data = dat)
title(ylab = "Value axis", xlab = "Single sample", font.lab = 2)
We need graphical parameter font.lab as this is the parameter that controls the axis labels. Read the entries in ?par for more info.

Resources