How to Add Different Lines for Facets

How to add different lines for facets

Make sure that the variable species is identical in both datasets. If it a factor in one on them, then it must be a factor in the other too

library(ggplot2)
dummy1 <- expand.grid(X = factor(c("A", "B")), Y = rnorm(10))
dummy1$D <- rnorm(nrow(dummy1))
dummy2 <- data.frame(X = c("A", "B"), Z = c(1, 0))
ggplot(dummy1, aes(x = D, y = Y)) + geom_point() + facet_grid(~X) +
geom_hline(data = dummy2, aes(yintercept = Z))

Sample Image

dummy2$X <- factor(dummy2$X)
ggplot(dummy1, aes(x = D, y = Y)) + geom_point() + facet_grid(~X) +
geom_hline(data = dummy2, aes(yintercept = Z))

Sample Image

How to add horizontal lines in different facets for 2x2 arrangements using ggplot2?

For the intercept to show up in specific panels, you'll need to have the Row referred to in facet_grid available as a variable inside Lines_in_plot. You'll also want to put yintercept inside aes so that ggplot knows to refer to the Lines_in_plot data for that yintercept.

...
#Auxiliar DF
Row <- c("a", "b")
Lines <- c(0.5, 1)
Lines_in_plot <- data.frame(Row, Lines)
Lines_in_plot$Row <- factor(Lines_in_plot$Target)

#Plot
ggplot(data = DF, aes(y = values)) +
geom_bar() +
facet_grid(Row~Column,
scales = "free") +
geom_hline(data = Lines_in_plot,
aes(yintercept = Lines),
linetype = "dashed",
color = "red")

Sample Image

Different `geom_hline()` for each facet of ggplot

If you have the value you wish to use for each facet as a column in the data frame, and that value is unique within each facet, then you can use geom_hline(aes(yintercept=column)), which will then plot a horizontal line for each of the facets

Different vertical line for each facet in ggplot

The issue was with that 'Mean' variable. Its format is not consistent with primary dataset. Structure it in the same way and the faceting will work as expected.

library(dplyr)
library(tidyr)
library(ggplot2)
# For all characteristics :
Mean <- colMeans(iris[-5]) %>% as_tibble(rownames = "Characteristic")
iris %>% pivot_longer(cols = !Species,names_to = "Characteristic",values_to = "Value") %>%
ggplot(aes(x=Value,fill=Species))+
geom_density()+
geom_vline(aes(xintercept = value),data=Mean) +
facet_wrap(~Characteristic)

Adding different lines to some facets in a facet_wrap on gather:ed data

Perhaps something like this?

(BTW it is good practice to make your question reproducible, either by using a built-in data set or by including a sample of data structured like yours by either using dput(YOUR_DATA) or providing code that generates it.)

mtcars %>%
ggplot(aes(wt, mpg)) +
geom_point() +
#important, this should have the faceted variable
geom_hline(data = tibble(gear = 3, y = 30), aes(yintercept = y)) +
facet_wrap(~gear)

Sample Image

Put 2 or more lines in each facet using facet_wrap

Try this reshaping data to long:

library(ggplot2)
library(tidyverse)
#Code
df %>% pivot_longer(-c(Day,Type)) %>%
ggplot(aes(x=Day, y=value, color= name,group=name))+
geom_line()+
facet_wrap(~Type)

Output:

Sample Image

How to add different vertical lines on specific plots of the two-factor facets using ggplot2?

The answer is given in the help file ?geom_hline()

The example there is:

# To show different lines in different facets, use aesthetics
p <- ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
facet_wrap(~ cyl)

mean_wt <- data.frame(cyl = c(4, 6, 8), wt = c(2.28, 3.11, 4.00))
p + geom_hline(aes(yintercept = wt), mean_wt)

So for your example this could be:

# create new dataframe
intercept <- data.frame(vs=c(rep(0, 2), rep(1, 2), rep(0,3), rep(1,3)),
am = c(rep(0, 4), rep(1, 6)),
int = c(10, 20, 10, 20, 15, 25, 35, 15, 25, 35))
# add vline to plot
p + geom_vline(aes(xintercept=int), intercept)

Sample Image

How can I add a line to one of the facets?

I don't have your data, so I made some up:

df <- data.frame(x=rnorm(100),y=rnorm(100),z=rep(letters[1:4],each=25))

ggplot(df,aes(x,y)) +
geom_point() +
theme_bw() +
facet_wrap(~z)

Sample Image

To add a vertical line at x = 1 we can use geom_vline() with a dataframe that has the same faceting variable (in my case z='b', but yours will be levels='major'):

ggplot(df,aes(x,y)) +
geom_point() +
theme_bw() +
facet_wrap(~z) +
geom_vline(data = data.frame(xint=1,z="b"), aes(xintercept = xint), linetype = "dotted")

Sample Image

ggplot: Add different lines in each facet of geom_bar

Both datasets need to share the same column.

dummy2 <- data.frame(s = levels(i$s)[-1], Z = c( 4,16,16,8,4))
dummy2$s <- factor(dummy2$s)

ggplot(ii, aes(reorder_within(sn, nbr, s), nbr)) +
geom_bar(stat = 'identity') +
scale_x_reordered() +
facet_wrap(.~ s, ncol=2,scales = "free_x") +
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=.5,colour='gray50'))+
geom_hline( aes(yintercept = Z),data = dummy2)

Sample Image



Related Topics



Leave a reply



Submit