YefR
YefR

Reputation: 369

How to plot a single value on an axis in ggplot2?

I want to create a plot, similar to the one below:

set.seed(8)
xpos<-rep(1:4,2)
sample<-rep(c("One Stage","Two Stage"),each=4)
dat<-data.frame(cbind(xpos,choice,sample))
dat$xpos<-as.integer(dat$xpos)
dat$choice<-(c(.2,.4,.3,.22,.17,.03,.081,.035))
dat$sample<-as.character(dat$sample)

ggplot(data=dat, aes(x=xpos, y=choice, group=sample, shape=factor(sample), colour=factor(sample))) + 
  geom_line(aes(linetype=factor(sample)))+
  geom_point(size=3)+
  geom_point(aes(x=1, y=0.5),size=3)+
  scale_linetype_manual(values=c("longdash", "dotted"))+
  scale_x_continuous("Block", breaks=seq(1,4,1),limits=c(0.8,4.2))+
  scale_y_continuous("Choice Rates", breaks=seq(0,1,0.2),limits=c(-0.02,1))+
  theme_classic(base_size = b.size)+
  labs(title="Model predictions" )+ theme(plot.title = element_text(hjust=0.5))+
  theme(legend.title =element_blank(),legend.position=c(.8,.85)) + scale_color_grey(start=0.2, end=0.2)

Here is how the graph looks like: enter image description here

Now, I want the single point to appear on the Y-Axis.

I know that I should change for this the "scale_x_continuous" limits (from 0.8, to 0), but then I get the following graph, which is not what I want:

enter image description here

What can be done?

Here is the final wanted result: enter image description here

Thanks to the comments below, I realized how to do it:

 ggplot(data=dat, aes(x=xpos, y=choice, group=sample, shape=factor(sample), 
  colour=factor(sample))) + 
  geom_line(aes(linetype=factor(sample)))+
  geom_point(size=3)+
  geom_point(aes(x=0.8, y=0.5),size=3)+
  scale_linetype_manual(values=c("longdash", "dotted"))+
  scale_x_continuous("Block", breaks=seq(1,4,1),limits=c(0.8,4.2),expand=c(0,0))+
  coord_cartesian(clip = 'off') +
  scale_y_continuous("Checking Rates", breaks=seq(0,1,0.2),limits=c(-0.02,1))+
  theme_classic(base_size = b.size)+
  labs(title="Model predictions" )+ theme(plot.title = element_text(hjust=0.5))+
  theme(legend.title =element_blank(),legend.position=c(.8,.85)) + scale_color_grey(start=0.2, end=0.2)

Upvotes: 0

Views: 649

Answers (1)

dww
dww

Reputation: 31454

You should us expand = c(0,0)) in the scale_x_continuous, and add coord_cartesian(clip = 'off') so the points don't get clipped (where half of the shape falls outside the plotting area).

ggplot(data=dat, aes(x=xpos, y=choice, group=sample, shape=factor(sample), colour=factor(sample))) + 
  geom_line(aes(linetype=factor(sample)))+
  geom_point(size=3)+
  geom_point(aes(x=1, y=0.5),size=3)+
  scale_linetype_manual(values=c("longdash", "dotted"))+
  scale_x_continuous("Block", breaks=seq(1,4,1),
    limits=c(1,4.2), expand = c(0,0))+
  coord_cartesian(clip = 'off') +
  scale_y_continuous("Choice Rates", breaks=seq(0,1,0.2),limits=c(-0.02,1))+
  labs(title="Model predictions" )+ theme(plot.title = element_text(hjust=0.5))+
  theme(legend.title =element_blank(),legend.position=c(.8,.85)) + 
  scale_color_grey(start=0.2, end=0.2) +
  theme_classic()

enter image description here

Upvotes: 2

Related Questions