ggplot2 violin plot for columns with less than 3 samples - r

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.

Related

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/

Lag 1 Scatter Plot in R

I have an assignment. There are two datasets and in one of the questions, I needed to find the lag 1 scatter plot of each time series.
I created the graphs, but my question is what does it tell us? I know we are looking for some sort of autocorrelation, yet I couldn't properly understand it. What can we infer from this scatter plot and how can we make any inference?
To give you a better understanding I added one of the dataset and its graph, as well.
Edit
Data and graph code.
x <- c(39,35,16,18,7,22,13,18,20,9,-12,-11,-19,-9,-2,16)
plot(dplyr::lag(x), x)
The graph can also be plotted with
tsDyn::autopairs(x, type = "points")

R: ggplot2 plotting 1 combined boxplot instead of 2 plots for 2 categories

This is probably a simple problem with a simple fix, but I cannot seem to get it right for some reason. I am trying to plot a continuous variable "cnt" against a categorical variable "yr" with 2 categories (1 or 2). The data is roughly split in half into year 1 and year 2.
When I try the following code to compare the boxplots over 2 years, I get the following graph however:
ggplot(data=rawd,aes(x=yr,y=cnt, color=yr))+
geom_boxplot()+
labs(x='Year', y= 'Hourly Usage Count')
I am not sure why that is the case. When I try to visualize it in any other format (such as using jitter or geom_point) there is a clear split between yr=1 and yr= 2 (see below)
I am also facing an issue when using the simple boxplot command on R, where I get the following curve:
boxplot(rawd$yr, rawd$cnt)
If anyone can see what I am doing wrong, please let me know.
As mentioned in the comment changing the 'yr' column in question to type 'factor' seems to fix it

how to plot in R

I am struggling with the following R code for plotting;
set.seed(1234)
x1=rnorm(100)^2
Index=1:length(x1)
cutoff=3
plot(Index,x1,type="h")
abline(h=cutoff,lty=2)
Firstly, I want to plot also the index of those values that are greater than cutoff value?
And secondly, I have five plots similar to above, I am using
par(mfrow= c(3,2))
It give 3 x 2 space, but for fifth plot, I need to plot it in the center;
The layout() function will be your best friend in terms of arranging the plots.
Check http://www.statmethods.net/advgraphs/layout.html for a good tutorial on the function.

In R and ggplot2 package, How to Add Lines?

I am doing a survival analysis and have produced the survival graph using
plot function to plot the Kaplan-Meier(KA variable) estimate as y value against time.
lines function to plot the step lines between estimate i and i+1, for each i=1,2,....
The code is as follows:
plot(KA)
for( i in 1:(length(KA)-1)){
lines(c(i,i+1),c(KA[i],KA[i])) # The horizontal step lines
lines(c(i+1,i+1),c(KA[i],KA[i+1])) # The vertical step lines
}
Now I want to make a more beautiful survival graph using ggplot2 package.
The question is: how to add the step lines into the graph?
I'm sorry, I can not put graphs as my reputation is less than 10.
Have a look at either geom_step, geom_path or geom_segment.
They might be helpful in what you are trying to achieve.
http://had.co.nz/ggplot2/geom_step.html
http://had.co.nz/ggplot2/geom_path.html
http://had.co.nz/ggplot2/geom_segment.html

Resources