Space between legend items - r

I'm using R and the lattice package to plot the chart below.
Notice the two items in the legend, that are shown with no spacing between them. If I show the legend above (or below) the chart, it's shown with some space between them, but not if I show in on the right. Is there any way I can separate the two items a bit?
The code used to produce this figure is also reproduced below.
barchart(val1 ~ val2, groups=group, tasks,
auto.key=list(
columns=1,
space="right",
text=c("Data 1","Data 2")
),
main="Title",
xlab="Tasks", ylab="Duration",
par.settings=list(superpose.polygon=list(col=c("firebrick","dodgerblue2")))
)

I think padding.text ist what you are looking for. I dont know, if that works with auto.key.
attach(mtcars)
gear.f<-factor(gear,levels=c(3,4,5),
labels=c("3gears","4gears","5gears"))
cyl.f <-factor(cyl,levels=c(4,6,8),
labels=c("4cyl","6cyl","8cyl"))
densityplot(~mpg|cyl.f,
main="Density Plot by Number of Cylinders",
xlab="Miles per Gallon"
,par.settings = list(superpose.line = list(col=c(1,2,3)))
,key = list(text = list(c("A", "B", "C")),lines = list(col=c("black", "red", "green")),
columns=1,space="top",padding.text=4
)
)

Related

how to fix legend in pie chart rstudio

pie(table(games_list$Genre),
main = "Rating Pie Chart",
col=brewer.pal(length(games_list$Genre),'Spectral'))
legend("topright",
legend=row.names(games_list$Genre),
fill = brewer.pal(length(games_list$Rating), 'Spectral'))
Error in legend("topright", legend = row.names(games_list$Genre), fill = brewer.pal(length(games_list$Rating), :
'legend' is of length 0
Check that row.names(games_list$Genre) returns something and is what the legend parameter expects.
Because you did not provide sample data, I am using the iris dataset in the example below:
pie(table(iris$Species),
main = "Rating Pie Chart",
col=brewer.pal(
length(unique(iris$Species)), # we need only unique values
'Spectral'
)
)
legend("topright",
legend=unique(iris$Species),
fill = brewer.pal(
length(unique(iris$Species)),
'Spectral'
)
)
The code above produces:
As an aside, if you have more than 2 or 3 types/classes you want to show, better use barcharts. Here is a good write up by Stephen Few on this topic "Save the Pies for Dessert"

y axis labeling in R and how to change x-axis to specific increments in R

I would like to create a plot of this data, with x-axis increments of 500000 and with sampleIDs on the y-axis. The following code works to create the plot, but the y-axis labels don't work, and I am unsure how to code the x-axis ticks. Also, I had to add headings manually to the data file (and then obviously add header = TRUE when I assigned d) to get the code to work. I shouldn't have had to put the column titles in though should I since I use setNames?
d = read.delim("n_reads_per_sample.tsv", header = TRUE, sep = "\t")
xticks <- ( ? increments of 500000 to xmax ? )
dotchart(
sort(setNames(d$n_reads, d$X.sample)),
xlim = c(0, at = xticks, 1 max(d$n_reads)),
labels = dimnames(d[[1]])
,
main = "reads per sample",
xlab = "number of reads",
ylab = "sample"
)
In case the link doesn't work, this is what the file looks like.
x.sample n_reads
LT-145 3193621
LT-323 786578
LT-458 485543
LT-500 3689123
LT-95 3308764
LT-367 765972
LT-205 2090226
LT-245 10238727
I can't get at your full data right now, so I am just using your sample in the question.
Not sure what you mean that the y-axis labels don't work. They seem OK to me. You can get the x-axis labels that you want by suppressing the x-axis produced by dotchart and then making your own axis using the axis function. That requires a little fancy footwork with par. Also, unless you stretch out your graphics window, there will not be enough room to print all of the axis labels. I reduced the font size and stretched the window to get the graph below.
UpperLimit <- ceiling(max(d$n_reads)/500000)*500000
xticks <- seq(0,UpperLimit, 500000)
par(xaxt = "n")
dotchart(
sort(setNames(d$n_reads, d$X.sample)),
xlim=c(0, UpperLimit),
labels = dimnames(d[[1]]),
main = "reads per sample",
xlab = "number of reads",
ylab = "sample"
)
par(xaxt = "s")
axis(1, at=xticks, cex.axis=0.7)

Struggling to add legend to box plot in R

I am relatively new user in R - and I seem stuck on what should be fairly easy, I am just not finding the problem in my code set-up. I am trying to create a legend on a simply box plot but I cannot get it to line up correctly, without overlaying itself.
My box plot:
boxplot(OS, main='Computer Users Surveyed', xlab='Program Used', ylab= "Seconds (s)", col=c('blue', 'gold1'))
Then when I add a legend:
legend("topright", c("linux", "windows"), border="black", fill = "blue", "gold1")
All it does is show me a blue square with the words gold1 - instead of double stacking the Linux and windows groups with the corresponding colors.
I think you made a simple mistake by not concatenating the fill colors:
Mock data:
OS <- data.frame(
x = rnorm(100),
y = runif(100)
)
boxplot(OS, main='Computer Users Surveyed', xlab='Program Used', ylab= "Seconds (s)", col=c('blue', 'gold1'), frame = F)
legend("topright", c("linux", "windows"), border="black", fill = c("blue", "gold1"))

How do you make one factor show as symbol, and another factor as colour in nMDS (vegan)?

I am trying to make an nMDS plot of data with a nested factor. I would like the nMDS to show both factors on one plot by using symbols and colour.
In this reproducible example, if use was nested in moisture, I would like the plot to show Moisture as different symbols, and then Use as different colours.
So far I have figured out this:
library("vegan")
library("BiodiversityR")
data(dune, dune.env)
MDS <- metaMDS(dune, distance="bray", strata=dune.env$Moisture)
MDS
plot(MDS$points[,2], MDS$points[,1], type="n", main="Communities by Use",
xlab="NMDS Axis 1", ylab="NMDS Axis 2", xlim=c(-1.5,1.5), ylim=c(-1.5,1.5))
ordisymbol(MDS, dune.env, factor="Use", cex=1.25, rainbow=T, legend=T)
Which gives me the different uses as both different symbols and colours, but shows me nothing about moisture. Is it possible to make it show the different factors instead? I'm assuming it might be somewhere in the MDS$points[,] arguments but I'm not sure what exactly those are doing.
Figured it out by modifying the answer from this question: Plot points of metaMDS
data(dune, dune.env)
dune.MDS <- metaMDS(dune, distance = "bray", strata=dune.env$Moisture)
dune.MDS
pchs<- c(0:5)
gr.moi <- factor(dune.env$Moisture)
gr.use <- factor(dune.env$Use)
col.gr <- c("red", "blue", "purple")
plot(dune.MDS, type = "n", display = "sites")
orditorp(dune.MDS,display="species",col="dark grey",air=0.01)
points(dune.MDS, display = "sites", pch = pchs[gr.moi], col = col.gr[gr.use])
legend("topright", legend=levels(gr.moi), bty = "n", col= c("black"), pch = pchs)
legend("bottomright", legend = levels(gr.use), bty = "n", col = col.gr, pch=c(20),)
And it will produce a lovely plot with symbols and colours exactly how I wanted :)

How to add a non-overlapping legend to associate colors with categories in pairs()?

I am using pairs(iris) to show possible relationships among the four variables (Sepal.length, Sepal.width, Petal.length, Petal.width) in the venerable Iris dataset.
When I add a color parameter...
pairs(iris[, 1:4], col = iris$Species)
...I can see the distinctions among the three species (Iris setosa, Iris virginica, and Iris versicolor), but the code -- as I've written it -- doesn't associate the colors with the species. In other words, there is no legend or anything that functions as a legend.
So someone suggested adding the following below that line of code...
par(xpd = TRUE)
legend( "bottomright", fill = unique(iris$Species),
legend = c( levels(iris$Species) ) )
...and although I get a legend box, the legend box overlays on the data in the pairs() graphic.
Is there a way to create something like a legend box for pairs() that does not overlap with the data presentation itself?
You can control the margin size with the oma argument to pairs. See the oma entry in ?par for details.
pairs(iris[, 1:4], col = iris$Species, oma=c(3,3,3,15))
par(xpd = TRUE)
legend("bottomright", fill = unique(iris$Species), legend = c( levels(iris$Species)))

Resources