Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interaction on x-axis #16

Closed
ghost opened this issue May 22, 2017 · 3 comments
Closed

Interaction on x-axis #16

ghost opened this issue May 22, 2017 · 3 comments

Comments

@ghost
Copy link

ghost commented May 22, 2017

Hi, first I wanna say thanks for your package - it works great so far but...
I have an issue with putting significance annotations to my bar plot where on x-axis I plotted an interaction between A and B (levels of A are: A1, A2 and levels of B are: B1, B2):

data %>%
ggplot(aes(x = interaction(A, B), y = Y)) +
geom_bar(stat="identity", position=position_dodge(width = .9), aes(fill = interaction(A, B)), color = "white") +
geom_errorbar(aes(ymin=Y-se, ymax=Y+se), width = .2, size = .8,
position = position_dodge(width = .9))

When I run "levels(interaction(A, B))" I get: A1.B1, A1.B2, A2.B1, A2.B2, so if I wanna add to this plot geom_signif that looks for example like this:
geom_signif(comparisons = list(c("A1.B1", "A2.B1"))

I get an error:

Error in f(...) :
Can only handle data with groups that are plotted on the x-axis

Is there any solution to fix it? Thanks for help

@const-ae
Copy link
Owner

Hey, currently there is no easy way to add the significance brackets with geom_bar(position="dodge"), because I haven't figured how ggplot internally places the bars.

The best approach right now is to manually set the x and y positions of the brackets, as shown in the vignette in the Advanced Options chapter.

But that issue is something I would really like to add as a feature, so I hope that maybe I soon I have a better answer than this.

@cafernandezlo
Copy link

same problem here with interaction but with geom_boxplot(). This happens also without the interaction() call everytime you try to plot two different levels for each value in the x-axis. For example, with one of the examples here you'll get the same error.

rm(list = ls())
library(datasets)
library(ggplot2)

data(airquality)
airquality$Month <- factor(airquality$Month,
labels = c("May", "Jun", "Jul", "Aug", "Sep"))
airquality_trimmed <- airquality[which(airquality$Month == "Jul" |
airquality$Month == "Aug" |
airquality$Month == "Sep"), ]
airquality_trimmed$Temp.f <- factor(ifelse(airquality_trimmed$Temp > ``mean(airquality_trimmed$Temp), 1, 0),
labels = c("Low temp", "High temp"))

p10 <- ggplot(airquality_trimmed, aes(x = Month, y = Ozone, fill = Temp.f)) +
geom_boxplot(alpha=0.7) +
geom_signif(comparisons = list(c("Jul", "Aug")),
map_signif_level=TRUE)+
scale_y_continuous(name = "Mean ozone in\nparts per billion",
breaks = seq(0, 175, 25),
limits=c(0, 175)) +
scale_x_discrete(name = "Month") +
ggtitle("Boxplot of mean ozone by month") +
theme_bw() +
theme(plot.title = element_text(size = 14, family = "Tahoma", face = "bold"),
text = element_text(size = 12, family = "Tahoma"),
axis.title = element_text(face="bold"),
axis.text.x=element_text(size = 11),
legend.position = "bottom") +
scale_fill_brewer(palette = "Accent") +
labs(fill = "Temperature")
p10

@const-ae
Copy link
Owner

Hey, the problem still is that geom_boxplot by default uses position="dodge" if an additional group/color/fill is defined, which means that the other layers don't know where the exact data will be plotted. Thus it is necessary to manually define where the brackets are supposed to start and end.

In the new release v0.3.0 I have extended the package with exactly that feature and made it simpler to move brackets to a custom location. The example that you provide would then look like this:

library(datasets)
library(ggplot2)
library(ggsignif)

data(airquality)
airquality$Month <- factor(airquality$Month,
                           labels = c("May", "Jun", "Jul", "Aug", "Sep"))
airquality_trimmed <- airquality[which(airquality$Month == "Jul" |
                                         airquality$Month == "Aug" |
                                         airquality$Month == "Sep"), ]
airquality_trimmed$Temp.f <- factor(ifelse(airquality_trimmed$Temp > mean(airquality_trimmed$Temp), 1, 0),
                                    labels = c("Low temp", "High temp"))

annot_1 <- wilcox.test(airquality_trimmed[airquality_trimmed$Month == "Jul" & airquality_trimmed$Temp.f == "High temp", "Ozone"],
                       airquality_trimmed[airquality_trimmed$Month == "Aug" & airquality_trimmed$Temp.f == "High temp", "Ozone"])$p.value

annot_2 <- wilcox.test(airquality_trimmed[airquality_trimmed$Month == "Jul" & airquality_trimmed$Temp.f == "Low temp", "Ozone"],
                       airquality_trimmed[airquality_trimmed$Month == "Aug" & airquality_trimmed$Temp.f == "Low temp", "Ozone"])$p.value

ggplot(airquality_trimmed, aes(x = Month, y = Ozone, fill = Temp.f)) +
  geom_boxplot(alpha=0.7) +
  geom_signif(annotations = c(formatC(annot_1, digits=3),formatC(annot_2, digits=3)),
              y_position = c(150, 180), xmin=c(1.2, 0.8), xmax=c(2.2, 1.8)) +
  scale_y_continuous(name = "Mean ozone in\nparts per billion",
                     breaks = seq(0, 175, 25),
                     limits=c(0, 175)) +
  scale_x_discrete(name = "Month") +
  ggtitle("Boxplot of mean ozone by month") +
  theme_bw() +
  theme(plot.title = element_text(size = 14, family = "Tahoma", face = "bold"),
        text = element_text(size = 12, family = "Tahoma"),
        axis.title = element_text(face="bold"),
        axis.text.x=element_text(size = 11),
        legend.position = "bottom") +
  scale_fill_brewer(palette = "Accent") +
  labs(fill = "Temperature") +
  ylim(NA, 190)

image

Note that you have to calculate the p-value used for annotation separately in this case as I have done here

annot_1 <- wilcox.test(airquality_trimmed[airquality_trimmed$Month == "Jul" & airquality_trimmed$Temp.f == "High temp", "Ozone"],
                       airquality_trimmed[airquality_trimmed$Month == "Aug" & airquality_trimmed$Temp.f == "High temp", "Ozone"])$p.value

and you then have to manually define the location of the brackets:

  geom_signif(annotations = c(formatC(annot_1, digits=3),formatC(annot_2, digits=3)),
              y_position = c(150, 180), xmin=c(1.2, 0.8), xmax=c(2.2, 1.8)) +

I know this does only circumvents the problem, but as I don't see any alternative I will close this issue for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants