Adjusting position of stat_pvalue_manual [duplicate] - r

I want to present two groups with their development over time in a boxplot, and add labels of significance for each group (intra-group comparisons). I have a working example using ggpubr::stat_compare_means, but I am unable to correctly position the two geoms.
I have attempted position = position_dodge(width=0.5)and several other positional elements, but for some reasons they wont move at all. My desired output would have each label set horizontally shifted to be over each groups box, and vertically adjusted to not overlap.
Example code using diamonds:
df <- filter(diamonds, color == "J" | color == "E")
ggplot(data = df, aes(x = cut, y = price, fill = color)) +
geom_boxplot() +
stat_compare_means(method = "t.test",data = filter(df, color == "J"), comparisons = list(c("Fair","Good"),c("Fair","Very Good"),c("Fair","Premium"))) +
stat_compare_means(method = "t.test",data = filter(df, color == "E"), comparisons = list(c("Fair","Good"),c("Fair","Very Good"),c("Fair","Premium")))

Although I really don't think this is a good idea for visualisation - here is a solution. If you use ggpubr, stay in the ggpubr syntax. And use faceting for subgrouping.
P.S. Try a table instead.
library(tidyverse)
library(ggpubr)
mydf <- filter(diamonds, color == "J" | color == "E")
comparisons <- list(c("Fair", "Good"), c("Fair", "Very Good"), c("Fair", "Premium"))
ggboxplot(mydf, x = "cut", y = "price", facet.by = "color") +
stat_compare_means(
method = "t.test", ref.group = "Fair", label = "p.format",
comparisons = comparisons
)
Created on 2020-03-20 by the reprex package (v0.3.0)

Related

GGPLOT2: Stacked bar plot for two discrete variable columns

I have a dataset with three columns (one categorical column and two-discrete variables column). I want to make a stacked bar plot to compare the values of the two discrete variables for each category. However, I get continuous coloring rather than discrete colors.
Reproducible code
sampleData <- data.frame(grp = c("A","B", "C"),
var_1 = c(15,20, 25),
var_2 = c(12, 13, 20))
sampleData
p <- ggplot(sampleData, aes(x = grp, y = var_1, fill= var_2)) +
geom_bar( stat="identity", position = "fill")+
coord_flip()+ theme_bw()
p
Instead, what I want is
*Var2 will always be smaller than its corresponding Var1 value for a particular category.
Thanks for the help!
Your problem here is that you haven't fixed your tibble from Wide to Long.
FixedData <- sampleData %>%
pivot_longer(cols = c("var_1", "var_2"), names_prefix = "var_",
names_to = "Variable Number", values_to = "ValueName")
Once you do this, the problem becomes much easier to solve. You only need to change a few things, most notably the y, fill, and position variables to make it work.
p2 <- ggplot(FixedData, aes(x = grp, y = ValueName, fill = `Variable Number`)) +
geom_bar(stat="identity", position = "stack")+
coord_flip()+ theme_bw()
p2

GGplot: Two stacked bar plots side by side (not facets)

I am trying to recreate this solution using ggplot2 in R: Combining two stacked bar plots for a grouped stacked bar plot
diamonds %>%
filter(color=="D"|color=="E"|color=="F") %>%
mutate(dummy=rep(c("a","b"),each=13057)) %>%
ggplot(aes(x=color,y=price))+
geom_bar(aes(fill=clarity),stat="identity",width=.25)+
facet_wrap(~cut)
I added a new variable to the diamonds dataset called dummy. dummy has two values: a and b. Let's say I want to compare these two values by creating a bar graph that has two stacked bars right next to each other (one for each value of dummy) for each value of color. How can I manipulate this such that there are two stacked bars for each value of color?
I think it would involve position dodge and/or a separate legend, but I've been unsuccessful so far. I do not want to add another facet - I want these both on the x-axis within each facet.
Similiar to the approach in the post you have linked one option to achieve your desired result would be via two geom_col and by converting the x axis variable to a numeric like so. However, doing so requires to set the breaks and labels manually via scale_x_continuous. Additionally I made use of the ggnewscale package to add a second fill scale:
library(ggplot2)
library(dplyr)
d <- diamonds %>%
filter(color == "D" | color == "E" | color == "F") %>%
mutate(dummy = rep(c("a", "b"), each = 13057))
ggplot(mapping = aes(y = price)) +
geom_col(data = filter(d, dummy == "a"), aes(x = as.numeric(color) - .15, fill = clarity), width = .3) +
scale_fill_viridis_d(name = "a", guide = guide_legend(order = 1)) +
scale_x_continuous(breaks = seq_along(levels(d$color)), labels = levels(d$color)) +
ggnewscale::new_scale_fill() +
geom_col(data = filter(d, dummy == "b"), aes(x = as.numeric(color) + .15, fill = clarity), width = .3) +
scale_fill_viridis_d(name = "b", option = "B", guide = guide_legend(order = 2)) +
facet_wrap(~cut)

Correct positioning of multiple significance labels on dodged groups in ggplot

I want to present two groups with their development over time in a boxplot, and add labels of significance for each group (intra-group comparisons). I have a working example using ggpubr::stat_compare_means, but I am unable to correctly position the two geoms.
I have attempted position = position_dodge(width=0.5)and several other positional elements, but for some reasons they wont move at all. My desired output would have each label set horizontally shifted to be over each groups box, and vertically adjusted to not overlap.
Example code using diamonds:
df <- filter(diamonds, color == "J" | color == "E")
ggplot(data = df, aes(x = cut, y = price, fill = color)) +
geom_boxplot() +
stat_compare_means(method = "t.test",data = filter(df, color == "J"), comparisons = list(c("Fair","Good"),c("Fair","Very Good"),c("Fair","Premium"))) +
stat_compare_means(method = "t.test",data = filter(df, color == "E"), comparisons = list(c("Fair","Good"),c("Fair","Very Good"),c("Fair","Premium")))
Although I really don't think this is a good idea for visualisation - here is a solution. If you use ggpubr, stay in the ggpubr syntax. And use faceting for subgrouping.
P.S. Try a table instead.
library(tidyverse)
library(ggpubr)
mydf <- filter(diamonds, color == "J" | color == "E")
comparisons <- list(c("Fair", "Good"), c("Fair", "Very Good"), c("Fair", "Premium"))
ggboxplot(mydf, x = "cut", y = "price", facet.by = "color") +
stat_compare_means(
method = "t.test", ref.group = "Fair", label = "p.format",
comparisons = comparisons
)
Created on 2020-03-20 by the reprex package (v0.3.0)

Highlight / Draw a box around some of the plots when using `facet_grid` in ggplot2

I am creating a matrix of plots similar to
ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_grid(rows = vars(cyl), cols = vars(drv))
Now, I would like to have some way to highlight some of the individual plots, say the ones where cyl is 5 or 6, and drv is f. So, ideally, this might look like this:
But I would also be happy with those panels having a different look by setting ggtheme to classic or similar.
However, it is very unclear to me how I can modify individually selected plots within a matrix of plots generated via facet_grid
From #joran answer found here, this is what I get :
[EDIT] code edited to select multiple facets
if(!require(tidyverse)){install.packages("tidyverse")}
library(tidyverse)
#dummy dataset
df = data.frame(type = as.character(c("a", "b", "c", "d")),
id = as.character(c("M5", "G5", "A7", "S3")),
val = runif(4, min = 1, max = 10),
temp = runif(4))
# use a rectangle to individually select plots
ggplot(data = df, aes(x = val, y = temp)) +
geom_point() +
geom_rect(data = subset(df, type %in% c("b", "c") & id %in% c("A7","G5")),
fill = NA, colour = "red", xmin = -Inf,xmax = Inf,
ymin = -Inf,ymax = Inf) +
facet_grid(type~id)
It does not use theme() but it seems simple enough to highlight some facets.

Hide legend for a single geom in ggplot2

I map the same variable (color) to color in two different geoms. I want them either to appear in separate legends (DHJ and EFI) or preferably just skip the second legend (for E, F, and I) altogether. Currently, R mixes the two together and gives me a legend that lists DEFHIJ in alphabetical order all mixed together.
Basically, I want to graph today's points onto some smoothed lines that use a standard dataset. I don't want there to be a legend for the smoothed lines - we are all familiar with them and they are standard on all our graphs. I just want a legend for the points only.
I've tried show.legend = FALSE as suggested elsewhere, but that doesn't seem to have an effect. guides(color = FALSE) removes the entire legend.
Reprex:
library(tidyverse)
set1 <- diamonds %>%
filter(color %in% c("D", "H", "J"))
set2 <- diamonds %>%
filter(color %in% c("E", "F", "I"))
ggplot() +
geom_point(data = set1,
aes(x = x, y = y, color = color)) +
geom_smooth(data = set2,
show.legend = FALSE,
aes(x = x, y = y, color = color))
Here is the graph that is produced. It has all 6 letters in the legend, instead of only DHJ.
If you want the legend to show only the colors from one dataset you can do so by setting the breaks in scale_color_discrete() to those values.
... +
scale_color_discrete(breaks = unique(set1$color) )
If you aren't using the colors of the lines, since this is standard background info, you could add the lines by using group ingeom_smooth() instead of color. (Also see linetype if you wanted to be able to tell the lines apart.)
ggplot() +
geom_point(data = set1,
aes(x = x, y = y, color = color)) +
geom_smooth(data = set2,
aes(x = x, y = y, group = color))

Resources