Grouped bar chart not working with lattice in R - r

I'm having trouble creating grouped barplots. Have explored base graphics and lattice.
My data looks like
compound detection LUtype
a 50 ag
a 75 urban
a 34 mixed
b 89 ag
......
I'd like to create a plot with compounds on the y axis (horizontal bar plot) with the bars colored to represent the land use type and detection on the x axis.
These data are stored in a data frame, which I tried converting to a matrix with as.matrix, but this doesn't work and from what I can tell, the matrix is only the row of compounds. This does not produce a plot.
bars<-data.frame(data6$compound,data6$detection,data6$LUtype)
barsM<-as.matrix(data6$compound,data6$detection,data6$LUtype)
barplot(barsM,horiz=TRUE,beside=TRUE)
I also tried to bypass the matrix by using lattice, by no plot here either.
library(lattice)
require(lattice)
barchart(data6$detection~data6$compound,groups=data6$LUtype,bars)
I'm reading this article
plotting grouped bar charts in R, and I have basically the same set up, but these solutions aren't working for me.

Related

ggplot2 violin plot for columns with less than 3 samples

I am wondering if anyone has found a way to display violin plots through ggplot2 with variables of 1 or 2 samples.
example code:
library(ggplot2)
testData <- data.frame(x=c("a","a","a","b","b"), y=c(1,2,2,1,2))
ggplot(data=testData ) + geom_violin(aes(x=x,y=y))
As you can see the violin plot for a has been drawn as it has 3 samples, the one for b no -> only 2 samples.
I saw geom_violin produces error when all values in a series are the same but no answer has been given, and it's been 7
years.
I know it is possible to display a violin plot with the violplot package, but I'd really prefer to keep to the ggplot package if possible.
Thanks,
HY
Thanks to #MarcoSandri and others.
I was on ggplot2 3.3.3, it now works on 3.3.6.

How to label 5 specific points on PCA plot

I have used package (tidyverse) and just wanted to add labels to 5 specific points on this lot. I have tried the below code but it is not giving me any points. the data set is about 2000 observations over 21 variables.
BOTTOM=which(interest2$ID%in%project.pca$ID);
text(which(interest2$ID%in%project.pca$ID)[BOTTOM,1], text(which(interest2$ID%in%project.pca$ID))[BOTTOM,2],text(which(interest2$ID%in%project.pca$ID)[BOTTOM,3],rownames(input)[BOTTOM],pos=1)
With ggplot, Make the PCA plot first, and the add edition layer with dataframe with only those 5 points. Check out in this post for example
https://datavizpyr.com/how-to-add-labels-to-select-points-with-ggrepel/

How to count the number of points below a contour line in R

I've created a heatmap in R based on simulations and plotted it using image.plot() and I have added contour lines by using contour(). I also have a data frame that contains a column for observations in first year and trend size that I have plotted on top of the heatmap using base R plot. Is there an easy way to count the number of points below the 0.5 contour line and about the 0.95 contour line?
Assuming you are plotting a variable called z, you can use something like.
table(z>.95)

Plot group in lattice, using different data sources

Using the lattice package in R, I would like to plot one row of 7 diagrams, all using the same Y-axis. The diagrams should be (vertical) line diagrams. The problem is that my data are each in 7 separate dataframes (containing X and Y data), with different slightly different limits on the Y-axis data.
Besides all tutorials, I don't get it right. What must my Code look like? Is there even a clean solution for this in lattice?
You could combine all your data frames into one and then do something like
xyplot(Y~X|odf,data=combinedDF,layout=c(7,1))
where odf is an indicator column of the original data frame. This by default should use a common y scale.
Apart from combining the data, you could create 7 separate plots, then print them.
p1 <- xyplot(Y~X,data=DF1,ylim=c(Y1,Y2))
p2 <- xyplot(Y~X,data=DF2,ylim=c(Y1,Y2))
etc.
To print:
print(p1,split=c(1,1,7,1),more=TRUE)
print(p2,split=c(2,1,7,1),more=TRUE)
...
print(p7,split=c(7,1,7,1),more=FALSE)
see ?print.trellis.
Of course, arranging single plots like this doesn't really use the features of lattice. You could just as easily do this with base graphics using layout or par(mfrow=c(1,7)) for example, and a common ylim.

How to plot a butterfly plot or symmetric barchart in R

I need to create a bargraph with middle x-axis and two positive y axis above and below.
It should look like a butterfly plot in SAS, but transposed x and y axis.
My data is lengths of male and female fish.
Sample data:
length <- c(12,13,15,14,13,16,18)
sex<-c("m","m","m","f","f","f","f")
dat=data.frame(length,sex)
Another term is 'opposed horizontal barchart'. (There are multiple authors to package: plotrix but Jim Lemon stands out as the most productive and is both the maintainer of the package and the author of pyramid.plot.) This is a modified version of an example in ?pyramid.plot:
install.packages("plotrix")
xy.pop<-c(3.2,3.5,3.6,3.6,3.5,3.5,3.9,3.7,3.9,3.5,3.2,2.8,2.2,1.8,
1.5,1.3,0.7,0.4)
xx.pop<-c(3.2,3.4,3.5,3.5,3.5,3.7,4,3.8,3.9,3.6,3.2,2.5,2,1.7,1.5,
1.3,1,0.8)
agelabels<-c("0-4","5-9","10-14","15-19","20-24","25-29","30-34",
"35-39","40-44","45-49","50-54","55-59","60-64","65-69","70-74",
"75-79","80-44","85+")
mcol<-plotrix::color.gradient(c(0,0,0.5,1),c(0,0,0.5,1),c(1,1,0.5,1),18)
fcol<-plotrix::color.gradient(c(1,1,0.5,1),c(0.5,0.5,0.5,1),c(0.5,0.5,0.5,1),18)
# removed labels in center but you could run the example and see another approach
par(mar=plotrix::pyramid.plot(xy.pop,xx.pop, labels=rep("",18),
main="Australian population pyramid 2002",lxcol=mcol,rxcol=fcol,
gap=0,show.values=TRUE))

Resources