0

I have creted the plot below using the base R plot() function but I would like to convert it to ggplot() and also add horizontal lines like in the example picture but until the crossing with the graphs and not a full continuing horizontal line until the end.

enter image description here

# Figure 3.1 & 3.2

# curve(logistic(pnorm(x), a=1, d=0),-3,3,ylab="Probability of x",
#       main="Logistic transform of x",xlab="z score units") 
# #logistic with a=1.702 is almost the same as pnorm 
# curve(logistic(pnorm(x), d=1),add=TRUE)


# Set x-axis values
theta <- seq(from = -10, to = 10, by = 0.001)

# Code for plot1

B_i <- 1
B_j <- -1
P_item1_rasch <- NULL
P_item2_rasch <- NULL
for (i in 1:length(theta)){
      P_item1_rasch[i] <- (exp((theta[i]-B_i)))/(1+(exp((theta[i]-B_i))))
      P_item2_rasch[i] <- (exp((theta[i]-B_j)))/(1+(exp((theta[i]-B_j))))
  }

#select the colors that will be used
library(RColorBrewer)
#all palette available from RColorBrewer
display.brewer.all()
#we will select the first 4 colors in the Set1 palette
cols<-brewer.pal(n=4,name="Set1")
#cols contain the names of four different colors



plot(theta, P_item1_rasch, xlim=c(-4,4), ylim=c(0,1))
lines(theta, P_item2_rasch,col=cols[2])

# Add lines at the values below, but only half as in the example Figures
# abline(h=0.5)
# abline(v=-1)
# abline(v=1)

1 Answer 1

1

Perhaps something like this?

theta <- seq(from = -10, to = 10, by = 0.001)

# Code for plot1

B_i <- 1
B_j <- -1
P_item0_rasch <- NULL
P_item1_rasch <- NULL
P_item2_rasch <- NULL
for (i in 1:length(theta)){
      P_item0_rasch[i] <- (exp((theta[i])))/(1+(exp((theta[i]))))
      P_item1_rasch[i] <- (exp((theta[i]-B_i)))/(1+(exp((theta[i]-B_i))))
      P_item2_rasch[i] <- (exp((theta[i]-B_j)))/(1+(exp((theta[i]-B_j))))
}

df <- data.frame(theta = rep(theta, 3),
                 P_item_rasch = c(P_item0_rasch, P_item1_rasch, P_item2_rasch),
                 number = factor(rep(1:3, each = length(theta))))

library(ggplot2)

ggplot(df, aes(theta, P_item_rasch, color = number)) +
  geom_line() +
  lims(x = c(-6, 6)) +
  geom_segment(x = -1, xend = 1, y = 0.5, yend = 0.5, lty = 2) +
  geom_vline(xintercept = c(-1, 0, 1), lty = 2) +
  scale_color_manual(values = RColorBrewer::brewer.pal(4, "Set1")[-1]) +
  theme_classic() +
  theme(legend.position = "none")
#> Warning: Removed 24000 row(s) containing missing values (geom_path).


Edit

The OP changed the question to alter the requirements. Here is a way to achieve them:

ggplot(df, aes(theta, P_item_rasch)) +
  geom_line(aes(color = number)) +
  lims(x = c(-6, 6)) +
  # Line between curves
  geom_segment(x = -1, xend = 1, y = 0.5, yend = 0.5, lty = 2) +
  # Optional line on left
  geom_segment(x = -Inf, xend = -1, y = 0.5, yend = 0.5, lty = 2) +
  # Lower lines
  geom_segment(data = data.frame(theta = c(-1, 0, 1), P_item_rasch = rep(-Inf, 3)),
               aes(xend = theta, yend = 0.5), lty = 2) +
  # Upper lines
  #geom_segment(data = data.frame(theta = c(-1, 0, 1), P_item_rasch = rep(Inf, 3)),
  #            aes(xend = theta, yend = 0.5), lty = 2) +
  scale_color_manual(values = RColorBrewer::brewer.pal(4, "Set1")[-1]) +
  theme_classic() +
  theme(legend.position = "none")

enter image description here Created on 2020-12-06 by the reprex package (v0.3.0)

Sign up to request clarification or add additional context in comments.

4 Comments

It looks nice and clean, not totally sure about the upper part of the lines. I have made yellow which could be removed and which could be added. Also would be nice if the middle line would be black, is that possible? the red line could be added* like in my edit.
@firmo23 it's a bit disheartening when the OP changes their requirements after the original question has been answered exactly as asked, but hopefully my update now gives you what you need, and you should be able to see from it how to add whatever segments you like.
You are right. I shlould have created a new Q. Tnx. I will try to understand the code and if needed I will create new Q in the future.
probably u could help with that as well stackoverflow.com/questions/65173961/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.