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)
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")
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))
dummy2$X <- factor(dummy2$X)
ggplot(dummy1, aes(x = D, y = Y)) + geom_point() + facet_grid(~X) +
geom_hline(data = dummy2, aes(yintercept = Z))
Add a line to each facet in a grid
You need to add the lower
specification to match the faceted data like so:
library('dplyr')
df <- df %>% mutate(lower = rep(c(10,20,30), each =3))
df
dates types vals lower
1 2018-02-01 a 10 10
2 2018-03-01 a 11 10
3 2018-04-01 a 12 10
4 2018-05-01 b 20 20
5 2018-06-01 b 21 20
6 2018-07-01 b 22 20
7 2018-08-01 c 30 30
8 2018-09-01 c 31 30
9 2018-10-01 c 32 30
Then specify the plot as before and add the geom_hline
like so on the altered df with lower
column - like so:
gg + geom_hline(aes(yintercept = lower), color = 'red')
Then you get something like this:
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")
Adding a label to a straight line in each facet of a facet wrapped ggplot
You just need to pass a different dataset to the labeling layer that still preserves your faceting variable. This will work using dplyr
g <- g +
geom_text(data = tdf %>%
group_by(b) %>%
summarize(median = median(m3)),
aes(x = as.POSIXct(-Inf, origin="1970-01-01"),
y = Inf,
label = median),
size = 2,
hjust = -0.5,
vjust = 1.4,
inherit.aes = FALSE)
We also have to explicitly convert the x
to a date/time value for the axis to work.
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)
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:
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)
Related Topics
Shade Region Between Two Lines with Ggplot
Summing Across Rows of a Data.Table for Specific Columns
Connecting Points with Lines in Ggplot2 in R
Mutating Multiple Columns in a Data Frame Using Dplyr
Error When I Try to Predict Class Probabilities in R - Caret
How to Correctly Interpret Ggplot's Stat_Density2D
How to Create Datatable with Complex Header in R Shiny
Visualise Distances Between Texts
How to Get the Text Between Two Words in R
Rollmean with Dplyr and Magrittr
How to Generate Bin Frequency Table in R
Difference Between Read.Csv() and Read.Csv2() in R