1

I have creted the plot below and I would like to add custom text in the x-axis in the points where those 2 vertical lines cross the x-axis like in the example picture below. The first βi and the second βj. How is that possible?

enter image description here

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

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)) +
  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")
1
  • do you want to keep the other numbers in the x axis? Commented Dec 6, 2020 at 23:00

2 Answers 2

2

You could use Unicode escapes in the x axis labels:

library(ggplot2)

ggplot(df, aes(theta, P_item_rasch)) +
  geom_line(aes(color = number)) +
  geom_segment(x = -1, xend = 1, y = 0.5, yend = 0.5, lty = 2) +

  geom_segment(x = -Inf, xend = -1, y = 0.5, yend = 0.5, lty = 2) +

  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_x_continuous(breaks = c(-6, -3, -1, 0, 1, 3, 6),
                     labels = c(-6, -3, "β\u1d62", 0, "β\u2c7c", 3, 6),
                     limits = c(-6, 6), name = "\u03b8") +

  scale_color_manual(values = RColorBrewer::brewer.pal(4, "Set1")[-1]) +
  theme_classic() +
  theme(legend.position = "none")

enter image description here

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

3 Comments

just a comment. if I want to write βι=βj as a label how should I write it?
"β\u1d62 = β\u2c7c"
thank you again. is this Q possible? stackoverflow.com/questions/65184734/…
2

This can be useful. As I was not clear I used @AllanCameron solution for hint on placement of labels (many thanks):

library(ggplot2)
#Code
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")+
  scale_x_continuous(breaks = c(-6,-3,-1,0,1,3,6),limits = c(-6,6),
                     labels=c(-6,-3,expression(beta[i]),0,
                              expression(beta[j]),
                              3,6))+
  xlab(expression(theta))

Output:

enter image description here

Comments

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.