add p value from pairwise comparison within each category - r

library(tidyverse)
library(ggpubr)
df <- tibble(
iq = rnorm(150, 100, 15),
condition = rep(c("A", "B"), each = 75),
time = rep(c("t1", "t2", "t3","t1", "t2","t3"), each = 25)
)
ggbarplot(df,
x = "condition",
y = "iq",
fill = "time",
palette = "grey",
add = "mean_se", add.params = list(group = "time"),
position = position_dodge(0.8)) +
stat_compare_means(aes(group = time),label = "p.signif", paired = TRUE,
comparisons = list(c("t1", "t2"),
c("t1", "t3"),
c("t2", "t3")))
stat_compare_means() couldn't conduct pairwise comparison separately for each category.

Maybe this code could help:
You can calculate first the p-values for each comparisons and then add them to the plot.
library(tidyverse)
if(!require(devtools)) install.packages("devtools") devtools::install_github("kassambara/rstatix")
library(rstatix)
library(ggpubr)
stat.test <- df %>% group_by(condition) %>% t_test(iq ~ time) %>% adjust_pvalue() %>% add_significance("p.adj") %>% mutate(y.position = 115)
stat.test
ggbarplot(df,
x = "time",
y = "iq",
facet.by = "condition",
fill = "time",
palette = "grey",
add = "mean_se", add.params = list(group = "time"),
position = position_dodge(0.8)) + stat_pvalue_manual(stat.test,label = "p.adj", y.position = "y.position")

Related

Compare the mean of multiple variables within two groups - using GGPLOT

#Sample data
set.seed(42)
DB = data.frame(Group =c(rep("1",16),
rep("2",4)) ,
Score1 = sample(1:20,20, replace = T),
Score2 = sample(1:20,20, replace = T),
Score3 = sample(1:20,20, replace = T),
Score4 = sample(1:20,20, replace = T))
I want to plot two bar charts comparing the mean of each score in both groups.
So the right side will be with a Title "Group 1 mean scores" and left side (left barchart) is "Group 2 mean scores"
Thanks.
You can pivot to long format and use stat = "summary"
library(tidyverse)
DB %>%
pivot_longer(-1, names_to = "Score") %>%
ggplot(aes(Group, value, fill = Score)) +
geom_bar(position = position_dodge(width = 0.8, preserve = "total"),
stat = "summary", fun = mean, width = 0.6) +
scale_fill_brewer(palette = "Set2") +
theme_minimal(base_size = 20)
Or if you prefer facets, you can do:
library(tidyverse)
DB %>%
pivot_longer(-1, names_to = "Score") %>%
mutate(Group = paste("Group", Group)) %>%
ggplot(aes(Score, value, fill = Score)) +
geom_bar(stat = "summary", fun = mean, width = 0.6) +
scale_fill_brewer(palette = "Set2", guide = "none") +
facet_grid(.~Group) +
theme_bw(base_size = 20)
Created on 2022-11-13 with reprex v2.0.2

How can I view the exact values I generated in my graph?

I have made a graph but I don't know how to view the exact values of the bars on the graph. Here is my code in case it is needed. I also have a picture of my graph.
Step 1: Load the tidyverse and tidyquant:
install.packages("tidyverse")
install.packages("tidyquant")
library("tidyverse")
library("tidyquant")
#STEP 2: Getting stocks data:
stocks <- c("TSLA", "UPST", "PLTR", "SPOT", "SHOP", "SPY", "BND")
stocks_df <- tq_get(stocks, from = '2017-01-01')
#Step 3: Group data:
port <- tq_get(c("TSLA", "UPST", "PLTR", "SPOT", "SHOP", "SPY", "BND"),
from = '2017-01-01')%>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "daily",
col_rename = "ret")
#Step 4: Computing portfolio returns:
myport <- port %>% tq_portfolio(symbol,ret, c(0.2, 0.2, 0.2, 0.2, 0.2, 0, 0))
benchmark <- port %>% tq_portfolio(symbol, ret, c(0, 0, 0, 0, 0, 0.6, 0.4))
#Step 5: Computing portfolio measure:
mVaR <- myport %>% tq_performance(portfolio.returns,
performance_fun = VaR,
p = 0.95,
method = "historical",
portfolio_method = "single") %>%
add_column(symbol = "MyPort", .before = 1)
bVaR <- benchmark %>% tq_performance(portfolio.returns,
performance_fun = VaR,
p = 0.95,
method = "gaussian",
portfolio_method = "single") %>%
add_column(symbol = "Benchmark", .before = 1)
#Step 6: Computing portfolio measure: Expected Shortfall (ES):
mES <- myport %>% tq_performance(portfolio.returns,
performance_fun = ES,
p = 0.95,
method = "historical",
portfolio_method = "single") %>%
add_column(symbol = "MyPort", .before = 1)
bES <- benchmark %>% tq_performance(portfolio.returns,
performance_fun = ES,
p = 0.95,
method = "gaussian",
portfolio_method = "single") %>%
add_column(symbol = "Benchmark", .before = 1)
#Step 7: Combining the results into a single table using rbind (row bind):
bothVaR <- rbind(mVaR, bVaR)
bothES <- rbind(mES, bES)
results <- inner_join(bothVaR, bothES)
#Step 8: Re-shaping the table into a data frame suitable for plotting:
results <- results %>%
pivot_longer(!symbol, names_to = "measure", values_to = "value")
#Step 9: Plot the results:
results %>% ggplot(aes(x = measure, y = abs(value), fill = symbol)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Value at Risk Approach to Measure a Diversified Portfolio",
x = "Risk Measure", y = " ", fill = " ") + theme_minimal() +
theme(plot.title = element_text(hjust = 0.5), legend.position = "top")
I tried looking up on Google but the examples they give is for a specific set of data with different names and values. I don't know to implement it into my code for my specific script and graph.
If you want to plot the values on the plot, this could work:
library(ggrepel)
results %>% ggplot(aes(x = measure, y = abs(value), fill = symbol)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Value at Risk Approach to Measure a Diversified Portfolio",
x = "Risk Measure", y = " ", fill = " ") + theme_minimal() +
theme(plot.title = element_text(hjust = 0.5), legend.position = "top") +
geom_text_repel(aes(label = round(abs(value), digits = 3)),
position = position_dodge(width = 1), direction = "y", size = 2.5)
One way to visualize the values when you hover over them is to use ggplotly as follow:
install.packages("plotly")
library(plotly)
Once you load the library, you store then your plot in a variable that I name
p
and do following:
plotly(p)

How can I add lines with labels to a barplot using ggplot2

You can copy the following code for the example which creates a barplot using ggplot2:
set.seed(999)
similarity_context_set1 = matrix(rnorm(10*3,10,1),ncol=3)
similarity_context_set2 = matrix(rnorm(10*3,10,1),ncol=3)
attraction_prop_context_set1 = matrix(rnorm(10*3,10,1),ncol=3)
attraction_prop_context_set2 = matrix(rnorm(10*3,10,1),ncol=3)
compromise_context_set1 = matrix(rnorm(10*3,10,1),ncol=3)
compromise_context_set2 = matrix(rnorm(10*3,10,1),ncol=3)
library(tidyverse)
library(ggthemes)
# add all matrices in a list. I use lst here since the ouptut is
# a named list
df <- data.frame(name1 = rep(rep(c("attraction", "compromise", "similarity"), each = 3), 2),
name2 = rep(c("1", "2"), each = 9),
x = rep(c("Third", "X", "Y"), 6),
y = rep(12, 18),
label = c("Now", "you", "can", "use", "any", "label", "you", "want",
"by", "inserting", "it", "as", "a", "string", "into", "this",
"character", "vector"))
lst(similarity_context_set1,
similarity_context_set2,
attraction_prop_context_set2,
attraction_prop_context_set1,
compromise_context_set1,
compromise_context_set2) %>%
# transform to tibble and add column names
map(as_tibble) %>%
map(set_names, c("X", "Y","Third")) %>%
# bind the list to one dataframe, add list names to column by
# setting .id
bind_rows(.id = "name") %>%
# transform data from wide to long as it is recommended ggplot
#input format here
pivot_longer(-1,names_to = "x", values_to = "y") %>%
# make to columns for facetting
separate(name, into = c("name1", "name2"), sep = "_", extra = "merge") %>%
mutate(name2 = str_extract(name2, "[0-9]")) %>%
# finally the plot
ggplot(aes(x, y, group=x, fill = x)) + theme_hc(base_size = 13)+
geom_bar(stat = "summary", fun = "mean",alpha=0.8 )+
scale_fill_manual(values = c("Y" = "gray1","X" = "gray1","Third" = "gray1"), guide="none" )+
facet_grid(name2~name1)+
stat_summary(fun.data = mean_se, geom = "errorbar", width=0.2)+
ggtitle("Perceptual Domain")+
theme(plot.title = element_text(hjust = 0.5))+
labs(x = "Response", y = "Mean Choice Proportion")+
geom_text(data = df, aes(label = label))
My question is how can I add different lines with labels? On the picture below you can see an example of what I mean:
Does anyone have an idea how I can do something like that?
This is much the same as your last question, and the answer much the same as the last answer. Please take time to read and understand what is happening in the code.
Create this data frame:
df2 <- data.frame(name1 = rep(c("attraction", "compromise", "similarity"), 2),
name2 = rep(c("1", "2"), each = 3),
yintercept = runif(6, 5, 10),
label = c("Now", "use", "whatever",
"label", "you", "like"))
And add this line:
geomtextpath::geom_texthline(data = df2, aes(yintercept = yintercept, label = label),
color = "red", size = 6, hjust = 0.8, vjust = -0.2, fontface = 2)

ggplot using facet_wrap of multiple data.frame with different length in R?

I am trying to achieve the attached hand drawn figure using the code below but its showing white spaces for all the years that i do not have data for. Any help would be appreciated.
library(lubridate)
library(tidyverse)
set.seed(123)
D1 <- data.frame(Date = seq(as.Date("2001-07-14"), to= as.Date("2001-07-21"), by="day"),
A = runif(8, 0,10),
D = runif(8,5,15)) %>%
gather(-Date, key = "Variable", value = "Value")
D2 <- data.frame(Date = seq(as.Date("1998-07-14"), to= as.Date("1998-08-30"), by="day"),
A = runif(48, 0,10),
D = runif(48,5,15)) %>%
gather(-Date, key = "Variable", value = "Value")
D <- bind_rows(D1,D2) %>% mutate(Year = year(Date))
my_linetype <- setNames(c("dashed", "solid"), unique(D$Year))
ggplot(data = D, aes(x = Date, y = Value, color = as.factor(Year), linetype = as.factor(Year)))+
geom_line(size = 1.1)+ facet_wrap(~Variable, scales = "free_y", nrow=2)
Desired Out
You can make a dummy Date variable in your data.frame where the year is equal among different groups. In the example below this added in the mutate() statement under the Unyear variable.
D <- bind_rows(D1,D2) %>% mutate(Year = year(Date),
Unyear = {year(Date) <- 0; Date})
my_linetype <- setNames(c("dashed", "solid"), unique(D$Year))
ggplot(data = D, aes(x = Unyear, y = Value, color = as.factor(Year), linetype = as.factor(Year)))+
geom_line(size = 1.1)+ facet_wrap(~Variable, scales = "free_y", nrow=2)

Stacked barchart in highcharter with molten data

I am trying to plot frequency on a stacked barchart with the following code
x = data.frame(
Clinic = c('A','A','A','A','A','A','B','B','B','B','B','C','C','C','C'),
Doctor = c('Kooner','Halliday','Katz','Alizadeh','Patel','Baxter','Kooner','Halliday','Patel','Katz','Alizadeh','Baxter','Katz','Patel','Alizadeh'),
VisitDate = c('2014-06-01','2014-06-01','2014-06-15','2014-07-01','2014-07-01','2014-07-01','2014-07-01','2014-07-01','2014-07-01','2014-08-01','2014-08-01','2014-07-01','2014-08-01','2014-09-01','2014-08-01')
)
allDates = data.frame(VisitDate=c('2014-06-01','2014-06-15','2014-07-01','2014-07-15','2014-08-01','2014-08-15','2014-09-01'))
library(plyr)
visits = plyr::count(x[,c(1,3)])
visits1 = merge(allDates,visits, all.x = TRUE)
library(highcharter)
hc = highchart() %>%
hc_chart(type = "column") %>%
hc_yAxis(title = list(text = "Visits")) %>%
hc_xAxis(categories = allDates$VisitDate) %>%
hc_plotOptions(column = list(
dataLabels = list(enabled = FALSE),
stacking = "normal",
enableMouseTracking = TRUE)
) %>%
hc_series(list(name="Clinic-A",data=merge(allDates,visits1[visits1$Clinic == "A", ], all.x = TRUE)[,3]),
list(name="Clinic-B",data=merge(allDates,visits1[visits1$Clinic == "B", ], all.x = TRUE)[,3]),
list(name="Clinic-C",data=merge(allDates,visits1[visits1$Clinic == "C", ], all.x = TRUE)[,3])
)
hc
I can plot this with ggplot without much coercion. Is it possible to do this in the highcharter without too much coercion(for example the 4 merge statements). The answer to this post doesn't work for me.
library(ggplot2)
library(scales)
ggplot()+
geom_bar(aes(y = freq, x = as.Date(VisitDate), fill = Clinic),data = visits, stat = "identity")+
theme(legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank())+
scale_x_date(date_breaks = "1 month")+
scale_y_continuous(breaks = pretty_breaks())
hchart, used in dataframe is similar to qplot. hchart try to have the same behavior as qplot.
So, how about this?:
hchart(visits, "column", x = as.Date(VisitDate), y = freq, group = Clinic) %>%
hc_plotOptions(column = list(
dataLabels = list(enabled = FALSE),
stacking = "normal",
enableMouseTracking = TRUE)
)
Hope this help.

Resources