i am trying to add wilcoxon stats in my graph, but the "stat_compare_means" does not work...
i have tried both ggplot and ggplot2.
library(readxl)
library(dplyr)
library(tidyverse)
library(ggpubr)
library(dplyr)
library(tidyr)
library(ggplot2)
library(Rtsne)
require(ggpubr)
#excel sheet resolution, voxel size comparison
data<-read_excel("res_all.xlsx", sheet="resolution")
# transform to long format using dplyr (included in tidyverse)
data_long <- as_tibble(data) %>%
gather(key, value,-parameter) %>%
mutate(cohort=ifelse(grepl("per",key), "per", "val"))
# plot graph
graph <- ggplot(data_long) +
aes(x=parameter, y=value, fill=cohort)+
geom_boxplot()+
stat_compare_means(method= "wilcox.test")
graph + ggtitle("Resolution comparison")+
theme_minimal()
error is Error in stat_compare_means(method = "wilcox.test") :
could not find function "stat_compare_means"
is it any other way to add W and p-values in my graph?
Thank you in advance.
[1]: https://i.stack.imgur.com/yfp8E.png
I think you forgot a "+" after theme_minimal().
Oh, and stat_compare_means is from ggpubr package, not ggplot. be sure you included it. Check if you have library(ggpubr) or require(ggpubr) in your R session. It is good if you can include full code and result in sessioninfo() for further troubleshoot.
The stat_compare_means() was introduced in ggpubr ver 0,1,3. So check the package with ?ggpubr for the version and lsf.str("package:ggpubr") to list all functions inside the package.
Related
I have used the following code
ggplot(IncomeGroup_count,
aes(x=Income_Group,
y= Percentage,
colour = 'lightblue'))
But the only thing it produces is the x and y axis with no bars.
It looks like you are missing the geom_bar function. Typically ggplot works as the base function for fitting whatever data you are using and then the geom functions "draw" the plot using that data. Here I have constructed a bar plot using data in R since you have not supplied your own data in your question.
#### Load Library ####
library(tidyverse)
#### Plot ####
airquality %>%
ggplot(aes(x=is.na(Ozone)))+
geom_bar()
Which gives you this bare bones bar plot. It takes the NA values from the airquality dataset, then draws bars thereafter plotting the NA values by "TRUE" for NA and "FALSE" for not NA:
Edit
Again it's difficult to guess what is going wrong if you don't share your data, so I'm assuming based off your comments below that you are trying to plot y explicitly as a count. As suggested in the comments, geom_col may be better for this. Using a different example from the same dataset:
airquality %>%
group_by(Month) %>%
summarise(Mean_Ozone = mean(Ozone, na.rm=T)) %>%
ggplot(aes(x=Month,
y=Mean_Ozone))+
geom_col()
You get this:
I am trying to come up with a way to plot the following graph in R. I have looked up a lot but did not find a solution.
This is an example of how data are:
df<-data.frame(matrix(NA,nrow=20,ncol=3)) colnames(df)<-c("time","A","B") df$time<-1:20 df$A<-c(rep("Success",3),rep("Fail",5),rep("Success",9),rep("Fail",3)) df$B<-c(rep("Success",7),rep("Fail",2),rep("Success",4),rep("Fail",7))
Any help to create the plot in R would be greatly appreciated.
This approach can be useful:
library(dplyr)
library(ggplot2)
#Code
df %>% pivot_longer(-time) %>%
ggplot(aes(x=time,y=name,fill=value))+
geom_tile()
Output:
I'm trying to figure out why my pca axes lines are not showing up in my final pca plot pca plot with no axes lines but nothing is standing out. I've tried going through and commenting out parts of the code but that hasn't helped. Any help is appreciated!
Data: https://pastebin.com/MWNfUz3S
#not all of these are used for the pca portion of my code but all are used at some point
library(readxl)
library(dplyr)
library(broom)
library(tidyr)
library(ggthemes)
library(ggplot2)
library(gridExtra)
library(cowplot)
library(gtable)
library(ggfortify)
library(data.table)
library(vegan)
library(ggrepel)
library(forcats)
library(scales)
input <-"End Members 2020_03.xls"
mydata<-read_excel(input,sheet=1)
rm(input)
df<-mydata[,c(5:24)]
pca<-rda(df,scale=T) #Conducting a correlation PCA
#Percent variance explained by PCs
pervar<-round(pca$CA$eig/sum(pca$CA$eig)*100,2)
#Plot values and limits for all LOP plots
scrs<-scores(pca,display=c("sites","species"))
xlim<-with(scrs,range(species[,1],sites[,1]))*1.1
ylim<-with(scrs,range(species[,2],sites[,2]))*1.1
ordlab<-ordipointlabel(pca,display="species")$labels
factor.scores<-as.data.frame(pca$CA$v) %>% select(PC1,PC2) %>% mutate(variable=row.names(.),PC1=PC1)
factor.scores.alt<-as.data.frame(scrs$species)
site.scores<-as.data.frame((pca$CA$u)) %>% select(PC1,PC2)
pca.scores<-as.data.frame(scrs$sites)
mydata$PC1<-pca.scores$PC1
mydata$PC2<-pca.scores$PC2
#Generate better labels in PCA plot
variables<-factor.scores.alt %>% mutate(PC1.adj=PC1*1.7,PC2.adj=PC2*1.7,labels=rownames(factor.scores.alt))
landuse.color.and.season.shape.pca<-ggplot(mydata,aes(PC1,PC2))+
geom_vline(xintercept=0.0)+
geom_hline(yintercept=0.0)+
#geom_point(aes(shape=Season,color=reorder(Landuse,Classification)),size=2)+
geom_point(aes(shape=Season,color=Landuse),size=3)+
geom_text_repel(data=variables,aes(PC1.adj,PC2.adj,label=labels),size=3,fontface="plain",family="serif")+
scale_color_gdocs(name="Landuse")+
#scale_shape_discrete(solid=T,name="Seasons")+
xlim(-3,3)+
ylim(3,-3)+
xlab("PC1 [56.4%]")+
ylab("PC2 [20.2%]")+
theme(legend.text=element_text(size=10,face="plain",family="serif"),
legend.title=element_text(size=10,face="plain",family="serif"),
axis.title=element_text(size=10,face="plain",family="serif"),
axis.text=element_text(size=10,face="plain",family="serif"),
plot.background=element_blank())
pca.legend<-get_legend(landuse.color.and.season.shape.pca + theme(legend.text=element_text(size=8),
legend.title=element_text(size=10),
text=element_text(family="serif"),
legend.position="bottom",
legend.title.align=0.5,
legend.box="horizontal",
legend.box.just="top",
legend.background=element_rect(fill="white",colour=F)))
pca.plot.final<-plot_grid(pca.legend,
landuse.color.and.season.shape.pca + theme(legend.position="none",
panel.background=element_blank()),
nrow=2,rel_heights=c(0.1,1.1))
pca.plot.final
This part of the post does not actually relate to the question but rather is being used to allow this post to be accepted. Apparently if you need to include more details with more the more code you provide. I don't really have any more details to add so here we are... Hope everyone is doing well.
I have a piece of code that is used to generate multiple plots from a dataset. The dataset is filtered based on the parameters required of the visualization and is plotted using ggplot.
library(ggplot2)
summary <- filter(dataframe)
plot <- ggplot(summary)
Now, I have multiple chunks of code for filtering each type of graph and each chunk has a 'summary' and 'plot'.
Is there a way I can plot multiple functions called 'plot' simultaneously? And is there a way to create a userinterface/button that when clicked will generate all the plots in one go?
If each plot is the same type of plot but with different data you could write it once an as #Victor Ordu suggests put them in a list using a loop or something like lapply or map() in the purrr package.
You could then arrange them using ggarrange from the ggpubr package.
For example:
library(tidyverse)
library(ggpubr)
# Plot function
example_plot <- function(x){
ggplot(x, aes(drat, wt)) +
geom_point()
}
# Summarise in a dataframe and plot into a list
graphs <- mtcars %>%
group_by(cyl) %>%
nest() %>%
mutate(graph = map(data, ~ example_plot(.x))) %>%
pull(graph)
# Plot them all!
ggarrange(graphs[[1]], graphs[[2]], graphs[[3]], ncol = 3)
Otherwise just skip straight to arranging them.
I would like to plot both a histogram to a fitted Weibull function on the same graph. The code to plot the histogram is:
hist(data$grddia2, prob=TRUE,breaks=5)
The code for the fitted Weibull function is:(Need the MASS package)
fitdistr(data$grddia2,densfun=dweibull,start=list(scale=1,shape=2))
How do I plot both together on the same graph. I've attached the data set.
Also, bonus to anyone who can provide code that can achieve the same thing, but create a graph for each column of data. Many columns within a data set. Would be nice to have all graphs on the same page.
https://www.dropbox.com/s/ra9c2kkk49vyyyc/Diameter%20Distribution.csv?dl=0
Here is the code
library("ggplot2")
library("dplyr")
library("tidyr")
library("MASS")
# Import dataset and filter the column "treeno"
# Use namespace dplyr:: explicitly because of conflict with MASS:: for function "select"
data <- read.csv("Diameter Distribution.csv") %>%
dplyr::select(-treeno)
# Function to provide the Weibull distribution for each column
# The distribution is calculated based on the estimated scale and shape parameters of the input
fitweibull <- function(column) {
x <- seq(0,7,by=0.01)
fitparam <- column %>%
unlist %>%
fitdistr(densfun=dweibull,start=list(scale=1,shape=2))
return(dweibull(x, scale=fitparam$estimate[1], shape=fitparam$estimate[2]))
}
# Apply function for each column then consolidate all in a data.frame
fitdata <-data %>%
apply(2, as.list) %>%
lapply(FUN = fitweibull) %>%
data.frame()
# Display graphs
multiplyingFactor<-10
ggplot() +
geom_histogram(data=gather(data), aes(x=value, group=key, fill=key), alpha=0.2) +
geom_line(data=gather(fitdata), aes(x=rep(seq(0,7,by=0.01),ncol(fitdata)), y=multiplyingFactor*value, group=key, color=key))
And the output figure
Variant: thanks to the wonderful ggplot2 package you can also have the graphs apart just by adding this final line of code
+ facet_wrap(~ key) + theme(legend.position = "none")
Which gives you this other figure: