0

Update

I managed to get the vline to show by doing as @danlooo suggested,

geom_vline(aes(xintercept= 4.7) # 4.7 is the rough position of the first value in VLineValues <- c(25, 26, 27), between level 12 & 40 (also position 4 and 5)

However, since I have a lot of plots in my real data that are generated in a loop with 3 different vlines it's hard to find a formula to get the position right for all of the vlines.

Therefore I then tried to add a secondary axis with sec.axis = sec_axis() . Unfortunately you can't add a continuous axis to a discrete on, you'll get the following error

Error: Discrete value supplied to continuous scale

Then I tried to plot the data with a contous axis and add the factorized one as the secondary. However that "squeezes" the graph together which makes it loose it characteristic S-shape.

So I eventually just gave up on making these particular plots with ggplot, and instead just used the "normal" plot() function, which works great even-though it doesn't look as great as the ggplots....


I'm trying to add a vertical line to a ggplot with a factorized x-axis (it has to be factorized)using geom_vline. However the line does not show up.

I have tried several suggested solutions from:

Positioning geom_vline with dates on x-axis

Draw geom_vline in discrete x axis

ggplot geom_vline on x-axis of class date

https://www.py4u.net/discuss/866042 (this solution is for python...)

https://github.com/tidyverse/ggplot2/issues/3197

https://github.com/tidyverse/ggplot2/issues/4285

https://community.rstudio.com/t/how-to-add-a-vertical-line-on-a-factor/38341

Issue with a drawing a vertical line in ggplot for categorical variable x-axis in R

This is an example of my current code:

library(drc)
library(ggplot2)
library(data.table)
library(tidyverse)

con <-  c(0,1,4,12,40,100,300,1000, 0,1,4,12,40,100,300,1000,0,1,4,12,40,100,300,1000)
Vector <- c(1.2, 1.5, 1.8, 2, 2.7, 3, 3.5, 4,1.2, 1.5, 1.8, 2, 2.7, 3, 3.5, 4,1.2, 1.5, 1.8, 2, 2.7, 3, 3.5, 4)
df <- data.frame(Vector, con)

VLineValues <- c(25, 26, 27)

  dfTemp <- df[1:8,]
  dfTemp$X = con[1:8]
  dfTemp$X <- factor(dfTemp$X, levels=unique(dfTemp$X))
  
  print(ggplot(dfTemp, aes(x = X, y = Vector))+
        ylim(1, 4.5)+
        geom_point()+
        geom_vline(aes(xintercept= VLineValues[1] )+
        geom_smooth(method = drm, method.args = list(fct = LL.5()), se = FALSE))
       

What I want to do is to loop over the df, get 3 separate plots with one line each at x = 25, x=26 & x=27.

As I mentioned above I've tried some other solutions where you get the position of the levels, mutate? the data with tidyverse and so forth.

The Vline shows up fine if the x-axis is numeric or if I for example write

geom_vline(1:5) #Gives Vlines at the 5 first levels in the factorised x-axis

However, it does not show up between the levels on the factorised x-axis

Any suggestions on how to get around this issue?

Norruas
  • 61
  • 1
  • 9

1 Answers1

2

A factor can only consist of values within given levels:

> dfTemp$X
[1] 0    1    4    12   40   100  300  1000
Levels: 0 1 4 12 40 100 300 1000

None of your x intercept values (25, 26, 27) are within the levels thus one can not plot the vertical lines.

xintercept can be set using either the string or the position of the levels:

library(tidyverse)

data <-
  iris %>%
  mutate(Species = Species %>% as.factor())

data %>%
  ggplot(aes(Species, Sepal.Length)) +
    geom_point() +
    geom_vline(xintercept = 2) +
    geom_vline(xintercept = "setosa")

levels(data$Species)
danlooo
  • 10,067
  • 2
  • 8
  • 22
  • Thank you for the clarification. So if I understand it correctly it isn't possible to add xintercept between the levels? In the provided example the xintercept is either at the setosa level (position 1) or at position 2 (versicolor level) – Norruas Sep 08 '21 at 08:46
  • 1
    That is the nature of factors: They are discrete. Only in continuous scales like (real) numbers you can always put another number in between. – danlooo Sep 08 '21 at 08:49
  • Ah, too bad, was not aware of that... Great suggestion with the secondary x-axis! I'll try that, thanks – Norruas Sep 08 '21 at 09:11