簡體   English   中英

如何在ggplot2中的軸上繪制單個值?

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

我要創建一個情節,類似於以下內容:

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)

該圖如下所示: 在此處輸入圖片說明

現在,我希望單點顯示在Y軸上。

我知道我應該為此更改“ scale_x_continuous”限制(從0.8到0),但是隨后得到以下圖形,這不是我想要的:

在此處輸入圖片說明

該怎么辦?

這是最終的通緝結果: 在此處輸入圖片說明

由於下面的評論,我意識到了如何做到這一點:

 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)

您應該在scale_x_continuous expand = c(0,0)) ,並添加coord_cartesian(clip = 'off')以免剪切點(形狀的一半落在繪圖區域之外)。

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM