ggplot2: Multiple trend lines in a faceted plot - r

I'm trying to add two trend lines to the data plotted in my faceted plot for two different depths (Road=color). Geom_smooth works to generate the first plot(pred_new2) with trend line, but as soon as I add facet_wrap - the plot is generated, but without any trend lines/smoothing and without any error.
pred_new$Site <- factor(pred_new$Site,
levels = c("A", "B", "C", "D", "E", "F", "G", "H", "I"))
pred_new2 <- ggplot(pred_new, aes(x = No_cars, y = Site, color = Road)) +
geom_point() +
geom_smooth(aes(x = No_cars, y = Site, color = Road), method = "lm")
pred_new3 <- pred_new2 +
geom_errorbarh(aes(xmin = No_cars - standerror, xmax = No_cars + standerror))
pred_new4 <- pred_new3 +
facet_wrap(~ Days, scales = "free_x") +
ylab("Site") +
xlab("No_cars") +
theme_classic()
pred_new4
Any help would be greatly appreciated.
pred_new = structure(list(Site = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L),
.Label = c("A", "B", "C", "D", "E", "F", "G", "H", "I"),
class = "factor"),
Days = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L),
.Label = c("Thursday", "Tuesday", "Wednesday"),
class = "factor"),
Road = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L),
.Label = c("east", "west"),
class = "factor"),
No_cars = c(15.266427, 8.323348, 8.368608, 9.747807, 7.976356, 8.5684, 6.604537,
3.812109, 6.719904, 4.799487, 4.996091, 4.796, 4.991479, 4.525789,
5.115136, 4.939559, 4.783792, 4.185007, 3.857553, 3.095228, 2.890727,
3.132784, 3.352974, 3.42561, 2.900284, 2.35416, 2.889976, 17.266427,
10.323348, 10.368608, 11.747807, 9.976356, 10.5684, 8.604537, 5.812109,
8.719904, 6.799487, 6.996091, 6.796, 6.991479, 6.525789, 7.115136,
6.939559, 6.783792, 6.185007, 5.857553, 5.095228, 4.890727, 5.132784,
5.352974, 5.42561, 4.900284, 4.35416, 4.889976),
standerror = c(1.7108483, 0.8175014, 0.6365042, 0.7171749, 0.9978123, 0.9881427,
0.9215597, 0.6365042, 1.6303975, 0.404129, 0.1934362, 0.1503158,
0.1694848, 0.2362161, 0.2337497, 0.2180687, 0.1604379, 0.3902528,
0.3276444, 0.1568268, 0.1218673, 0.1374084, 0.1915103, 0.1895107,
0.1767974, 0.1300738, 0.3163943, 1.7108483, 0.8175014, 0.6365042,
0.7171749, 0.9978123, 0.9881427, 0.9215597, 0.6365042, 1.6303975,
0.404129, 0.1934362, 0.1503158, 0.1694848, 0.2362161, 0.2337497,
0.2180687, 0.1604379, 0.3902528, 0.3276444, 0.1568268, 0.1218673,
0.1374084, 0.1915103, 0.1895107, 0.1767974, 0.1300738, 0.3163943)),
row.names = c(NA, -54L), class = "data.frame")

I guess that you swap the axes. To include trend you need numeric values for Site, so I include as.numeric(Site) in ggplot.
library(ggplot2)
ggplot(pred_new, aes(x = as.numeric(Site), y = No_cars, color = Road)) +
geom_point() +
geom_smooth(method = "lm") +
geom_errorbar(aes(ymin = No_cars - standerror, ymax = No_cars + standerror)) +
xlab("Site") +
# this will recode your x-axis
scale_x_continuous(breaks = 1:9, labels = LETTERS[1:9]) +
facet_wrap(~ Days, scales = "free_x") +
theme_classic() +
# you can swap x and y axes with coord_flip()
coord_flip()
Is this desired output?
In case you really want Site on the y-axis, you can simply flip x and y axes:
ggplot(pred_new, aes(y = as.numeric(Site), x = No_cars, color = Road)) +
geom_point() +
geom_smooth(method = "lm") +
geom_errorbarh(aes(xmin = No_cars - standerror, xmax = No_cars + standerror)) +
ylab("Site") +
# this will recode your y-axis
scale_y_continuous(breaks = 1:9, labels = LETTERS[1:9]) +
facet_wrap(~ Days, scales = "free_x") +
theme_classic()

Related

ggplot: why does order on x-axis not level instead of printing alphabetically?

I have this plot
With
> str(a)
'data.frame': 150 obs. of 2 variables:
$ study: Factor w/ 7 levels "A","S","H","D",..: 7 2 4 5 3 1 7 2 2 4 ...
$ n : Factor w/ 6 levels "N0","N1","N2a",..: 1 1 2 4 1 1 2 1 1 1 ...
I would like the x-axis to arrange by sample size, i.e. level = c("all", "S", "H", "B", "C", "A", "K", "D")
As you can see, the order is printed alphabetically.
I have tried specifying as ... aes(x=factor(nystudie, level=c(...), but that does not work. What am I doing wrong? I followed this post
library(tidyverse)
colsze <- c("#E1B930", "#2C77BF", "#E38072", "#6DBCC3", "grey40", "black", "#8B3A62")
a %>%
as_tibble() %>%
mutate(nystudie=as.factor(study),
n.seven=as.factor(n)) %>%
bind_rows(., mutate(., nystudie="all")) %>%
count(nystudie, n.seven, .drop=F) %>%
ggplot(aes(x = factor(nystudie, level = c("all", "S", "H", "B", "C", "A", "K", "D")),
n, color = n.seven, fill= n.seven, label=n)) +
geom_col(position = position_dodge2(preserve = "single", padding = 0.1))+
geom_text(aes(label=n),position = position_dodge2(0.9), vjust=-0.25, fontface=2, cex=4.5, show.legend = F) +
scale_fill_manual(values = alpha(colsze, .2),
name="Stage", label=c("N0", "N1", "N2a", "N2b", "N2c", "N3")) +
scale_color_manual(values = colsze,
name="Stage", label=c("N0", "N1", "N2a", "N2b", "N2c", "N3")) +
scale_x_discrete(name = "", label=c("All\n(n=1,905)",
"A\n(n=221)",
"B\n(n=234)",
"C\n(n=232)",
"D\n(n=108)",
"H\n(n=427)",
"K\n(n=221)",
"S\n(n=462)")) +
scale_y_continuous(name="",
breaks=seq(0,950,100)) +
coord_cartesian(ylim = c(0,950)) +
guides(fill = guide_legend(nrow = 1)) + theme(axis.text.x = element_text(color = "grey20", size =15),
legend.text=element_text(size=16), legend.title=element_text(size=16, face="bold"),
legend.position="top")
Data sample
a <- structure(list(study = structure(c(7L, 2L, 4L, 5L, 3L, 1L, 7L,
2L, 2L, 4L, 4L, 6L, 2L, 5L, 3L, 7L, 1L, 1L, 2L, 6L, 1L, 3L, 2L,
7L, 2L, 2L, 6L, 6L, 6L, 2L, 1L, 2L, 6L, 1L, 2L, 2L, 3L, 4L, 2L,
3L, 2L, 5L, 2L, 3L, 6L, 5L, 3L, 2L, 4L, 3L, 5L, 6L, 2L, 7L, 2L,
3L, 3L, 3L, 7L, 7L, 3L, 4L, 1L, 1L, 2L, 2L, 6L, 2L, 3L, 2L, 3L,
2L, 1L, 2L, 3L, 5L, 3L, 1L, 1L, 1L, 7L, 4L, 3L, 2L, 4L, 3L, 3L,
3L, 2L, 6L, 7L, 3L, 2L, 2L, 6L, 2L, 2L, 6L, 7L, 3L, 3L, 3L, 6L,
2L, 2L, 7L, 7L, 1L, 1L, 6L, 3L, 3L, 7L, 1L, 2L, 7L, 1L, 1L, 7L,
4L, 4L, 4L, 2L, 3L, 3L, 6L, 1L, 4L, 6L, 3L, 5L, 5L, 3L, 3L, 7L,
5L, 3L, 6L, 3L, 5L, 2L, 3L, 7L, 6L, 2L, 1L, 6L, 5L, 1L, 6L), .Label = c("A",
"S", "H", "D", "K", "C", "B"), class = "factor"), n = structure(c(1L,
1L, 2L, 4L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 4L, 2L, 1L, 2L,
3L, 2L, 2L, 4L, 4L, 4L, 2L, 4L, 1L, 2L, 4L, 1L, 1L, 4L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 4L, 1L, 1L, 4L, 2L, 1L, 1L, 4L, 1L, 1L, 2L,
1L, 5L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 4L, 1L, 2L, 1L,
4L, 1L, 1L, 1L, 1L, 6L, 1L, 2L, 5L, 4L, 2L, 6L, 1L, 4L, 2L, 4L,
2L, 1L, 1L, 4L, 1L, 2L, 1L, 1L, 4L, 4L, 4L, 1L, 4L, 2L, 1L, 1L,
4L, 2L, 1L, 2L, 1L, 5L, 5L, 1L, 4L, 1L, 2L, 2L, 4L, 1L, 1L, 1L,
2L, 4L, 4L, 1L, 5L, 2L, 1L, 5L, 2L, 4L, 1L, 1L, 1L, 4L, 4L, 1L,
1L, 4L, 4L, 4L, 1L, 4L, 4L, 1L, 4L, 5L, 4L, 5L, 1L, 5L, 1L, 1L,
4L, 2L, 1L, 2L, 4L), .Label = c("N0", "N1", "N2a", "N2b", "N2c",
"N3"), class = "factor")), row.names = c(NA, -150L), class = "data.frame")
The levels are being changed again at scale_x_discrete step. Try :
library(dplyr)
library(ggplot2)
a %>%
mutate(nystudie=as.factor(study),
n.seven=as.factor(n)) %>%
bind_rows(., mutate(., nystudie="all")) %>%
count(nystudie, n.seven, .drop=F) %>%
mutate(nystudie = factor(nystudie,
level = c("all", "S", "H", "B", "C", "A", "K", "D"),
labels = c("All\n(n=1,905)", "S\n(n=462)", "H\n(n=427)", "B\n(n=234)",
"C\n(n=232)", "A\n(n=221)", "K\n(n=221)", "D\n(n=108)"))) %>%
ggplot(aes(x = nystudie,
n, color = n.seven, fill= n.seven, label=n)) +
geom_col(position = position_dodge2(preserve = "single", padding = 0.1))+
geom_text(aes(label=n),position = position_dodge2(0.9), vjust=-0.25, fontface=2, cex=4.5, show.legend = F) +
scale_fill_manual(values = alpha(colsze, .2),
name="Stage", label=c("N0", "N1", "N2a", "N2b", "N2c", "N3")) +
scale_color_manual(values = colsze,
name="Stage", label=c("N0", "N1", "N2a", "N2b", "N2c", "N3")) +
scale_x_discrete(name = "") +
scale_y_continuous(name="",
breaks=seq(0,950,100)) +
coord_cartesian(ylim = c(0,950)) +
guides(fill = guide_legend(nrow = 1)) +
theme(axis.text.x = element_text(color = "grey20", size =15),
legend.text=element_text(size=16),
legend.title=element_text(size=16, face="bold"),
legend.position="top")

ggarrange() function overvrites the color of my boxplots

I am making two boxplots and want to arrange them beside each other. I have made each of them look like I want when displaying them separately but when I use ggarrange() the colors disappear. This is my code for the plots:
BOX1_data <- read.table(file = "clipboard",
sep = "\t", header=TRUE)
BOX1_data$Diagnosis <- as.factor(BOX1_data$Diagnosis)
BOX1plot <- ggplot(BOX1_data, aes(x=Diagnosis, y=No.Variants, fill= Diagnosis)) + geom_boxplot() +
scale_fill_brewer(palette = "Dark2") +
scale_x_discrete(labels = c("AC\nN=38", "SqCC\nN=15", "SCLC\nN=8", "BL disease\nN=16"))
BOX2_data <- read.table(file = "clipboard",
sep = "\t", header=TRUE)
BOX2_data$Stage <- as.factor(BOX2_data$Stage)
BOX2plot <- ggplot(BOX2_data, aes(x=Stage, y=No.Variants, fill = Stage)) + geom_boxplot(width = 0.4) +
scale_fill_brewer(palette = "Dark2") +
scale_x_discrete(labels = c("Stage I-III\nN=24", "Stage IV\nN=37"))
To arrange the plots I then write:
BOX_list <- list(BOX1plot, BOX2plot)
ggarrange(plotlist = BOX_list, labels = c('A', 'B'), ncol = 2)
The easiest way of getting rid of gridlines etc I thought was by using theme_set() and I think that this might be my problem.
My code is:
theme_set(theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), panel.background = element_blank(),
axis.line = element_line(colour = "grey")))
I realize that theme_bw() overwrites my colors in the boxes. But I have tried removing it, switching it for theme_transparent() (this removes all my labels) and neither works. I have searched for a way of just adding a transparency to my boxes in the theme so that my colors will shine through. I am also suspicious that maybe the palette that I chose might give me the same colors in the two plots which I also do not want. To add, if it matters, I have 4 groups in the first plot and 2 in the second.
dput(BOX1_data)
structure(list(Diagnosis = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("1", "2", "3", "4"), class = "factor"),
No.Variants = c(3L, 4L, 6L, 14L, 3L, 3L, 4L, 3L, 3L, 3L,
8L, 6L, 22L, 10L, 6L, 9L, 1L, 9L, 3L, 4L, 8L, 2L, 13L, 3L,
11L, 19L, 5L, 5L, 3L, 12L, 4L, 2L, 4L, 18L, 8L, 7L, 7L, 12L,
4L, 1L, 6L, 3L, 2L, 8L, 10L, 3L, 15L, 9L, 13L, 13L, 15L,
10L, 10L, 12L, 6L, 3L, 12L, 9L, 15L, 10L, 18L, 3L, 6L, 3L,
6L, 1L, 3L, 3L, 7L, 1L, 2L, 10L, 7L, 7L, 1L, 0L, 2L)), row.names = c(NA,
-77L), class = "data.frame")
dput(BOX2_data)
structure(list(No.Variants = c(3L, 4L, 6L, 14L, 3L, 3L, 4L, 3L,
3L, 3L, 8L, 6L, 22L, 10L, 6L, 9L, 1L, 9L, 3L, 4L, 8L, 2L, 13L,
3L, 11L, 19L, 5L, 5L, 3L, 12L, 4L, 2L, 4L, 18L, 8L, 7L, 7L, 12L,
4L, 1L, 6L, 3L, 2L, 8L, 10L, 3L, 15L, 9L, 13L, 13L, 15L, 10L,
10L, 12L, 6L, 3L, 12L, 9L, 15L, 10L, 18L), Stage = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1",
"2"), class = "factor")), row.names = c(NA, -61L), class = "data.frame")
Grateful for any tips!
As already pointed out, it seems the OP's issue with theme_set() removing the fill colors set in your two plots was solved by updating to a new version of ggplot2. Herein, I have a solution for the second part of OP's question (that was clarified in the comments). Represented here for convenience:
Now it is just the problem that I want the palette to continue on the second plot's boxes and not restart so that I will get different colors on all boxes.
In order to do this, one has to realize that there are 4 fill colors for the first plot BOX1plot, and 2 fill colors for BOX2plot. For BOX1plot, we want the color palette to begin at the first color, but for BOX2plot, we want the palette to start on the 5th color sequence in the palette. There's no way to do this through the scale_*_brewer() functions, so the approach here will be to access the Brewer palette from RcolorBrewer::brewer.pal(), and then assign where to begin and end in that sequence based on the number of levels of each factor using scale_fill_manual() to just set the color values from the extracted Brewer color palette.
You can just "know" that you need to "use colors 1-4" for BOX1plot and "use color 5 and 6" for BOX2plot; however, it is much more elegant to just calculate this automatically based on the number of levels (in case you want to run this again). The code below does this:
library(ggplot2)
library(ggpubr)
library(RColorBrewer)
# ... read in your data as before
# create factors (as OP did before)
BOX1_data$Diagnosis <- as.factor(BOX1_data$Diagnosis)
BOX2_data$Stage <- as.factor(BOX2_data$Stage)
# make color palette based on Brewer "Dark2" palette
lev_diag <- length(levels(BOX1_data$Diagnosis))
lev_stage <- length(levels(BOX2_data$Stage))
lev_total <- lev_diag + lev_stage
my_colors <- brewer.pal(lev_total, "Dark2")
BOX1plot <- ggplot(BOX1_data, aes(x=Diagnosis, y=No.Variants, fill= Diagnosis)) + geom_boxplot() +
scale_fill_manual(values=my_colors[1:lev_diag]) +
scale_x_discrete(labels = c("AC\nN=38", "SqCC\nN=15", "SCLC\nN=8", "BL disease\nN=16"))
BOX2plot <- ggplot(BOX2_data, aes(x=Stage, y=No.Variants, fill = Stage)) + geom_boxplot(width = 0.4) +
scale_fill_manual(values = my_colors[(lev_diag+1):lev_total]) +
scale_x_discrete(labels = c("Stage I-III\nN=24", "Stage IV\nN=37"))
BOX_list <- list(BOX1plot, BOX2plot)
ggarrange(plotlist = BOX_list, labels = c('A', 'B'), ncol = 2)
If you have issues with ggarrange() I would suggest next approach using patchwork:
library(ggplot2)
library(patchwork)
#Data format
BOX1_data$Diagnosis <- as.factor(BOX1_data$Diagnosis)
#Plot 1
BOX1plot <- ggplot(BOX1_data, aes(x=Diagnosis, y=No.Variants, fill= Diagnosis)) + geom_boxplot() +
scale_fill_brewer(palette = "Dark2") +
scale_x_discrete(labels = c("AC\nN=38", "SqCC\nN=15", "SCLC\nN=8", "BL disease\nN=16"))
#Data format
BOX2_data$Stage <- as.factor(BOX2_data$Stage)
#Plot 2
BOX2plot <- ggplot(BOX2_data, aes(x=Stage, y=No.Variants, fill = Stage)) + geom_boxplot(width = 0.4) +
scale_fill_brewer(palette = "Dark2") +
scale_x_discrete(labels = c("Stage I-III\nN=24", "Stage IV\nN=37"))
#Arrange plots
BOX1plot+BOX2plot+plot_annotation(tag_levels = 'A')
The output:

show 3 factors ggplot geom

I am trying to make a PCA plot using ggplot and geom_point.
I would like to illustrate 3 factors (Diet, Time, Antibiotics).
I thought I could outline the points in black for one factor).
However this isn't showing the third factor (Time) for the Fill color.
Here is a subset of my data:
> dput(dat.pcx.annot.test)
structure(list(PC1 = c(25.296379160162, 1.4703101394886, 11.4138097811008,
1.41798772574591, 23.7253675969881, 15.5683516005535, -34.6012195481675,
-25.7129281491955, -2.97230018393742, 4.83421092719293, -0.0274189140249825,
23.227939504077, 15.2002258785889, -35.2243685702227, -34.2537374460037,
-7.6380794043063), PC2 = c(27.2678813936857, -9.88577494210313,
-6.19394322321806, -8.88953660465497, 33.6791127012231, -13.2912233546802,
7.77877968081575, 2.7371646557436, -8.41929538502921, -11.5151849519265,
-9.40733576034963, 32.3549860618533, -11.2170071727855, 10.0455709347794,
3.05679707335492, -6.66218028060621), Diet = structure(c(1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L), .Label = c("RC",
"WD"), class = "factor"), Time = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("ZT14",
"ZT2"), class = "factor"), Antibiotics = structure(c(2L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L), .Label = c("Antibiotics ",
"None"), class = "factor")), row.names = c(1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 18L, 19L, 20L, 21L, 22L), class = "data.frame")
Here is the plotting command :
ggplot(dat.pcx.annot.test,aes(x=PC1,y=PC2,color=Diet,shape=Antibiotics,Fill=Time))+
geom_point(size=3,alpha=0.5)+
scale_color_manual(values = c("black","white") )
And the plot it produces:
I thought if I had both color and fill specified then they would both show.
I would like black outlines for Antibiotics, and Fill color for Time.
Right now Time is not represented.
Any help on how to simultaneously view the 3 factors.
Thanks
Yes I had a fill typo. And I finally figured out how to get the legends to correspond. Here is my final answer.
ggplot(dat.pcx.annot,aes(x=PC1,y=PC2,color=Diet,shape=Antibiotics,fill=Time))+
geom_point(size=3)+
scale_shape_manual(values = c(21, 22) )+
scale_color_manual(values = c("black","white") )+
scale_fill_manual(values=c("#EC9DAE","#AEDE94"))+
xlab(PC1var)+
ylab(PC2var)+
guides(fill=guide_legend(override.aes=list(shape=21)))+
guides(color=guide_legend(override.aes=list(shape=21)))
guides(fill=guide_legend(override.aes=list(shape=21,fill=c("#EC9DAE","#AEDE94"),color=c("black","white"))))
ggsave("cohort2_pca.pdf")

Remove three sides of border around ggplot facet strip label

I have the following graph:
And would like to make what I thought would be a very simple change: I would like to remove the top, right and bottom sides of the left facet label border lines.
How do I do I remove those lines, or draw the equivalent of the right hand lines? I would rather not muck about with grobs, if possible, but won't say no to any solution that works.
Graph code:
library(ggplot2)
library(dplyr)
library(forcats)
posthoc1 %>%
mutate(ordering = -as.numeric(Dataset) + Test.stat,
Species2 = fct_reorder(Species2, ordering, .desc = F)) %>%
ggplot(aes(x=Coef, y=Species2, reorder(Coef, Taxa), group=Species2, colour=Taxa)) +
geom_point(size=posthoc1$Test.stat*.25, show.legend = FALSE) +
ylab("") +
theme_classic(base_size = 20) +
facet_grid(Taxa~Dataset, scales = "free_y", space = "free_y", switch = "y") +
geom_vline(xintercept = 0) +
theme(axis.text.x=element_text(colour = "black"),
strip.placement = "outside",
strip.background.x=element_rect(color = NA, fill=NA),
strip.background.y=element_rect(color = "black", fill=NA)) +
coord_cartesian(clip = "off") +
scale_x_continuous(limits=NULL)
Data:
structure(list(Dataset = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 5L, 5L, 5L, 5L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L), .Label = c("All.habitat", "Aut.habitat", "Habitat.season",
"Lit.season", "Spr.habitat"), class = "factor"), Species = structure(c(1L,
2L, 3L, 5L, 6L, 10L, 11L, 12L, 13L, 1L, 3L, 5L, 6L, 13L, 1L,
2L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L), .Label = c("Ar.sp1",
"Ar.sp2", "Arc.sp1", "B.pus", "Dal.sp1.bumps", "Dip.unID", "I.palladium",
"Pale", "Ph.sp3", "Port", "Somethus", "sty", "Sty.sp1"), class = "factor"),
Species2 = structure(c(2L, 9L, 1L, 4L, 5L, 7L, 11L, 12L,
13L, 2L, 1L, 4L, 5L, 13L, 2L, 9L, 4L, 5L, 6L, 10L, 8L, 7L,
11L, 13L), .Label = c("Arcitalitrus sp1", "Armadillidae sp1 ",
"Brachyiulus pusillus ", "Dalodesmidae sp1", "Diplopoda",
"Isocladosoma pallidulum ", "Ommatoiulus moreleti ", "Philosciidae sp2",
"Porcellionidae sp1", "Siphonotidae sp2", "Somethus sp1",
"Styloniscidae ", "Styloniscidae sp1"), class = "factor"),
Taxa = structure(c(3L, 3L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
1L, 2L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 3L), .Label = c("Amphipoda",
"Diplopoda", "Isopoda"), class = "factor"), Variable = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Autumn", "Litter",
"Spring", "Summer"), class = "factor"), Coef = c(1.911502938,
2.086917154, 1.571872993, 12.61184801, 15.6161116, -1.430032837,
-12.51944478, 12.33934516, -8.040249562, 8.08258816, 1.780142396,
12.88982576, 16.78107544, -13.22641153, 1.68810887, 2.093965381,
12.27209197, 15.08328526, -6.334640911, -11.29985948, -11.62658947,
-1.676293808, -6.246555908, -3.470297147), SE = c(0.403497472,
2.21607562, 0.348600794, 2.423896379, 0.509468128, 3.423013791,
2.382857733, 1.775086895, 2.087788334, 2.23631504, 0.33402261,
2.518562443, 0.459720131, 1.950974996, 0.2476205, 0.235648095,
1.815155489, 0.325804415, 2.564680067, 2.437104984, 2.212583358,
2.677618401, 2.324019051, 0.420436743), Test.stat = c(18.36532749,
13.27324683, 13.29039037, 20.50277493, 44.06097153, 10.55234932,
14.64951518, 13.22575401, 20.16415411, 16.55627107, 11.81407568,
15.15213717, 40.67205188, 12.62233207, 37.60085488, 16.90879258,
20.20215107, 80.30520371, 13.35250626, 13.01692428, 17.52987519,
20.03658771, 12.02467914, 53.5052683)), row.names = 10:33, class = "data.frame")
This solution is based on grobs: find positions of "strip-l" (left strips) and then substitute the rect grobs with line grobs.
p <- posthoc1 %>%
mutate(ordering = -as.numeric(Dataset) + Test.stat,
Species2 = fct_reorder(Species2, ordering, .desc = F)) %>%
ggplot(aes(x=Coef, y=Species2, reorder(Coef, Taxa), group=Species2, colour=Taxa)) +
geom_point(size=posthoc1$Test.stat*.25, show.legend = FALSE) +
ylab("") +
theme_classic(base_size = 20) +
facet_grid(Taxa~Dataset, scales = "free_y", space = "free_y", switch = "y") +
geom_vline(xintercept = 0) +
theme(axis.text.x=element_text(colour = "black"),
strip.placement = "outside",
#strip.background.x=element_rect(color = "white", fill=NULL),
strip.background.y=element_rect(color = NA)
) +
coord_cartesian(clip = "off") +
scale_x_continuous(limits=NULL)
library(grid)
q <- ggplotGrob(p)
lg <- linesGrob(x=unit(c(0,0),"npc"), y=unit(c(0,1),"npc"),
gp=gpar(col="red", lwd=4))
for (k in grep("strip-l",q$layout$name)) {
q$grobs[[k]]$grobs[[1]]$children[[1]] <- lg
}
grid.draw(q)

Order geom_lines from the highest to the lowest in each facet

I have a factor comp_id that has 4 levels (comp1 to comp4). I want to order each level from the highest to the lowest in a geom_line plot.
I got this plot
using this script
library(data.table)
library(ggplot2)
dat <- as.data.table(df)
dat[, ord := sprintf("%02i", frank(dat, comp_id, -value, ties.method = "first"))]
ggplot(dat, aes(x = ord, y = value , group = comp_id , colour = comp_id))+
geom_line()+
facet_wrap(~comp_id, ncol = 1, scales = "free_x", labeller = label_parsed, drop = TRUE)+
theme(axis.text.x=element_text(angle=35, vjust=1, hjust=1,
))
to replace x axis labels
+scale_x_discrete(labels = dat[, setNames(as.character(predictor), ord)])
As you can see, it worked fine for all levels except comp3 where variables ordered (100 to 105) were plotted at the start of facet where they were supposed to be plotted at the end. I wonder what went wrong. Any suggestions will be appreciated.
DATA
> dput(df)
structure(list(predictor = c("c_C2", "c_C3", "c_C4", "d_D2",
"d_D3", "d_D4", "d_D5", "h_BF", "h_BFI", "h_ER", "h_f", "h_PET",
"h_QuFl", "h_Ra", "l_Da", "l_NaCo", "l_ShBe", "m_a", "m_DrDe",
"m_ElRa", "m_MeElm", "m_MeSlPe", "Mr_Co", "Mr_GRAv", "Mr_GREy",
"Mr_Mu", "Mr_Sa", "s_SaLo", "s_SiLo", "s_sSiLo", "s_Stl", "Sr_Li",
"Sr_SaCoCoTe", "Sr_SaLoSi", "Sr_SaMubcl", "c_C2", "c_C3", "c_C4",
"d_D2", "d_D3", "d_D4", "d_D5", "h_BF", "h_BFI", "h_ER", "h_f",
"h_PET", "h_QuFl", "h_Ra", "l_Da", "l_NaCo", "l_ShBe", "m_a",
"m_DrDe", "m_ElRa", "m_MeElm", "m_MeSlPe", "Mr_Co", "Mr_GRAv",
"Mr_GREy", "Mr_Mu", "Mr_Sa", "s_SaLo", "s_SiLo", "s_sSiLo", "s_Stl",
"Sr_Li", "Sr_SaCoCoTe", "Sr_SaLoSi", "Sr_SaMubcl", "c_C2", "c_C3",
"c_C4", "d_D2", "d_D3", "d_D4", "d_D5", "h_BF", "h_BFI", "h_ER",
"h_f", "h_PET", "h_QuFl", "h_Ra", "l_Da", "l_NaCo", "l_ShBe",
"m_a", "m_DrDe", "m_ElRa", "m_MeElm", "m_MeSlPe", "Mr_Co", "Mr_GRAv",
"Mr_GREy", "Mr_Mu", "Mr_Sa", "s_SaLo", "s_SiLo", "s_sSiLo", "s_Stl",
"Sr_Li", "Sr_SaCoCoTe", "Sr_SaLoSi", "Sr_SaMubcl", "c_C2", "c_C3",
"c_C4", "d_D2", "d_D3", "d_D4", "d_D5", "h_BF", "h_BFI", "h_ER",
"h_f", "h_PET", "h_QuFl", "h_Ra", "l_Da", "l_NaCo", "l_ShBe",
"m_a", "m_DrDe", "m_ElRa", "m_MeElm", "m_MeSlPe", "Mr_Co", "Mr_GRAv",
"Mr_GREy", "Mr_Mu", "Mr_Sa", "s_SaLo", "s_SiLo", "s_sSiLo", "s_Stl",
"Sr_Li", "Sr_SaCoCoTe", "Sr_SaLoSi", "Sr_SaMubcl"), comp_id = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("comp1",
"comp2", "comp3", "comp4"), class = "factor"), value = c(0.0633325075111356,
-0.0193713154441617, 0.000785081075580719, 0.287610195287972,
-0.0913783988809322, -0.122928438782758, 0.305621459875726, 0.0356570047659489,
0.367574915852176, -0.240835821698893, 0.0035597425358522, 0.295952594554233,
-0.0439920206129066, -0.235580426938533, 0.191947159509267, -0.132931615006652,
0.065155805120025, 0.038311284807646, 0.187182963731454, 0.120969596703282,
-0.118935354491654, -0.173851183397175, 0.125870264508295, 0.158977975187947,
-0.209351605852615, -0.0231602829054583, 0.078383405846316, 0.0959455355349004,
0.238306328058919, -0.188667962455942, -0.138302814516594, -0.0586994514783439,
0.019524606432138, 0.210636138928319, -0.204454169255484, -0.149879080476447,
0.282741114373524, -0.272911905666994, 0.102508662574812, -0.35056583225677,
0.257262737814283, 0.202117594283655, 0.191773977367133, 0.298513575892895,
0.139576016330362, 0.165641757285727, -0.071542760140058, 0.116819894570386,
0.145104320521166, 0.126636637925691, 0.0810830011112734, -0.0949935353116725,
0.0785254958291791, 0.0326439188223452, 0.065833153228218, 0.155405435626813,
0.128737420120173, 0.214943178842044, -0.0210359058420932, 0.0117832135586799,
0.0762824228178598, -0.29145271973574, -0.17089908579109, -0.0992003952524557,
0.163749177828358, 0.196561728687348, 0.0951493527111932, 0.17238711709624,
0.0638301486629609, -0.0351097560634362, 0.0647994534663104,
-0.154895398844537, 0.186448424833243, 0.240881706707846, -0.241364320964797,
-0.089459273670017, 0.0491598702691844, -0.200660845431752, -0.0339722426751736,
0.131396251991635, -0.195471026941394, -0.05919918680627, -0.184160478394361,
0.129464190293723, 0.193021703469902, 0.178985522376368, -0.245966624042807,
-0.23478025602535, 0.198620462933836, -0.157573246492692, -0.00808698000885529,
0.0413693509741982, -0.121020524702316, 0.105148862728949, 0.214386790903084,
-0.204515275979768, -0.0906160054540168, -0.276985960928353,
0.0768294557774406, -0.074181085595352, 0.138680723918144, -0.119684214245213,
-0.0919678069134681, 0.322602153170851, 0.228878715511945, -0.433082572929477,
0.05754301130056, 0.130719232236558, 0.253999327778221, 0.0469683234741709,
-0.0258294537417061, -0.258318910865727, -0.00406472629347961,
-0.165003562015847, -0.0292142578447021, 0.00862320222199929,
0.0875367120866572, 0.0331716236283754, -0.0418387105725687,
-0.12523142839593, -0.200857915084298, 0.138378222132672, 0.00992811008724002,
-0.0201043482518474, -0.148894977354092, -0.323240591170999,
-0.0556713655820164, 0.379033571103569, -0.264420286734383, 0.127560649906739,
-0.00546455207923468, -0.203293330594455, -0.122085266718802,
-0.0970860819632599, -0.173818516285048, -0.0585031143296301,
0.125084378608705, 0.0655074180474436, 0.254339734692359, 0.00114212078410835
)), class = "data.frame", .Names = c("predictor", "comp_id",
"value"), row.names = c(NA, -140L))
Here is an approach using tidyverse and continuous scale
library(tidyverse)
df %>%
arrange(comp_id, desc(value)) %>% #arrange by comp_id and descending value
mutate(ord = 1:n()) -> dat #create the x scale
ggplot(dat, aes(x = ord, y = value , group = comp_id , colour = comp_id))+
geom_line()+
facet_wrap(~comp_id, ncol = 1, scales = "free_x", drop = TRUE)+
theme(axis.text.x=element_text(angle=35, vjust=1, hjust=1)) +
scale_x_continuous(labels = dat$predictor, breaks = dat$ord, expand = c(0.02, 0.02))
In addition to the nice answer by #missuse, there was another way that gave me what I wanted.
using as factor / as numeric / as.character with the x axis
aes(x = as.factor(as.numeric(as.character(ord)))
and using as numeric /as character while replacing the x axis labels
as.numeric(as.character(ord))
The final script is
ggplot(dat, aes(x = as.factor(as.numeric(as.character(ord))), y = value , group = comp_id , colour = comp_id))+
geom_line()+
facet_wrap(~comp_id, ncol = 1, scales = "free_x", labeller = label_parsed, drop = TRUE)+
theme(axis.text.x=element_text(angle=35, vjust=1, hjust=1,
))+
scale_x_discrete(labels = dat[, setNames(as.character(predictor), as.numeric(as.character(ord)))])

Resources