简体   繁体   中英

Convert plot to ggplot and add horizontal lines until specific points

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.

在此处输入图像描述

# 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)

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")

在此处输入图像描述 Created on 2020-12-06 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM