Looking at this code:
pairs(Iris[1:3], main = "Anderson's Iris Data -- 3 species",
pch = c(21), cex = 2,bg = c("red","green3","blue")[unclass(iris$Species)])
is it possible to show the groups/classes Species as legend color coded?
pairs(iris[1:3], main = "Anderson's Iris Data -- 3 species",
pch = c(21), cex = 2, bg = c("red","green3","blue")[unclass(iris$Species)], oma=c(4,4,6,10))
par(xpd=TRUE)
legend(0.55, 1, as.vector(unique(iris$Species)), fill=c("red", "green3", "blue"))
From ?pairs:
Graphical parameters can be given as arguments to plot such as main. par("oma") will be set appropriately unless specified. Hence any attempts to specify par before pairs will result in override.
Additionally it is very complicated to control the legend position in pairs.
I recommend using library(GGally)
library(GGally)
ggpairs(iris, aes(color = Species), columns = 1:4)
Related
having issues getting three ellipses colors to show up. I am seeing all three shapes, but missing a color.
Also, I have two labels that I hope to delete. They could remain as points, but they aren't needed for the visual.
Here's my code-
#ordinate, nmds with bray-curtis distances
camord <- metaMDS(freq, distance = "bray", autotransform = FALSE)
#plotting ordination
ordiplot(camord, display='species')
ordilabel(camord, display="species",font=3, col = "black", cex = 0.5)
with(env, ordiellipse(camord, Habitat, col = as.numeric(envhab), lwd = 1.75))
legend("topright", legend = c(levels(envhab)), fill= as.numeric(factor(levels(envhab))))
I've been struggling with this for a day or two, tried a few things...
tried rearranging my CSV and using points...
points(camord, display = "species", label = colnames(camord)[1:11], cex = 0.5)
to just label the first 11 species.
Tried using text function... no labels again.
ordiplot(camord, display='species')
points(camord, display = "species", cex = 0.5)
with(env, ordiellipse(camord, Habitat, col = as.numeric(envhab), lwd = 2))
legend("topright", legend = c(levels(envhab)), fill= as.numeric(factor(levels(envhab))))
text(camord$species[1:11,1], camord$species[1:11,2], labels = colnames(camord)[1:11], cex = 0.5)
Many apologies as I am just beginning to learn. Any help or guidance would be appreciated.
Just a minor question. I am trying to make a legend for the following plot.
# fitting the linear model
iris_lm = lm(Petal.Length ~ Sepal.Length, data = iris)
summary(iris_lm)
# calculating the confidence interval for the fitted line
preds = predict(iris_lm, newdata = data.frame(Sepal.Length = seq(4,8,0.1)),
interval = "confidence")
# making the initial plot
par(family = "serif")
plot(Petal.Length ~ Sepal.Length, data = iris, col = "darkgrey",
family = "serif", las = 1, xlab = "Sepal Length", ylab = "Pedal Length")
# shading in the confidence interval
polygon(
c(seq(8,4,-0.1), seq(4,8,0.1)), # all of the necessary x values
c(rev(preds[,3]), preds[,2]), # all of the necessary y values
col = rgb(0.2745098, 0.5098039, 0.7058824, 0.4), # the color of the interval
border = NA # turning off the border
)
# adding the regression line
abline(iris_lm, col = "SteelBlue")
# adding a legend
legend("bottomright", legend = c("Fitted Values", "Confidence Interval"),
lty = c(1,0))
Here's the output so far:
My goal is to put a box in the legend next to the "Confidence Interval" tab, and color it in the same shade that it is in the picture. Naturally, I thought to use the pch parameter. However, when I re-run my code with the additional legend option pch = c(NA, 25), I get the following:
It is not super noticeable, but if you look closely at the padding on the left margin of the legend, it actually has decreased, and the edge of the border is now closer to the line than I would like. Is there any way to work around this?
That's a curious behavior in legend(). I'm sure someone will suggest a ggplot2 alternative. However, legend() does offer a solution. This solution calls the function without plotting anything to capture the dimensions of the desired rectangle. The legend is then plotted with the elements you really want but no enclosing box (bty = "n"). The desired rectangle is added explicitly. I assume you mean pch = 22 to get the filled box symbol. I added pt.cex = 2 to make it a bit larger.
# Capture the confidence interval color, reusable variables
myCol <- rgb(0.2745098, 0.5098039, 0.7058824, 0.4)
legText <- c("Fitted Values", "Confidence Interval")
# Picking it up from 'adding a legend'
ans <- legend("bottomright", lty = c(1,0), legend = legText, plot = F)
r <- ans$rect
legend("bottomright", lty = c(1,0), legend = legText, pch = c(NA,22),
pt.bg = myCol, col = c(1, 0), pt.cex = 2, bty = "n")
# Draw the desired box
rect(r$left, r$top - r$h, r$left + r$w, r$top)
By the way, I don't think this will work without further tweaking if you place the legend on the left side.
I am using R for plotting. When my graph plots the legend appears where I want it to be but the colors are missing. mtcars 2 is a modified version of mtcars (one of the pre-loaded data sets) that adds a model and country of origin to the data set. mtcars.pca is what I named my redundance analysis (rda function under vegan), and mtcars.clust is titled for hierarchical clustering of the continuous factors of mtcars (hclust function of vegan) Below is the code I am using with mtcars2.
pca.fig = ordiplot(mtcars.pca, type = "none", las=1, xlim=c(-15,15), ylim = c(-20,10))
points(pca.fig, "sites", pch = 19, col = "green", select = mtcars2$origin =="domestic")
points(pca.fig, "sites", pch = 19, col = "blue", select = mtcars2$origin =="foreign")
ordiellipse(mtcars.pca, mtcars2$origin, conf = 0.95, label = FALSE)
ordicluster(mtcars.pca, mtcars.clust, col = "gray")
legend("bottomright", title="Car Origin", c("domestic", "foreign"), col = "origin")
You need to specify a vector of colours in legend and also a pch:
library("vegan")
data(dune, dune.env)
ord <- rda(dune)
plot(ord, type = "n")
cols <- c("red","blue","green")
points(ord, col = cols[dune.env$Use], pch = 19)
legend("bottomright", legend = levels(dune.env$Use), bty = "n",
col = cols, pch = 19)
If you don't add pch but just use col = cols legend() doesn't display any points. Because you used pch = 19 in your points() calls, use the same in the legend() call.
Also, note how to plot points of different colours in a single pass. I have some examples and explanation that go through the indexing trick I used in my code above to achieve this in a blog post of mine from a few years ago: http://www.fromthebottomoftheheap.net/2012/04/11/customising-vegans-ordination-plots/
I came to this question having the next problem in xts object:
I wanted to plot all time-series in xts object with legend. Moreover, there were around 20.
I used (wrong):
plot(returns_xts)
addLegend(...)
Correct version:
plot(returns_xts, legend.loc = "bottomright", col=1:20, lty = 1)
There is legend.loc parameter
col = 1:20 generates colors for you
Result:
I can create a dendrogram using
x<-1:100
dim(x)<-c(10,10)
set.seed(1)
groups<-c("red","red", "red", "red", "blue", "blue", "blue","blue", "red", "blue")
x.clust<-as.dendrogram(hclust(dist(x)))
x.clust.dend <- x.clust
labels_colors(x.clust.dend) <- groups
x.clust.dend <- assign_values_to_leaves_edgePar(x.clust.dend, value = groups, edgePar = "col") # add the colors.
x.clust.dend <- assign_values_to_leaves_edgePar(x.clust.dend, value = 3, edgePar = "lwd") # make the lines thick
plot(x.clust.dend)
However I want to delete the scale of height information in the left as shown in Figure below.
My guess is that it should be extremely trivial but I am not able to find a way to do this. One solution which I don't want is using the ggplot2 as below:
ggplot(as.ggdend(dend2))
This is because I will loose some of the formatting like color_bars()
The graphical parameter 'axes = FALSE" can be used to remove the distance measure for the plot.dendogram command:
plot(x.clust.dend, axes=F)
This will produce the following dendogram without distance axis:
You can just set yaxt = "n"
plot(x.clust.dend, yaxt = "n")
You can add another axis with
axis(side = 2, labels = FALSE)
I have done a PCA in R and have graphed the results with a scatter plot.
I have 7 data variables and the last set of points come out as stock yellow. How can I make it so each set of points is a specific colour?
This is the code I have been using:
plot(clam.pca$x[,1], clam.pca$x[,2], pch = clam$Form.code,
col = clam$Form.code, cex = .8, lty = "solid", lwd = 1,
xlab = "Axis1", ylab = "Axis2", xlim = c(-0.2, 0.30))
Rory
You can't set the colour to a variable using the generic plotting functions. You have to manually set what colour you want as a vector. So for example:
x<-seq(1:10)
y<-seq(1:10)
cols<-ifelse(group=="a","red","green")
plot(x,y,col=cols)